derefrencing pointer to incomplete type

F

friend.05

1)

#include <stdio.h>
#include <stdlib.h>
#include "graph.h"

#define MAX_VTX 4


struct _graph_vertex {


char *label;
int exectime;



};


struct _graph_edge {

int produced, consumed, delay;



};


struct _graph {

graph_vertex vertices[MAX_VTX];
graph_edge_pointer adjmtx[MAX_VTX][MAX_VTX];



};


graph_pointer graph_construct(int *src, int *snk, int cnt)
{
graph_type g;
graph_pointer gpt;
graph_vertex_pointer gvt;
int i,j;

for(i=0 ; i < MAX_VTX ; i++)
{
for(j=0 ; j < MAX_VTX; j++)
g.adjmtx[j]=NULL;
}


//for(i=0;i<MAX_VTX;i++)
//{
// for(j=0;j<MAX_VTX;j++)
//g.adjmtx[j]=(struct _graph_edge*)malloc(sizeof(struct
_graph_edge));
//
g.adjmtx[j]=(graph_edge_pointer)malloc(sizeof(graph_edge));


//}


for(i=0;i<MAX_VTX;i++)
{
gvt = &g.vertices;

gvt=(graph_vertex_pointer)malloc(sizeof(graph_vertex));
}
for(i=0;i<cnt;i++)
{


g.adjmtx[src][snk[j]]=(graph_edge_pointer)malloc(sizeof(graph_edge));

}


for(i=0;i<MAX_VTX;i++)
{
printf("Enter execution time for Vertex %d\n",i);
scanf("%d",&g.vertices.exectime);
printf("Enter label for Vertex %d\n",i);
scanf("%s",&g.vertices.label);
}


for(i=0;i<cnt;i++)
{
printf("Enter Produced for Edge %d\n",i);
scanf("%d", &g.adjmtx[src][snk[j]]->produced);
printf("Enter Consumed for Edge %d\n",i);
scanf("%d", &g.adjmtx[src][snk[j]]->produced);
printf("Enter Delay for Edge %d\n",i);
scanf("%d", &g.adjmtx[src][snk[j]]->produced);


}


gpt = &g;
printf("%d",gpt->vertices[1].exectime);
return gpt;



}


2)

#include <stdio.h>
#include <stdlib.h>
#include "graph.h"

int main()
{


int src_vtx[]= {0,1,3,0,3};
int snk_vtx[] = {1,2,2,3,1};
int edgcnt = sizeof(src_vtx)/sizeof(int);
graph_pointer gp;


gp = graph_construct(src_vtx,snk_vtx,edgcnt);


printf("%d",gp->vertices[1].exectime);


I am getting error over here:


gcc : derefrencing pointer to incomplete type
vc++ : left of 'vertices' specifies undefined struct/union '_graph'


return 0;




3) graph.h


#ifndef _graph_h
#define _graph_h


#include "floatmath.h"


/* A vertex in a graph. */
typedef struct _graph_vertex graph_vertex;


/* A pointer to a graph vertex. */
typedef graph_vertex *graph_vertex_pointer;


/* An edge in a graph. */
typedef struct _graph_edge graph_edge;


/* A pointer to a graph edge. */
typedef graph_edge *graph_edge_pointer;


/* A graph. */
typedef struct _graph graph_type;


/* A pointer to a graph. */
typedef graph_type *graph_pointer;


/* An array of pointers to graph edges. */
typedef graph_edge_pointer *graph_edge_array;


graph_pointer graph_construct(int *, int *, int);


#endif
 
M

Michael Mair

Note:
- This has also been posted to gnu.gcc.help, message
<[email protected]>
Please indicate this fact in all groups you post to, so you do not
make people tell you the same things independently.
- Your code contains much vertical spacing; I threw it out without
explicit notes.

friend.05 said:
1)

#include <stdio.h>
#include <stdlib.h>
#include "graph.h"

#define MAX_VTX 4

struct _graph_vertex {

Leading underscores do not "belong" to the user in many cases.
It is a good idea not to use them if you are not sure in which
cases they belong to you and in which they do not.

//g.adjmtx[j]=(struct _graph_edge*)malloc(sizeof(struct
_graph_edge));


