typedef struct and pointer - c

When we have:
struct node{.....}
typedef struct node Node;
typedef Node *ptr;
is ptr a pointer to struct node or typedef changes the meaning of it?

The definition
typedef struct node *ptr;
will make ptr an alias for struct node *.
Afterwards you can do either
struct node *some_pointer;
Or
ptr some_pointer;
Both will define the variable some_pointer to be a pointer to the node structure.
But making type-aliases of pointer types is not something I recommend. It can make the code harder to read, understand and maintain.
Take for example the most common problem that I've seen here on Stack Overflow when it comes to pointer type aliases:
ptr some_pointer = malloc(sizeof(ptr));
That allocates memory enough for a pointer to the structure, not for the whole structure. If using e.g.
Node *some_pointer = malloc(sizeof(Node*));
that error would be much clearer and easier to find.
See Is it a good idea to typedef pointers?

Related

Pointer in typedef struct

I came across a stack tutorial in the C language and I can't seem to understand what the *Stack pointer is pointing towards nor the *next pointer.
I would just like to have a quick explanation on what these two pointers actually point to.
typedef struct StackElement
{
int value;
struct StackElement *next;
}StackElement, *Stack;
Neither points to anything in the example you've given. This code is just the declaration of the structure type itself. You could break the typedefs out into maybe simpler form:
struct StackElement
{
int value;
struct StackElement *next;
};
typedef struct StackElement StackElement;
typedef struct StackElement *Stack;
That is, there is a declaration of the structure itself, which contains a field next to be used by the implementation code of this stack. That field, when filled in, will point to another struct StackElement structure.
The typedef parts just make convenience names - StackElement can be used in place of struct StackElement, and Stack can be used instead of struct StackElement *.

typedef struct what meant by making pointer next pointing node?

cant understand this line of code node *Next;
typedef struct Node_type node;
struct Node_type{
u32 value;
node *Next;
};
I think you answered the question itself in your question. It’s just a pointer to another ‘Node_type’ or it’s name alias ‘node’. That means you can assign it an address of another one of that type whether it be local storage on the stack or dynamically allocated memory on the heap.

Self referring structure declaration

The follwing declaration is valid.
struct node
{
int a;
struct node *next;
};
However, when we define the following, it gives error.
"error: field ‘next’ has incomplete type"
Why is it so?
struct node
{
int a;
struct node next; /* Not a pointer */
};
node in struct node is a "struct tag", which at the point you write it creates an "incomplete type": a struct variable which is not at this point declared, but not defined. The type is not complete before the final }; of your struct.
In C, an incomplete type can be referenced even before it is fully defined, by using a pointer to that type. You can however not allocate a variable (instance) of that type, because the actual struct definition is yet to be defined. (It works exactly like abstract base classes in C++, if you are familiar with those.)
So when you write
struct node {
int a;
struct node *next;
};
the row struct node *next means "here is a pointer to a struct node, even though I have no idea how that type is defined yet". But you cannot declare a variable of type struct node inside the struct definition of that very same type, simply because you cannot use something before you have created it.
You can't have structure that contains itself as a member:
struct node
{
int a;
struct node next;
};
Think about this question: if it is possible, what is the size of such structure? struct node contains struct node next as a member, then the member next would contain a member of type struct node as well, and so on and on and on... The size would be infinite.
Your second declaration would define a struct which is infinitely deeply nested, which is impossible.
This is the same case as with forward declaration of class/struct type:
struct node;
struct node* node_ptr; /* is valid */
struct node node_instance; /* is invalid */
So struct node; basically says: hey there is a structure defined somewhere outside this file. The type is valid, pointers may be used, but you cannot instantiate the object.
This is because the size of the pointer is known and is specific to the target architecture (e.g. 32 or 64-bit). The size of the structure is unknown until declared.
When you declare the type completey, then you will be allowed to declare the object of that type:
struct node {
int a;
struct node* b; /* this will work, but the size of struct is unknown yet */
}
struct node* node_ptr; /* this works always with full and forward declarations */
struct node node_object; /* now, the size of struct is known, so the instance may be created */
it should be like this:
struct node {
int a;
struct node *next;
};
this works,
but
struct node {
int a;
struct node next;
};
cannot be understood by the compiler as node becomes recursive structure, and the compiler does not know how much memory to allocate for node.
However, if you use a pointer, it understands that the size of a pointer is equal to size of the memory to be addressed, and hence reserves that space, regardless of if node is a complete struct.
Pointer store address, struct has structure. If declared like struct it would be recursive and infinite. If declared like pointer it refers to other structure somewhere else.
Infinite nodes inside a node? Does that make sense? What will be the size of "struct node"?

