weird typedef struct problems

This is a discussion on weird typedef struct problems within the C Programming forums, part of the General Programming Boards category; Hi! This is my first post on this board, so I would first like to say hello to everybody here! ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    weird typedef struct problems

    Hi!

    This is my first post on this board, so I would first like to say hello to everybody here!

    I want to program a directed graph datastructure. OK, so here are my two types: edges and nodes:

    Code:
    struct node_struct
    {
    	unsigned int n_e;	// number of connected edges
    	struct edge_struct **e;	// array of connected edges (array of pointers to edges)
    };
    typedef struct node_struct node;
    
    struct edge_struct
    {
    	float c;		// capacity
    	float f;		// flow
    	node * u;	// start
    	node * v;	// end
    };
    typedef struct edge_struct edge;
    Further, I have these functions for memory allocation on the heap.
    Code:
    void alloc_node
    (
     node ** u,
     unsigned int n
    )
    {
    	*u = ((node)*) malloc(sizeof(node));
    	if(*u == NULL)
    	{
    		printf("alloc_node: (1) Not enough storage available.\n");
    		exit(1);
    	}
    	(*u)->n_e = n;
    	(*u)->e = (edge**) malloc(n * sizeof(edge*));
    	if ((*u)->e == NULL)
    	{
    		printf("alloc_node: (2) Not enough storage available.\n");
    		exit(1);
    	}
    }
    
    
    void alloc_edge
    (
     edge** e
    )
    {
    	*e = ((edge)*) malloc(sizeof(edge));
    	if(*e == NULL)
    	{
    		printf("alloc_edge : Not enough memory available.\n");
    		exit(1);
    	}
    	(*e)->c = 0.0f;
    	(*e)->f = 0.0f;
    	(*e)->u = 0;
    	(*e)->v = 0;
    }
    Well, ok, this compiles fine (under MS VS 2003 Pro), with this main function:
    Code:
    int main()
    {
    	node * v1;
    	//alloc_node(&v1,3);
    	edge * a;
    }
    But if I uncomment the second line, then the compiler complains with

    f:\src\main.c(18): error C2275: 'edge' : illegal use of this type as an expression

    where line 18 eaxtly points to the line holding "edge * a;", so not the call to alloc_node.
    But as I said, if I comment the call to the alloc_node, everything compiles fine.

    I don't know where my error lies.

    Thanks a lot in advance!

    Best regards,
    Oliver

  2. #2
    Registered User
    Join Date
    Jul 2008
    Posts
    2
    Baaaaah,

    I just found out that this problem just occurs under the Microsoft Compiler. Using gcc, it compiles fine.

    Any idea, where the problem is for the Microsoft compiler?

    Thanks,
    Oliver

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    struct node_struct
    {
        unsigned int n_e;    // number of connected edges
        struct edge_struct **e; // error: struct edge_struct is not declared at this point
    };
    typedef struct node_struct node;
    
    struct edge_struct
    {
        float c;        // capacity
        float f;        // flow
        node * u;    // start
        node * v;    // end
    };
    typedef struct edge_struct edge;
    int main()
    {
        node * v1;
        //alloc_node(&v1,3); // v1 is not initialized
        edge * a;
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    In C89, once you do an actual bit of code (like calling a function), you can not declare any more variables (so declaring edge * a would be a syntax error). That restriction was relaxed in C99, which gcc follows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. typedef struct
    By Bleech in forum C Programming
    Replies: 4
    Last Post: 12-30-2006, 06:18 PM
  3. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 09:24 AM
  4. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 04:27 AM
  5. typedef struct vs. struct
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 01-26-2002, 11:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21