GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Sep-2005, 23:45
Danny Danny is offline
New Member
 
Join Date: Sep 2005
Posts: 4
Danny is on a distinguished road

Problem with typedef struct, cannot malloc(sizeof(struct element)); unless at the top


Here is the .c, THIS will work

CPP / C++ / C Code:
//DDList.c
#include <stdio.h>
#include <malloc.h>
#include "DLList.h"
void main() 
{
      p_addy2 temp2 = malloc(sizeof(struct element));
      p_addy temp = malloc(sizeof(struct element));
//      temp->next = NULL; // LOOK HERE <--------------------
      p_addy3 temp3 = malloc(sizeof(struct element));
      temp->next = NULL; // AND LOOK HERE <----------------
      temp->num = 5;

      printf("%i", temp->num);

      free(temp);
      free(temp2);
      free(temp3);
}
and the .h:
CPP / C++ / C Code:
//DLList.h

typedef struct element *p_addy3;
typedef struct element *p_addy2;
typedef struct element *p_addy;

struct element
{
      int num;
      struct element *prev;
      struct element *next;
};

BUT, this one will not work:

CPP / C++ / C Code:
//DDList.c
#include <stdio.h>
#include <malloc.h>
#include "DLList.h"
void main() 
{
      p_addy2 temp2 = malloc(sizeof(struct element));
      p_addy temp = malloc(sizeof(struct element));
      temp->next = NULL; // LOOK HERE <---------- this line is un-commented
      p_addy3 temp3 = malloc(sizeof(struct element));
//      temp->next = NULL; // AND LOOK HERE <----------------
      temp->num = 5;        //  ^^^ This line is commented

      printf("%i", temp->num);

      free(temp);
      free(temp2);
      free(temp3);
}
and the .h is the same.

compiler error:
Quote:
c:\home\programming\CTestPrograms\DLList\DLList.c( 15) : error C2275: 'p_addy3' : illegal use of this type as an expression
c:\home\programming\CTestPrograms\DLList\DLList.h( 4) : see declaration of 'p_addy3'
c:\home\programming\CTestPrograms\DLList\DLList.c( 15) : error C2146: syntax error : missing ';' before identifier 'temp3'
c:\home\programming\CTestPrograms\DLList\DLList.c( 15) : error C2144: syntax error : '<Unknown>' should be preceded by '<Unknown>'
c:\home\programming\CTestPrograms\DLList\DLList.c( 15) : error C2144: syntax error : '<Unknown>' should be preceded by '<Unknown>'
c:\home\programming\CTestPrograms\DLList\DLList.c( 15) : error C2143: syntax error : missing ';' before 'identifier'
c:\home\programming\CTestPrograms\DLList\DLList.c( 15) : error C2065: 'temp3' : undeclared identifier
c:\home\programming\CTestPrograms\DLList\DLList.c( 15) : warning C4047: '=' : 'int' differs in levels of indirection from 'void *'
c:\home\programming\CTestPrograms\DLList\DLList.c( 27) : warning C4022: 'free' : pointer mismatch for actual parameter 1

Build log was saved at "file://c:\home\programming\CTestPrograms\DLList\Debug\Bui ldLog.htm"
DLList - 6 error(s), 2 warning(s)

Keep in mind that temp has allready been malloc'ed, SOOOOO why won't the second one work? Do I have to do ALL my malloc'ing at the beginning of the function? I don't get it, temp has nothing to do with temp3 OR even p_addy3! Or does it...
  #2  
Old 11-Sep-2005, 00:46
Dr. Evil Dr. Evil is offline
Member
 
Join Date: Oct 2004
Location: Netherlands
Posts: 120
Dr. Evil will become famous soon enough
Maybe your compile dosn't allow nested declarations.

EDIT: I get the same type of thing with Borland, so it probably is the fact that it's nested.
  #3  
Old 11-Sep-2005, 01:56
Danny Danny is offline
New Member
 
Join Date: Sep 2005
Posts: 4
Danny is on a distinguished road
Quote:
Originally Posted by Dr. Evil
Maybe your compile dosn't allow nested declarations.

EDIT: I get the same type of thing with Borland, so it probably is the fact that it's nested.

OMG, that's right, I'm writing in C! I think only cpp allows declerations "willy nilly". Thanks. BTW, I'm using VC++ 2003 .NET

Thanks.
 
 

Recent GIDBlogToyota - 2008 September Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
2D arrays:dynamic allocation and freeing bravetanveer C Programming Language 48 27-Nov-2007 15:55
Urgent ! Pls Help Me ! mycashmoney C Programming Language 4 01-Jul-2006 22:49
[Tutorial] GUI programming with FLTK dsmith FLTK Forum 10 03-Oct-2005 15:41
problem on typedef fwongmc C Programming Language 10 24-Dec-2004 03:54
typedef struct problem grizli C Programming Language 8 19-Jun-2004 16:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:22.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.