3

Suppose I have the following structure

typedef struct _Stack {
   struct _Stack *next;
} Stack;

Note in above there is no data type for storage, only *next pointer to structure. So, my question is will it be possible that the following function is valid.

void stackPush(Stack **stackP, void *dataP) {
    Stack *data = (Stack*)dataP;

    data->next = *stackP;
    *stackP = data;
}

I saw this function in glib library in the file gtrashstack.c. But when I compiled above, I got a warning : In data->next : assignment from incompatible pointer type.

I know that, I can rewrite the structure with generic pointer. But I only want to know, why the above will not work?

Update: My mistake, here I write typedef struct _Stack but in my program, I missed _Stack.

4
  • Your question is based on a false premise. It's valid and it will work. Though data->next = *stackP should probably be written data->next = &(stackP->next); or data->next = (struct _Stack *) *stackP;. Commented Jan 16, 2014 at 6:02
  • why not data->next = (Stack *)*stackP Commented Jan 16, 2014 at 6:06
  • Because data->next is of type struct _Stack *, not Stack *. Commented Jan 16, 2014 at 6:09
  • @David data->next = (Stack*)*stackP` also work. It's my mistake, I forget to add _Stack in typedef struct in my original program Commented Jan 16, 2014 at 6:13

2 Answers 2

2

This function is valid. Probably it is used for different structures like:

 typedef struct my_Stack {
   struct my_Stack *next;
   sometype1 somename1;
   ...
   sometypen somenamen;
 } MyStack;

i.e. for the lists where the pointer to the next elem is placed as the first field of the structure. It is the same trick as when classes are built via simple inheritance in C++. For such a structure you can call:

x = malloc(sizeof(struct my_Stack));
x->somename1 = aaa; ...
stackPush(&mylist, x);

I am not sure if this style of programming is supported by the C standard. It's not a good style for novices. It is for developers who know what they are doing in C.

Sign up to request clarification or add additional context in comments.

5 Comments

So it means, we can also implement linked list with only pointer to structure and no data type.
It is the same thing.
it's better said that it's just a trick. the data IS there right next to the next pointer. But in the context of stackPush it didn't need to care how big the data was. so it just received *dataP as void pointer; interpret it as having a 'next' pointer at that address (not caring that there might also be data in the following address); then just do stackPush
So it's just a way to implement stackPush for any type of linked-list node, independent of what the data type is. as long as the linked-list node declare next pointer as the first variable in the struct.
@ashish2expert Check keyword: Intrusive list, there is an implemention in kernel.
1

The answer is yes, it is possible to implement stack with only pointer to structure (no data type). See implementation of generic list or queue in c in Linux (don't know if it is implemented in windows also). See how it is implemented Linux Kernel Linked List Explained

2 Comments

Thanks for the answer, I really want this
@ashish2expert Pay attention to the list_entry macro which returns pointer to the actual data struct given absolute memory address of inner filed list( this will be stack in your case)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.