Passing a pointer to a function ... how?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • miner
    New Member
    • May 2010
    • 2

    Passing a pointer to a function ... how?

    Hi all

    I'm doing my first steps in C and I need some help plz.

    I have 2 structs, a node* and a stack*. I want to write a push() function that accepts to arguments, the node to insert and the pointer to the the top of the stack.

    This way it works without problems: push(node *theNode, stack *theStack)
    The call looks like that: push(newNode, newStack) and inside the function I can access the pointer with newStack->head.

    I tried doing it this way: push(node *theNode, node *topNode) and call it like that: push(newNode, newStack->head) but it doesn't work(also tried using brackets etc).

    What am I doing wrong?
    Hope my question is clear enough.

    Thank you in advance for your help

    Minas
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    What do you mean by "it doesn't work"? Also, please post the code, using [code] tags.

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      Guess:
      You're not updating the newStack -> head. You will have pointed the new node to the old head. Unfortunately, the only one who knows about it is the new head node!
      The stack doesn't keep track of who else is pointing to its head node.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Also, a stack does not mean inserting nodes at the beginning of the list. What it means is that the last node added is the next one retreived.

        Therefore, you can add to the end of the list (push) and remove from the end of the list (pop) and still have a stack. Some desingers have a stack struct that has both the address of the first and last node of the stack.

        Comment

        • miner
          New Member
          • May 2010
          • 2

          #5
          Thank you for your replies.

          I have to be more specific. Here's is a code sample :

          Code:
          #include <stdio.h>
          #include <malloc.h>
          
          typedef struct node { 
          	int val;
          	struct node *next;
          } NODE;
          
          typedef struct { 
          	NODE *head;
          	int size; 
          } STACK;
          
          void push(node *kombos, STACK *top); // (1)
          
          int main(){
          
          STACK *newStack;
          newStack=(STACK *) malloc(10*sizeof(STACK));
          newStack->head=NULL;
          newStack->size=0;
          
          for(int i=0; i<10; i++){
          	NODE *newNode;
          	newNode=(NODE *) malloc(sizeof(NODE));
          	newNode->val=i;
          	newNode->next=NULL;
          	push(newNode, newStack);  //(2)
          	newStack->size+=1;
          }
          return 0;
          }
          void push(node *kombos, STACK *top){ // (3)
          if (kombos==NULL)
          	printf("error");
          else{
          	kombos->next=top->head;
          	top->head=kombos;
          	}
          }
          This creates a stack structure (the pointer and also an int with the number of nodes). I tried to do the same thing this way:

          Code:
          void push(node *kombos, node *top); //this goes to line 14
          
          push(newNode, newStack->head); //line 28
          
          void push(node *kombos, node *top){ // line 33
          if (kombos==NULL)
          	printf("error");
          else{
          	kombos->next=top;
          	top=kombos;
          	}
          }
          I thought that push() is expecting for 2 nodes, I'm passing 2 nodes ... so everything would be OK. But I'm not getting the same result.
          Is there a way to update the newStack->head pointer?

          Thank you very much for your time
          Minas

          Comment

          • hemaninfo
            New Member
            • May 2010
            • 1

            #6
            Passing a pointer to a function ... how?

            if you are passing a pointer as an argument to a function, like as shown below:

            int* iptr = new int[];
            PassPtrToFuncAs Arg(iptr);

            then the declaration for the function PassPtrToFuncAs Arg() would look something like this:

            return-type PassPtrToFuncAs Arg(int** iptrAsArg);

            when you are passing pointer means there should be 'a pointer to a pointer' on receiving end (i.e., here PassPtrToFuncAs Arg())

            Comment

            Working...