473,779 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
+ 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 1833
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[];
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())
May 8 '10 #6

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

Similar topics

6
8830
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* pParam)
7
5196
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 this. Ok... here goes.... I have a function that passes a pointer to a string to another function. For example: int FunctionA ()
9
2198
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 simple array, to pass to a range constructor for a const vector. Here is some demonstration code: #include <iostream> using namespace std;
12
5403
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
6
3030
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
2220
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 **pEnteredEmployee; int main()
18
2879
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 write an ordinary differential equation class that would solve a general equation in the form: dx/dt = f(x,t). The ode class shouldn't know anything about f, except how to call it.
1
3978
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 arguments, is this VC specific or a standard c++ behavior? the code looks like this: class Base { public: int member; };
7
2232
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
2186
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 suddenly I can't use sizeof(array) / sizeof(array) anymore within that function ? Help - What point am I missing ?
0
9636
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10306
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10074
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6724
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5503
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.