0

So I have the function top() to return the top of the a stack (implemented as a linked list). It returns a Node struct. I am getting errors when I try to access the variables of the returned struct.

typedef struct nodeStrcut{
    int x,y;
    struct nodeStrcut* next;
}Node;

Node top(Node** head){
    return **head; 
}

void push(Node** head, int x, int y){
    //create a new node to contain the value 
    Node* newPtr = (Node*) malloc(sizeof(Node));
    newPtr->x = x;
    newPtr->y = y; 
    newPtr->next = *head;
    *head = newPtr; 
}

int main(int argc, char **argv){
    Node* stack;
    stack = NULL;
    push(&stack, 3, 3);
    push(&stack, 2, 3);
    push(&stack, 3, 5);
    printf("Node value: %d, %d\n", (pop(&stack)).x, (pop(&stack)).y); 
    return -1;
}

And then I get the following error:

project.c: In function ‘main’:
error: request for member ‘x’ in something not a structure or union
error: request for member ‘y’ in something not a structure or union

I know that I could use stack->x to get values but I need to have a function that returns value from the stop of the stack. Help would be appreciated.

Vineet Patel
  • 477
  • 7
  • 17

3 Answers3

1

I suppose it's just a typo (pop instead of top), such that you actually call a library function not returning your Node type. Write printf("Node value: %d, %d\n", top(&stack).x, top(&stack).y); and it should work as expected.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
1
Node* newPtr = (Node*) malloc(sizeof(Node));

There is no need to cast the return of malloc, it is unnecessary. See: Do I cast the result of malloc?. The following is sufficient:

Node *newPtr = malloc (sizeof *newPtr);

The address for head isn't changing in top, so there is no need to pass the address of head, e.g.

Node top (Node *head){
    return *head;
}

You shouldn't return a negative value from main(). There are two defined returns:

EXIT_SUCCESS: 0
EXIT_FAILURE: 1

See What should main() return in C and C++?

Putting it altogether:

#include <stdio.h>
#include <stdlib.h>

typedef struct nodeStrcut{
    int x,y;
    struct nodeStrcut* next;
} Node;

Node top (Node *head){
    return *head;
}

void push (Node **head, int x, int y) {

    Node *newPtr = malloc (sizeof *newPtr);
    newPtr->x = x;
    newPtr->y = y;
    newPtr->next = *head;
    *head = newPtr;
}

int main (void) {

    Node *stack = NULL;

    push (&stack, 3, 3);
    push (&stack, 2, 3);
    push (&stack, 3, 5);

    printf ("Node value: %d, %d\n", (top (stack)).x, (top (stack)).y);

    return 0;
}

Example Use/Output

$ ./bin/stacktop
Node value: 3, 5
David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

You don't need to pass in a pointer to a pointer in top(). It's sufficient to change the function definition from Node top(Node** head) to Node top(Node* head).

Now you won't need to pass in the address of stack when calling the following (without spelling mistakes):

printf("Node value: %d, %d\n", (top(stack)).x, (top(stack)).y); 
frslm
  • 2,969
  • 3
  • 13
  • 26