Thread: pointer to struct in function

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    54

    pointer to struct in function

    Im trying to learn how to access a structre through using a pointer passed from one function to another ..

    Code:
    #include <stdio.h>
    void proof( struct foo bar  *p){
      printf("%i\n",p->a);
    }
    int main( int argc, char * argv[] ){
      struct foo{
        int a;
      }bar;
      struct foo *p;
      p = &bar;
      p->a = 10;
      proof(&p);
      return (0);
    }
    however I get this error when I try and compile .. error: expected ‘;’, ‘,’ or ‘)’ before ‘*’ token
    any help appreciated thanks al.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    It's not
    Code:
    struct foo bar *p
    it's
    Code:
    struct foo *p
    This
    Code:
    struct foo{
        int a;
      }bar;
    creates an instance of struct foo called bar.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    54
    if I try
    Code:
    #include <stdio.h>
    void proof( struct foo *p){
    
      printf("%i\n",p->a);
    }
    int main( int argc, char * argv[] ){
    
      struct foo{
        int a;
      }bar;
    
      struct foo *p;
      p = &bar;
    
      p->a = 10;
    
      proof(&p);
      return (0);
    }
    I get multiple warnings
    ‘struct foo’ declared inside parameter list its scope is only this definition or declaration, which is probably not what you want

    In function ‘proof’: error: dereferencing pointer to incomplete type
    In function ‘main’: passing argument 1 of ‘proof’ from incompatible pointer type
    Last edited by mad_muppet; 06-13-2010 at 06:16 PM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    A compiler starts at the top of the file and goes to the bottom.

    Where you define your proof function, how does the compiler know about struct foo, when you've declared and defined it within main?

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    45
    if you copied this from a textbook, naughty textbook.
    if you wrote this yourself, naughty you

    using foo, bar, p as variable names when you are trying to learn something just adds a layer of complexity that doesnt need to be there. ive rewritten the example so that it makes sense.

    i changed 2 things. firstly where you define the struct. now, when i was taught about structures, we would always define them before main. so ive made that change. if you define a structure inside of main, then the structure definition would be local to the main function (that is, no one else but main can see the struct) which is why you get an error on your proof function. proof doesnt know what a 'struct foo' is. you have to define structures before you can write functions which use them.

    the second thing is your line 'proof(&p)'. what this does is it passes the address of the variable p and gives it to the function. what we actually want is the address of bar, which is already stored in p. so the correct line is just 'proof(p);' without ampersand.

    here is the code with my changes.

    Code:
    #include <stdio.h>
    
    struct animal {
       char *name;
       int  legs;
    };
    
    void goToTheVet(struct animal* sickPet) {
       printf("hello %s. your %i legs are all healthy\n",
       sickPet->name, sickPet->legs);
    }
    
    int main(int argc, char* argv[]){
    
       struct animal myCat;
    
       struct animal *pointerToAnimal;
       pointerToAnimal = &myCat;
    
       pointerToAnimal->name = "Mr. Snuggles";
       pointerToAnimal->legs = 4;
    
       goToTheVet(pointerToAnimal);
       // this works too
       // goToTheVet(&myCat);
       return (0);
    }

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    54
    thanks for the help .. very much appreciated ..

    my daughter has used my c programming book as a coloring in book and my wife has disappered it while I was at work ..

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    My wife dun run off... *DUN DUN* ...while I was at work. *DUN DUN*
    My kid used my C book... *DUN DUN* ...as a colorin' book. *DUN DUN*
    I hate my job... *DUN DUN* ...and my boss is a jerk. *DUN DUN*
    When she run off... *DUN DUN* ...my dog she dun took. *DUN DUN*
    I got the workin-too-hard-C-programmin'-no-wife-havin'-bluuuuesssss. *DUN DUN*


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 08:38 PM
  2. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  3. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM