Struct as pointers

This is a discussion on Struct as pointers within the C Programming forums, part of the General Programming Boards category; Greetings. I seek assistance with gaining an understanding of structs in the case when they are passed as pointers to ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    22

    Struct as pointers

    Greetings.

    I seek assistance with gaining an understanding of structs in the case when they are passed as pointers to function. Below are code snippets to illustrate my points of contention:

    Code:
    typedef struct stack_t Stack;
    
    struct stack_t{
      int * data; 
      int top;              //points to top of the *data
    }

    I define & initialize the struct as follows:

    Code:
    Stack stk;
    Stack * S = &stk;
    init(S, 3);
    It is as this point when I initialize the struct that my understanding of the subject leads me to attempt the following method, which results in compiler errors. The definition of init() is as follows:

    Code:
    void init(Stack * S, int N){
       int * stk = (int*)malloc(N*sizeof(int));
       stk = &(S->data);                                   //Causes error
       S->top = -1;
    }

    Would greatly appreciate assistance with addressing the above concern.

    Look forward to a prompt response.

    Best regards,
    wirefree101

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    957
    In the future, please show the actual errors you get; they are much more helpful than simply saying you got an error.

    This case, though, I can eyeball easily. stk is a pointer to int. &S->data is a pointer to pointer to int. These are not the same thing and cannot be used interchangeably. For this case, I see no particular reason why you are even using a temporary variable at all. Why not:
    Code:
    S->data = malloc(N * sizeof *S->data);
    This, I imagine, is what you're trying to do. In your code, I presume you have the assignment turned around; that is, you want to assign stk to S->data, not the other way around.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by wirefree101 View Post
    Greetings.

    I seek assistance with gaining an understanding of structs in the case when they are passed as pointers to function. Below are code snippets to illustrate my points of contention:

    Code:
    typedef struct stack_t Stack;
    
    struct stack_t{
      int * data; 
      int top;                 //points to top of the stack data
    }
    Shouldn't the struct definition be the other way around as top is not a pointer it can't point to the top of *data ie
    Code:
    struct stack_t
    {
      int data; 
      int *top;
    }
    Given the above struct definition, top can now point to the top of the data stack.
    Quote Originally Posted by wirefree101 View Post
    I define & initialize the struct as follows:

    Code:
    Stack stk;
    Stack * S = &stk;
    init(S, 3);
    It is as this point when I initialize the struct that my understanding of the subject leads me to attempt the following method, which results in compiler errors. The definition of init() is as follows:

    Code:
    void init(Stack * S, int N){
       int * stk = (int*)malloc(N*sizeof(int));
       stk = &(S->data);                                   //Causes error
       S->top = -1;
    }
    Once again stuff seems to be switched around, as in
    Code:
       int * stk = (int*)malloc(N*sizeof(int));
       //stk = &(S->data);    // no can't do as cas noted besides you're initializing S not stk
       S->top = stk;
       S->data = -1;

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    Indonesia
    Posts
    68
    Code:
    stk = (S->data);
    stk is pointing to s-> next

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    22
    Appreciate the responses.

    Best regards,
    wirefree101
    Last edited by wirefree101; 11-13-2009 at 07:58 AM.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    22
    Greetings.

    I seek assistance with understanding the usage of pointer to a pointer in cases where struct is employed. I state my point of contention with the help of the following illustrative code:

    Code:
    int main(){
       Node *ptr = NULL;       //pointer to struct Node
       initialize(&ptr);
    }
    
    void initiatize(** ptrN){
       if(*ptrN == NULL)
          ...                  //code to initialize struct elements
          ...
    }
    My query is the following:

    Qs.: What has been achieved that differs from the case where I call initialize(ptr) (not initialize(&ptr)) and initialize() receives (*ptrN) (not (**ptrN))?

    Would greatly appreciate a simply stated & prompt response.

    Best regards,
    wirefree101

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by wirefree101 View Post

    Qs.: What has been achieved that differs from the case where I call initialize(ptr) (not initialize(&ptr)) and initialize() receives (*ptrN) (not (**ptrN))?

    Would greatly appreciate a simply stated & prompt response.

    Best regards,
    wirefree101

    replace the Node* type with int and follow the code
    Code:
    int a = 0;
    initialize(&a);
    
    ...
    
    void initialize(int* b)
    {
       *b = 5;
    }
    what will happen if instead of initialize(int* b) you do initialize(int)
    and instead of &a you pass just a?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Then replace "int" with your real type, eg Node* and watch how it follows the very same rules
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    Registered User
    Join Date
    Sep 2005
    Posts
    22
    Quote Originally Posted by vart View Post
    replace the Node* type with int and follow the code
    Appreciate the responses.

    As suggested, I submit the following code for your comments.

    Synopsis: Two functions perform the addition operation - first receives a pointer to a pointer while the second receives simply a pointer. They both achieve the desired addition operation. The detail is for the sake of completion.


    Code:
    void addI(int **j){
    	printf("%d\n",j);	// pointer's address
    	printf("%d\n",*j);	// i's address
    	printf("%d\n",**j);	// i
    	**j = **j + 1; 			//store at this address the content of this address added to 1
    	printf("---%d\n",*j);	
    }
    
    void addX(int *j){
    	printf("%d\n",j);	//x's address
    	printf("%d\n",*j);   //x
    	*j = *j + 1; 			//store at this address the content of this address added to 1
    }
    
    
    int main(){
    	int i = 5;
    	int * ptrI = &i;
    	printf("%d\n",ptrI);	//address stored by pointer i.e. i's address
    	printf("%d\n",&ptrI);//pointer's own address
    	printf("%d\n",*ptrI);//value at address stored by pointer i.e. i
    	addI(&ptrI);
    	printf("i=%d\n",i);
    	
    	int x = 5;
    	int * ptrX = &x;
    	addX(ptrX);
    	printf("x=%d\n",x);
    	
    	return 0;
    }
    Would appreciate your comments (corrections) against each of my comments in the code. The same would assist my understanding.

    Best regards,
    wirefree101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  4. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 10:50 PM
  5. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 11:24 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21