By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

derefrencing pointer to incomplete type

P: n/a
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[i][j]=NULL;
}
//for(i=0;i<MAX_VTX;i++)
//{
// for(j=0;j<MAX_VTX;j++)
//g.adjmtx[i][j]=(struct _graph_edge*)malloc(sizeof(struct
_graph_edge));
//
g.adjmtx[i][j]=(graph_edge_pointer)malloc(sizeof(graph_edge));
//}
for(i=0;i<MAX_VTX;i++)
{
gvt = &g.vertices[i];

gvt=(graph_vertex_pointer)malloc(sizeof(graph_vert ex));
}
for(i=0;i<cnt;i++)
{
g.adjmtx[src[i]][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[i].exectime);
printf("Enter label for Vertex %d\n",i);
scanf("%s",&g.vertices[i].label);
}
for(i=0;i<cnt;i++)
{
printf("Enter Produced for Edge %d\n",i);
scanf("%d", &g.adjmtx[src[i]][snk[j]]->produced);
printf("Enter Consumed for Edge %d\n",i);
scanf("%d", &g.adjmtx[src[i]][snk[j]]->produced);
printf("Enter Delay for Edge %d\n",i);
scanf("%d", &g.adjmtx[src[i]][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

Sep 30 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Note:
- This has also been posted to gnu.gcc.help, message
<11*********************@e3g2000cwe.googlegroups.c om>
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 wrote:
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.

<snip>
//g.adjmtx[i][j]=(struct _graph_edge*)malloc(sizeof(struct
_graph_edge));
As illustrated by the above: // comments are bad for posting
code in usenet.
g.adjmtx[i][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.hthen 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.
<snip>
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 30 '06 #2

P: n/a
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 Mair wrote:
Note:
- This has also been posted to gnu.gcc.help, message
<11*********************@e3g2000cwe.googlegroups.c om>
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 wrote:
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.

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

As illustrated by the above: // comments are bad for posting
code in usenet.
g.adjmtx[i][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.hthen 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.
<snip>
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 30 '06 #3

P: n/a
friend.05 wrote:
#ifndef _floatmath_h
#define _floatmath_h
Michael Mair wrote:
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.
I like this convention:

#ifndef H_FLOATMATH_H
#define H_FLOATMATH_H

--
pete
Oct 1 '06 #4

P: n/a

friend.05 wrote:
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. ;-(
return 0;
Oct 1 '06 #5

P: n/a
pete wrote:
friend.05 wrote:
>Michael Mair wrote:
>>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.

#ifndef _floatmath_h
#define _floatmath_h

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)
Oct 1 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.