Creating a stack in C, structure for nodes

I have been given a structure for the nodes that will make up my stack but I am having trouble understanding it.
struct stackNode
{
char data;
struct stackNode *nextPtr;
};
typedef struct stackNode StackNode;
typedef StackNode *StackNodePtr;
I understand that I have a structure called stackNode, renamed StackNode (or a second type with the same everything except name?), which has two types, a char and a pointer to a stackNode.
I am not sure what the final line means, can anyone step through and explain it to me? I think it means there is a new type, which is a pointer to a StackNode, called a StackNodePtr. Is this right?
Yes, whenever you use a StackNodePtr you are essentially using a StackNode* that is in turn equal to a struct stackNode*. The spacing and asterix placement can evidently lead to some confusion. I would personally write it as typedef StackNode* StackNodePtr; to be a bit clearer with what gets typedef-ed to what.
These three lines would be equal:
StackNodePtr myPointer;
StackNode *myPointer;
struct stackNode *myPointer;
The reason for the typedef struct stackNode StackNode is usually to avoid having to write struct whenever using it.
You have three things.
You are declaring a type named struct stackNode, and providing the structure definition.
You are defining a new type, StackNode, that is an alias of (and is type-compatible with) struct stackNode.
You are defining a new type, StackNodePtr, that is an alias of (and, again, type-compatible with,) StackNode * (and struct stackNode *).
The poing being, that instead of declaring your node variable as struct stackNode newNode, and your list head as struct stackNode *head, you declare your node variable as StackNode newNode and your list head as StackNodePtr head.
This is a fairly common idiom, although some people recommend against doing it this way. (And, of course, other people insist that this is the right way, too.)

Next struct item,incomplete type [duplicate]

This question already has answers here:
self referential struct definition?
(9 answers)
Closed 6 years ago.
struct node{
struct node next;
int id;
}
gives "field next has incomplete type error ".
what is wrong with this struct ?
When creating a self-referential data type, you need to use pointers to get around problems of circularity:
struct node;
struct node {
struct node * next;
int id;
}
...should work, but take care to allocate memory correctly when using it.
Why a pointer? Consider this: the point of a struct definition is so that the compiler can figure out how much memory to allocate and what parts to access when you say node.id. If your node struct contains another node struct, how much memory should the compiler allocate for a given node?
By using a pointer you get around this, because the compiler knows how much space to allocate for a pointer.
If a struct could contain another instance of its own type, its size would be infinite.
This is why it can only contain a pointer to its it own type.
Furthermore, at that point in code, the size of the struct is unknown, so the compiler couldn't know how much space to reserve for it.
You need to do a forward-declaration of node, since the compiler doesn't know node yet while it's processing its definition.
You probably meant to store a pointer, not a node object itself.
Try this:
struct node;
struct node{
struct node *next;
int id;
};
Some uses of incomplete types are ill-formed, such as when you try to declare an object of an incomplete type. However, you can declare a pointer to an incomplete type (for example). In this case that is just what is needed here:
struct node{
struct node *next;
int id;
};
The problem is, when the compiler reaches this line:
struct node{
struct node next; /* << this line */
the compiler actually doesn't know what is struct node, because you're defining struct node.
In general, you cannot use a type which is not defined or incomplete.
In order to work, you should write:
typedef struct _node{
struct _node* next;
int id;
}node;

Resources