Passing a dynamic array of struct to a function, the returning the same array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • trycstruct
    New Member
    • Mar 2010
    • 4

    Passing a dynamic array of struct to a function, the returning the same array

    In the 'calling()' function I declare the following pointer:
    Code:
    struct tags {
    char aname[100];
    int aval;
    .
    .
    .
    }*tgs;
    I then call the 'called()' function like such:
    Code:
    tgs = struct tags get_id_tags(buf, tgs);
    The 'called()' function looks like this:
    Code:
    struct tags *get_id_tags(char *bu, struct tags *tg) {
    /* statements here*/
    return tg;
    }
    When I try to compile, I get the following warnings:
    "warning: passing argument 2 of 'get_id_tags' from incompatible pointer type"
    "warning: assignment from incompatible pointer type"
    both regard the function call.
    I have tried with various level of indirection, but unsuccessfully.
    I have used such structures before, but either as globals, or processed in the same function.
    I simply cannot understand what I,m doing wrong, and would be grateful if
    some good soul can help me out.
    I'm not a student (unfortunately) , i'm 65.
    Thank anybody for the attention.
    Regards Sergio (trycstruct).
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    For starters this
    Code:
    tgs = struct tags get_id_tags(buf, tgs);
    should be this
    Code:
    tgs = get_id_tags(buf, tgs);
    That is the only error I see in what is posted, I assume you pre-declare get_id_tags before trying to call it? A predeclaration looks like this
    Code:
    struct tags *get_id_tags(char *bu, struct tags *tg);
    It declares the functions return type and paramater types to allow the compiler to do type checking.

    You don't have to be a student to post here and anyway surely you can still be a student at 65 if you are still learning anything ;)

    Comment

    • trycstruct
      New Member
      • Mar 2010
      • 4

      #3
      Thank for replying banfa.
      I had made a mistake while copying & pasting.
      The struct in the program is the following:
      Code:
        struct tags {
          char tg[TG_NAM];
          char id[ID_LEN];
          char class[ID_LEN];
          char col[COL_NAM];
          int op;
          int cl;
          int siz;
        }*alltgs;
      The predeclaration looks like this:
      Code:
      struct tags *get_id_tags(char *tbuf, struct tags *stt);
      The actual call looks like this:
      Code:
      alltgs = get_id_tags(hbuf, alltgs);
      And the (not yet written) function looks like this:
      Code:
      struct tags *get_id_tags(char *tbuf, struct tags *stt) {
      
        return stt;
      }
      But the compiler complains about, and I have tried all sort of combination of '*' and '&'.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        OK well I took that code and produced

        Code:
        #define TG_NAM 20
        #define COL_NAM 20
        #define ID_LEN 20
        
        struct tags {
            char tg[TG_NAM];
            char id[ID_LEN];
            char class[ID_LEN];
            char col[COL_NAM];
            int op;
            int cl;
            int siz;
        }*alltgs;
         
        struct tags *get_id_tags(char *tbuf, struct tags *stt);
         
        int main()
        {
        	char hbuf[50];
        	alltgs = get_id_tags(hbuf, alltgs);
        	return 0;
        }
        
        struct tags *get_id_tags(char *tbuf, struct tags *stt) {
         
          return stt;
        }
        In which I just add a few #defines for array sizes and put a main round your function call and this compiles without errors.

        You haven't accidentally declared a variable name tgs in the code calling your function, this would hide the global declaration of tgs.

        Also use of the word class as a member name in your structure will prove troublesome if you ever try to convert this project to C++.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          You should have posted this not put it in a PM to me
          Originally posted by trycstruct
          I have written a small, (do nothing) program that yields exactly the same compiler warning messages that my real program yields.
          So the question this time would be:
          How can I get this (do nothing) program to compile without warnings, and is it possible at all?
          If I declare the 'struct tags' in the program's global area, the program compiles cleanly; I know, I have done that a number of times before, I just wanted to know why I cannot do it this way.
          So if you got to this point, following is the program:
          Code:
          /* test-stru.c */
           
          #include <stdlib.h>             /* For 'NULL' (below) */
           
           
          void func1(char *fil);
          struct tags *get_id_tags(char *tbuf, struct tags *stt);
           
          /* ************************************************************************ */
          int main(int argc, char **argv) {
            char buff[256] = {0};
           
            /* Lots of code here; i.e. to copy argv[??] int 'buff', etc.. */
            func1(buff);
           
            return 0;
          }
           
          /* ************************************************************************ */
          void func1(char *fil) {
            char *hbuf = NULL;
            struct tags {
               char tg[32];
               int siz;
           }*alltgs;
           
            /* malloc 'hbuf' to the size of 'fil', then read in 'fil' */
            /* Call 'get_id_tags() to fill in 'alltgs' */
           
            alltgs = get_id_tags(hbuf, alltgs);
           
            /* Free everything  */
          }
           
          /* ************************************************************************ */
           struct tags *get_id_tags(char *tbuf, struct tags *stt) {
           
             /* Scan 'tbuf' and fill in 'stt' */
           
             return stt;
           }
          And again, following are the warning messages:
          Code:
          test-stru.c: In function 'func1':
          test-stru.c:30: warning: passing argument 2 of 'get_id_tags' from incompatible pointer type
          test-stru.c:30: warning: assignment from incompatible pointer type
          The compiler I use is the GNU 'gcc'.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            However creating a small do nothing program that produced the same behaviour is probably the best thing you could have done as far as demonstrating the problem and it is in fact now clear what the problem is.

            At line 7 of your code listing you try to use the type struct tags. However it is not declared until line 22.

            Line 22 - 25 defines a structure and declares a variable. You need to split this up because you need to define the structure at the head of the file before any references to it, around about line 4 - 5. However you need to keep the variable declaration at line 22.

            Split this at line 22

            Code:
            struct tags {
                char tg[32];
                int siz;
            }*alltgs;
            Into this at line 5
            Code:
            struct tags {
                char tg[32];
                int siz;
            };
            and this at line 22
            Code:
            struct tags* alltgs;

            Comment

            • trycstruct
              New Member
              • Mar 2010
              • 4

              #7
              Just to say thank you. This problem is solved.

              Thank you for your help, it was very appreciated.
              trycstruct

              Comment

              Working...