Thread: pointers to structures (2)

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    hello, I'm new to c/c++ programming, I've read through a lot of beginners tutorials and understood quite a few things. I'm currently trying to write a program that modifies a struct by using a function that has pointers to the struct as arguments. Unfortunately it's not working. I think something's wrong with my function prototype. Can u please take a look and help me out. Thanks
    Code:
    #include <stdio.h>
    
    void full (struct Alpha*); 
    
    int main()
    {
        struct Alpha {
            char id;
            int age;    
        };
        Alpha toni;
        full(&toni);
        
        printf("id=%c age=%d",toni.id,toni.age);
        
        return 0;
    }
        
    void full (struct Alpha *p){
        p->id='x';
        p->age=19;          
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    There is something wrong with your function prototype. It's declared with an arguement of a type that doesn't exist, yet. Declare your struct above your function prototype, not in main. ..and before you ask, your next problem is because you don't have the keyword struct preceding "Alpha toni"
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    thanks

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Also:
    Code:
    Alpha toni;
    You can't do that in C without a typedef. You need to use:
    Code:
    struct Alpha toni;
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding linked lists, structures, and pointers
    By yougene in forum C Programming
    Replies: 5
    Last Post: 07-13-2011, 08:13 PM
  2. Copying pointers in structures instead of the structure data?
    By Sparrowhawk in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 06:04 PM
  3. returning pointers to structures
    By Giant in forum C++ Programming
    Replies: 2
    Last Post: 06-20-2005, 08:40 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Help with pointers and members of structures
    By klawton in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 12:34 PM