As illustrated by the above: // comments are bad for posting
code in usenet.
g.adjmtx[j]=(graph_edge_pointer)malloc(sizeof(graph_edge));


- This statement is outside of a loop.
- It is unnecessary to cast the return value of malloc(); this
can hide an error. If your compiler complains if you take away
the cast _and_ have included <stdlib.h> then you are compiling
C code with a C++ compiler (or in the compiler's C++ mode) --
bad idea.
- the favoured form of allocation around here is
T *p;
...
p = malloc(sizeof *p);
If the type of p changes, then nothing goes wrong.
- you forgot to check for malloc() success or failure.
2)

#include <stdio.h>
#include <stdlib.h>
#include "graph.h"

int main()
{


int src_vtx[]= {0,1,3,0,3};
int snk_vtx[] = {1,2,2,3,1};
int edgcnt = sizeof(src_vtx)/sizeof(int);
graph_pointer gp;


gp = graph_construct(src_vtx,snk_vtx,edgcnt);


printf("%d",gp->vertices[1].exectime);


I am getting error over here:


gcc : derefrencing pointer to incomplete type

You did not give the structure definition in "graph.h" but
only said "I mean 'struct _graph *' whenever I say 'graph_pointer'".
The compiler only knows that there is a type "struct _graph" but
not what it looks like.
Either do not try to access the interna of your graphs from outside
the graph "library" or move the struct definitions to "graph.h".
vc++ : left of 'vertices' specifies undefined struct/union '_graph'

return 0;

You do not post your full programme -- this code will not compile.
3) graph.h


#ifndef _graph_h
#define _graph_h


#include "floatmath.h"

As you did not provide floatmath.h (or at least neglected to point
out which of "1)" and "2)" is supposed to be that), I cannot
compile your code -- you make it unnecessarily hard to help you.
/* A vertex in a graph. */
typedef struct _graph_vertex graph_vertex;

/* A pointer to a graph vertex. */
typedef graph_vertex *graph_vertex_pointer;

/* An edge in a graph. */
typedef struct _graph_edge graph_edge;

/* A pointer to a graph edge. */
typedef graph_edge *graph_edge_pointer;

/* A graph. */
typedef struct _graph graph_type;

/* A pointer to a graph. */
typedef graph_type *graph_pointer;

/* An array of pointers to graph edges. */
typedef graph_edge_pointer *graph_edge_array;

graph_pointer graph_construct(int *, int *, int);

#endif

Cheers
Michael
 
F

friend.05

thanks for ur help:

sorry for trouble

I am giving my floatmath.h:

#ifndef _floatmath_h
#define _floatmath_h

/* A vector of floating point (double-typed) numbers. */
typedef struct _floatmath_vector floatmath_vector;

/* Return a new vector with a given length, and a given sequence
(array) of
element values.
*/
floatmath_vector *floatmath_vectorNew(int, double *);

/* Given an index and a vector, return the element of the vector at
that index.
Vectors are indexed starting at zero.
*/
double floatmath_vectorElement(floatmath_vector *, int);

/* Given a vector, return the length (number of elements in) the
vector.
*/
int floatmath_vectorLength(floatmath_vector *);

/* Return the remainder when dividing the first number by the second.
*/
double floatmodulus(double, double);

/* Using Euclid's algorithm, return the greatest common divisor of two
numbers.
*/
double euclid(double, double);

/* Return the least common multiple of two numbers. */
double lcm(double, double);

/* Given pointers to the numerator and denominator of a fraction,
modify
the numerator and denominator so that the fraction is in reduced form.
*/
void reduce_fraction(double *, double *);

/* Given an array of floating point numbers and the length of the
array,
return the greatest common divisor of all elements in the array.
*/
double vect_gcd(double *, int);

#endif



Michael said:
Note:
- This has also been posted to gnu.gcc.help, message
<[email protected]>
Please indicate this fact in all groups you post to, so you do not
make people tell you the same things independently.
- Your code contains much vertical spacing; I threw it out without
explicit notes.

friend.05 said:
1)

#include <stdio.h>
#include <stdlib.h>
#include "graph.h"

#define MAX_VTX 4

struct _graph_vertex {

Leading underscores do not "belong" to the user in many cases.
It is a good idea not to use them if you are not sure in which
cases they belong to you and in which they do not.

//g.adjmtx[j]=(struct _graph_edge*)malloc(sizeof(struct
_graph_edge));


As illustrated by the above: // comments are bad for posting
code in usenet.
g.adjmtx[j]=(graph_edge_pointer)malloc(sizeof(graph_edge));


- This statement is outside of a loop.
- It is unnecessary to cast the return value of malloc(); this
can hide an error. If your compiler complains if you take away
the cast _and_ have included <stdlib.h> then you are compiling
C code with a C++ compiler (or in the compiler's C++ mode) --
bad idea.
- the favoured form of allocation around here is
T *p;
...
p = malloc(sizeof *p);
If the type of p changes, then nothing goes wrong.
- you forgot to check for malloc() success or failure.
2)

#include <stdio.h>
#include <stdlib.h>
#include "graph.h"

int main()
{


int src_vtx[]= {0,1,3,0,3};
int snk_vtx[] = {1,2,2,3,1};
int edgcnt = sizeof(src_vtx)/sizeof(int);
graph_pointer gp;


gp = graph_construct(src_vtx,snk_vtx,edgcnt);


printf("%d",gp->vertices[1].exectime);


I am getting error over here:


gcc : derefrencing pointer to incomplete type

You did not give the structure definition in "graph.h" but
only said "I mean 'struct _graph *' whenever I say 'graph_pointer'".
The compiler only knows that there is a type "struct _graph" but
not what it looks like.
Either do not try to access the interna of your graphs from outside
the graph "library" or move the struct definitions to "graph.h".
vc++ : left of 'vertices' specifies undefined struct/union '_graph'

return 0;

You do not post your full programme -- this code will not compile.
3) graph.h


#ifndef _graph_h
#define _graph_h


#include "floatmath.h"

As you did not provide floatmath.h (or at least neglected to point
out which of "1)" and "2)" is supposed to be that), I cannot
compile your code -- you make it unnecessarily hard to help you.
/* A vertex in a graph. */
typedef struct _graph_vertex graph_vertex;

/* A pointer to a graph vertex. */
typedef graph_vertex *graph_vertex_pointer;

/* An edge in a graph. */
typedef struct _graph_edge graph_edge;

/* A pointer to a graph edge. */
typedef graph_edge *graph_edge_pointer;

/* A graph. */
typedef struct _graph graph_type;

/* A pointer to a graph. */
typedef graph_type *graph_pointer;

/* An array of pointers to graph edges. */
typedef graph_edge_pointer *graph_edge_array;

graph_pointer graph_construct(int *, int *, int);

#endif

Cheers
Michael
 
C

Cong Wang

friend.05 said:
1)

graph_pointer graph_construct(int *src, int *snk, int cnt)
{
graph_type g;

'g' is defined here.
graph_pointer gpt;
graph_vertex_pointer gvt;
int i,j;
....

gpt = &g;

Pointer 'gpt' points to 'g'. And 'g' is defined on stack, NOT heap.
printf("%d",gpt->vertices[1].exectime);

This is right because the function hasn't terminated.
return gpt;



} ....

printf("%d",gp->vertices[1].exectime);

So this is wrong. ;-(
 
C

CBFalconer

pete said:
I like this convention:

#ifndef H_FLOATMATH_H
#define H_FLOATMATH_H

I do too. The rationale behind it is that the leading H_ avoids
conflict with files whose names begin with e or E, while preservng
the convention of upper case for defines. The reason for avoiding
the E is that such names are reserved for the implementation, and
are actually used to define possible runtime system errors. I
adopted it after that was pointed out to me by someone on this
newsgroup.

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
<http://www.dinkumware.com/refxc.html> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
<http://clc-wiki.net> (C-info)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top