473,468 Members | 1,358 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Passing a pointer to a function ... how?

2 New Member
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
May 7 '10 #1
5 1822
Markus
6,050 Recognized Expert Expert
What do you mean by "it doesn't work"? Also, please post the code, using [code] tags.
May 7 '10 #2
jkmyoung
2,057 Recognized Expert Top Contributor
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.
May 7 '10 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
May 7 '10 #4
miner
2 New Member
Thank you for your replies.

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

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <malloc.h>
  3.  
  4. typedef struct node { 
  5.     int val;
  6.     struct node *next;
  7. } NODE;
  8.  
  9. typedef struct { 
  10.     NODE *head;
  11.     int size; 
  12. } STACK;
  13.  
  14. void push(node *kombos, STACK *top); // (1)
  15.  
  16. int main(){
  17.  
  18. STACK *newStack;
  19. newStack=(STACK *) malloc(10*sizeof(STACK));
  20. newStack->head=NULL;
  21. newStack->size=0;
  22.  
  23. for(int i=0; i<10; i++){
  24.     NODE *newNode;
  25.     newNode=(NODE *) malloc(sizeof(NODE));
  26.     newNode->val=i;
  27.     newNode->next=NULL;
  28.     push(newNode, newStack);  //(2)
  29.     newStack->size+=1;
  30. }
  31. return 0;
  32. }
  33. void push(node *kombos, STACK *top){ // (3)
  34. if (kombos==NULL)
  35.     printf("error");
  36. else{
  37.     kombos->next=top->head;
  38.     top->head=kombos;
  39.     }
  40. }
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:

Expand|Select|Wrap|Line Numbers
  1. void push(node *kombos, node *top); //this goes to line 14
  2.  
  3. push(newNode, newStack->head); //line 28
  4.  
  5. void push(node *kombos, node *top){ // line 33
  6. if (kombos==NULL)
  7.     printf("error");
  8. else{
  9.     kombos->next=top;
  10.     top=kombos;
  11.     }
  12. }
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
May 8 '10 #5
hemaninfo
1 New Member
if you are passing a pointer as an argument to a function, like as shown below:

int* iptr = new int[];
PassPtrToFuncAsArg(iptr);

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

return-type PassPtrToFuncAsArg(int** iptrAsArg);

when you are passing pointer means there should be 'a pointer to a pointer' on receiving end (i.e., here PassPtrToFuncAsArg())
May 8 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: keepyourstupidspam | last post by:
Hi, I want to pass a function pointer that is a class member. This is the fn I want to pass the function pointer into: int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
7
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving...
9
by: shaun | last post by:
Dear all, I realized an error in a previous post, I reproduce it here because I'm still not sure how to solve it: I want to make a templated function which points to one-past-the-end of a...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
6
by: Roman Mashak | last post by:
Hello, I belive the reason of problem is simple, but can't figure out. This is piece of code: struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ };
5
by: jewel87 | last post by:
Hello, I have a question about pointers. Could anyone please explain to me the following: I have a dynamically allocated array of structures like: struct Employee{......}; Employee...
18
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to...
1
by: autumn | last post by:
Hi everybody, I'm having problem passing pointer to member object as template argument, seems VC 2005 does not allow 'pointer to base member' to 'pointer to derived member' conversion in template...
7
by: lovecreatesbea... | last post by:
Shoud we declare non-pointer function parameters with const keywords? int main(void){ int f(const int i); int i; f(i); return 0; }
20
by: jason | last post by:
Hello, I'm a beginning C programmer and I have a question regarding arrays and finding the number of entries present within an array. If I pass an array of structures to a function, then...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.