derefrencing pointer to incomplete type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • friend.05

    derefrencing pointer to incomplete type

    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_poin ter adjmtx[MAX_VTX][MAX_VTX];



    };


    graph_pointer graph_construct (int *src, int *snk, int cnt)
    {
    graph_type g;
    graph_pointer gpt;
    graph_vertex_po inter 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_V TX;i++)
    //{
    // for(j=0;j<MAX_V TX;j++)
    //g.adjmtx[i][j]=(struct _graph_edge*)ma lloc(sizeof(str uct
    _graph_edge));
    //
    g.adjmtx[i][j]=(graph_edge_po inter)malloc(si zeof(graph_edge ));


    //}


    for(i=0;i<MAX_V TX;i++)
    {
    gvt = &g.vertices[i];

    gvt=(graph_vert ex_pointer)mall oc(sizeof(graph _vertex));
    }
    for(i=0;i<cnt;i ++)
    {


    g.adjmtx[src[i]][snk[j]]=(graph_edge_po inter)malloc(si zeof(graph_edge ));

    }


    for(i=0;i<MAX_V TX;i++)
    {
    printf("Enter execution time for Vertex %d\n",i);
    scanf("%d",&g.v ertices[i].exectime);
    printf("Enter label for Vertex %d\n",i);
    scanf("%s",&g.v ertices[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_vt x,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_p ointer;


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


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


    /* 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_poin ter *graph_edge_arr ay;


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


    #endif

  • Michael Mair

    #2
    Re: derefrencing pointer to incomplete type

    Note:
    - This has also been posted to gnu.gcc.help, message
    <1159642467.628 856.89800@e3g20 00cwe.googlegro ups.com>
    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*)ma lloc(sizeof(str uct
    _graph_edge));
    As illustrated by the above: // comments are bad for posting
    code in usenet.
    g.adjmtx[i][j]=(graph_edge_po inter)malloc(si zeof(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_vt x,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_p ointer;
    >
    /* An edge in a graph. */
    typedef struct _graph_edge graph_edge;
    >
    /* A pointer to a graph edge. */
    typedef graph_edge *graph_edge_poi nter;
    >
    /* 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_poin ter *graph_edge_arr ay;
    >
    graph_pointer graph_construct (int *, int *, int);
    >
    #endif
    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.

    Comment

    • friend.05

      #3
      Re: derefrencing pointer to incomplete type

      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_vect or floatmath_vecto r;

      /* Return a new vector with a given length, and a given sequence
      (array) of
      element values.
      */
      floatmath_vecto r *floatmath_vect orNew(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_vecto rElement(floatm ath_vector *, int);

      /* Given a vector, return the length (number of elements in) the
      vector.
      */
      int floatmath_vecto rLength(floatma th_vector *);

      /* Return the remainder when dividing the first number by the second.
      */
      double floatmodulus(do uble, 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
      <1159642467.628 856.89800@e3g20 00cwe.googlegro ups.com>
      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*)ma lloc(sizeof(str uct
      _graph_edge));
      >
      As illustrated by the above: // comments are bad for posting
      code in usenet.
      >
      g.adjmtx[i][j]=(graph_edge_po inter)malloc(si zeof(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_vt x,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_p ointer;

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

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

      /* 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_poin ter *graph_edge_arr ay;

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

      #endif
      >
      Cheers
      Michael
      --
      E-Mail: Mine is an /at/ gmx /dot/ de address.

      Comment

      • pete

        #4
        Re: derefrencing pointer to incomplete type

        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

        Comment

        • Cong Wang

          #5
          Re: derefrencing pointer to incomplete type


          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_po inter 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;
          >

          Comment

          • CBFalconer

            #6
            Re: derefrencing pointer to incomplete type

            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)


            Comment

            Working...