struct node {
int data;
struct node* next;
}
void push (struct node **head, int data) {
struct node* newNode = malloc (sizeof (struct node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
C++ version
void Stack::push( void *data ) {
struct node *newNode = new node;
newNode->data = data;
newNode->next = head;
head = newNode;
}
In C ++, head is a private or protected member of the stack class and is declared as node * head.
Question: why the head can retain its value after calling push () in C ++.
In c, we need to declare it as **, since we want to change the value of the head pointer after calling the push () function. In C ++ code, won't the changes in the head be lost after the call?
source
share