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
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.
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.
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.
Thank you for your replies.
I have to be more specific. Here's is a code sample : - #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: - 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
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())
Sign in to post your reply or Sign up for a free account.
Similar topics |
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)
|
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 ()
|
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;
|
by: Mike |
last post by:
Consider the following code:
"""
struct person {
char *name;
int age;
};
typedef struct person* StructType;
|
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 */
};
| |
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()
|
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.
|
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;
};
|
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;
}
|
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 ?
|
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...
| |
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,...
|
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...
|
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...
|
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,...
|
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...
|
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();...
| |
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |