Thread: pointers in structures

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    18

    pointers in structures

    i was reading a program about structures and pointers but am unable to understand what is actually happening, can someone please help me out that what processes are happening.
    thanks
    Code:
    #include<stdio.h>
    #define SIZE 5
    
    struct stack
    {
    	int term[SIZE];
    	int top;
    };
    
    void initstack( struct stack*);
    
    int main()
    {
    	struct stack st;
    	initstack(&st );
    }
    
    void initstack(struct stack*st)          // having trouble understanding this part.
    {
    	st->top = 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by esi View Post
    i was reading a program about structures and pointers but am unable to understand what is actually happening, can someone please help me out that what processes are happening.
    thanks
    Code:
    #include<stdio.h>
    #define SIZE 5
    
    struct stack
    {
    	int term[SIZE];
    	int top;
    };
    
    void initstack( struct stack*);
    
    int main()
    {
    	struct stack st;
    	initstack(&st );
    }
    
    void initstack(struct stack*st)          // having trouble understanding this part.
    {
    	st->top = 0;
    }
    The function initstack is called with the parameter of the address to the struck st base. In C, all parameters are passed by copying the original - in the case of this struct, that copy of the address, points right to the struct just like it was the original address. Now in the initstack function, that is address is changed into the address of a pinter by the alias *st, which allows you to access the structure, even though it was created locally inside main().

    (It was defined outside main(), but created in main() ).

    The st->top = 0 is a handy way of accessing a struct with a pointer. St is the name of the struct, of course and *st points to it's base memory address; -> refers to the fact that it's a pointer doing the accessing, and top refers to the int part of the st struct.

    Hope that helps.

    Adak

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    OK, starting inside main() you have only 2 lines.

    Code:
    struct stack st;
    initstack(&st );
    The first one allocates space for a variable of type struct stack. The second one passes the address of your variable to the function initstack().

    All you're doing is passing an address to a function. Here's the function definition:

    Code:
    void initstack(struct stack*st)          // having trouble understanding this part.
    {
    	st->top = 0;
    }
    All this does is take a pointer to a variable of type struct stack. A pointer just contains the address of a variable.

    Therefore, the lone statement inside the function actually alters the struct that was passed to it because its address was passed. The statement above is equivalent to the following:

    Code:
    (*st).top = 0
    Because st inside this function is a pointer, you need to derefence it.

    Forgetting structs for a moment, the pointer aspect of it is just like this:

    Code:
    void someFunction(int *);
    
    int main(void)
    {
    	int x;
    	someFunction(&x);
    
    	return 0;
    }
    
    void someFunction(int *p)
    {
    	*p = 0;
    }
    If you realize the pointer relationship with regular variables, then it's just a matter of applying the concept over to structs.

    Edit: Yes.... I'm getting slow in my old age.

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