whats the difference between making the head and n in the code - c

In this code a head object is made right in front of the structure definition,
but the n is made in int main.
Can we just declare both the way head was declared? I think I'm just not understanding how struct node *n is different from the way head was declared, because they are both objects right?
struct node
{
int data;
struct node *next;
}*head;
.
.
.
int main()
{
int i,num;
struct node *n;
head = NULL;
...

In your code you are mixing the declaration of struct node and the declaration of the variable head, which is correct C but TBH not very readable.
Write your code rather like that:
struct node
{
int data;
struct node *next;
}; // declaration of struct node
.
.
struct node *head; //declaration of a variable head of type "pointer to struct node"
.
.
.
int main()
{
int i,num;
struct node *n; //declaration of a local variable n of type "pointer to struct node"
head = NULL;
...
It's equivalent to your code, but more readable.

"head" is a global pointer where as "n" is local pointer to node structure.
Also note that neither "head" nor "n" are objects. They are pointer to objects of node structure. You need to allocate memory for them.

Related

Why write * sign before declaring a struct variable, which has a self referencing pointer in C?

I was making a linked list in C, then a query raised in my mind that (read the title above)
struct node
{
int data;
char age;
} temp;
// versus
struct node
{
int data;
struct node* next;
} *temp;
A struct like node cannot contain itself. If this were allowed, each struct node variable would be infinitely sized.
struct node* next; is a pointer to a struct node value. A pointer has a known, finite size and thus a struct can contain a pointer to another value of the same type. By having a pointer to a next struct node you are creating a linked list. This pointer can also be NULL which allows your list to have an end.

Typedef/Global structure in C, without using "typedef"?

Language: C
I wish I knew how to reference this in the title better. I recently came across this piece of code concerning struct definition, and I am unfamiliar with the syntax, particularly the "*Node" portion. Might someone please help me understand what's going on? Why/how aren't we using typedef/ is this something else entirely? For further reference, the source code uses "Node" (no *asterisk) within some function definitions without passing or declaring any "Node" variables within the function itself. This leads me to think it's some sort of global variable declaration. Any help appreciated!
struct node {
char word[MAX];
struct node *left;
struct node *right;
} *Node;
In struct declaration, the right brace that terminates the list of members may be followed by list of variables. So, here
struct node {
char word[MAX];
struct node *left;
struct node *right;
} *Node;
Node is of type struct node * which can hold pointer of any struct node type variable. That means, if you have
struct node anode;
you can do
Node = &anode;
If you have this kind of global struct declaration followed by variables after right brace, that terminates the list of members, then all those variable are global variables of that struct type. That means, if you have
struct node {
char word[MAX];
struct node *left;
struct node *right;
} Node, *ptrNode;
and this struct node type defined globally then both Node and ptrNode are global variables.
Don't confuse this with typedef. typedef is used to create an alias name for another data type.
For e.g.
tyepdef struct node {
char word[MAX];
struct node *left;
struct node *right;
} stNode, *ptrStNode;
Now the stNode is an alias of struct node and ptrStNode is alias of struct node *. That means, you can use them to declare variables of struct node and struct node* type respectively.
For struct node type, this
stNode aNode;
is same as
struct node aNode;
and for struct node * type, this
ptrStNode ptrNode;
is same as
stNode *ptrNode;
is same as
struct node *ptrNode;
Hope this helps.
Code defines a pointer name Node to a struct. See declare Node as pointer to struct node;
Perhaps it is more understandable broken down into two step equivalent code.
// Define `struct node`
struct node {
char word[MAX];
struct node *left;
struct node *right;
};
// Define & declare a pointer: `Node`
struct node *Node;
Why/how aren't we using typedef/ is this something else entirely?
Author did not want to define an alias for the type struct main via typeface. Just to define a type called struct main and declare a pointer.

What is the difference in '->' between struct node and struct node*?

I am new to pointers and there is this code for merge sort of linked lists. And here it has declared a dummy node as struct node dummy; and the next node for dummy node is NULL so to set it we use dummy.next = NULL;.
/* Link list node */
struct node
{
int data;
struct node* next;
};
struct node* SortedMerge(struct node* a, struct node* b)
{
/* a dummy first node to hang the result on */
struct node dummy;
/* tail points to the last result node */
struct node* tail = &dummy;
/* so tail->next is the place to add new nodes
to the result. */
dummy.next = NULL;
//Code continues...
}
I understand that i can use it if it was struct node *dummy;
but we cant use it here as it is not a pointer node.
So my question is why doesn't dummy->next = NULL work here?
and what is the difference between struct node and struct node* ??
a -> b is shorthand for (*a).b.
If a is not a pointer, *a is not valid, and neither is a -> b.
dummy is not a pointer to a structure. It is the structure variable itself.
You can derefer attributes of a structure with the operator -> only if it is a pointer to the structure.
If you are using the struct variable, then . is the way to go about, which is very much the case with dummy.
I understand that i can use it if it was struct node *dummy;
If by "it" you mean struct node dummy; then the answer is no. You cannot use a pointer to node in the same way as a pointer to node.
So my question is why doesn't dummy->next = NULL work here?
Because dummy is a node, not a pointer, and operator -> if for pointers. The expression dummy->next has the same semantics as (*dummy).next.
. So my question is why doesn't dummy->next = NULL work here? and what is the difference between struct node and struct node* ?
Declared as this struct node dummy;
dummy->next=NULL doesn't work because dummy is not a pointer to struct .
If you write so -
struct node A; // then A is a struct variable which can access struct members using '.' operator
and this -
struct node* B; // then B is a pointer to struct node which can access struct member using '->` or like this (*B).data=something.

What is the purpose of the first "node" in the declaration: "typedef struct node { - - - } Node;"?

I am studying code examples from my professor in order to become better acquainted with linked data structures.
In our linked-list.c example the professor defines a type Node as follows:
typedef struct node {
int data;
struct node *next;
} Node;
What's the point of the lower case node? I was under the impression that you could just write, for example:
typedef struct {
int data;
struct node *next;
} Node;
and then use Node as its own type. Does it have something to do with the fact that if you don't include a lower case node then when the compiler is evaluating the code it will not be able to understand what is meant by "struct node *next"?
Take a look at this declaration:
struct node {
int data;
struct node *next;
};
typedef struct node Node;
This can be combined into a single statement (simplifying a declaration):
typedef struct node {
int data;
struct node *next;
} Node;
Does it have something to do with the fact that if you don't include a lower case node then when the compiler is evaluating the code it will not be able to understand what is meant by "struct node *next"?
Yes.
The node in struct node is the tag of the struct type. If you give the struct a tag, you can refer to that type from the moment on the tag is complete, so in
typedef struct node {
int data;
struct node *next;
} Node;
the struct node *next; declares a member next that is a pointer to the struct type being defined. The typedef name Node is not available before the ; ending the definition is reached.
If you omit the tag, you cannot refer to the type being defined in any way before the typedef is complete, so in
typedef struct {
int data;
struct node *next;
} Node;
the line struct node *next; declares a new, unrelated, incomplete struct type with the tag node that next points to.
That's valid, but nothing about struct node is known (unless it is defined somewhere else), so you can't use the next pointer without casting it to a pointer to a complete type everywhere (not quite everywhere, Node foo; foo.next = malloc(12); etc. would still work).
He is defining a temporary name for the node because he is using a well know technique to avoid writing struct node on the declaration of each struct object.
If he would just do:
struct node {
int data;
struct node *next;
};
you would have had to use:
struct node* node;
to declare a new node. And to avoid that you would have to define later:
typedef struct node Node;
in order to be able to declare objects like the following:
Node* node;
In the end:
typedef struct node {
int data;
struct node *next;
} Node;
Is just a shortcut for struct node { ... }; in addition to typedef struct node Node;.
Here struct node is a type like int
and Hence
struct node {
int data;
struct node *next;
}NodeVar;
means you are declaring a single variable Node of struct node.
like int intVar;
typedef is to make your code understandable.
so that when you use
typedef struct node Node;
you can use the same declaration as
Node NodeVar;
Consider this code:
#include <stdio.h>
typedef struct {
int data;
struct node *next;
} Node;
int main()
{
Node a, b = {10, NULL};
a.next = &b;
printf("%d\n", a.next->data);
}
This won't compile. The compiler has no idea what a struct node is, other than it exists. So you might change the definition in the struct to Node *next;. The typedef isn't in scope before it's declared, so it still won't compile. The simple answer is to do as he said, use the node tag after struct, and it works fine.
The lower case 'node' is a structure type... i.e. a struct node { stuff } is a node structure containing stuff.
On the other hand, the upper case "Node" is a completely new data type which refers to a 'struct node'
Generally (though in C++ I think you can), you cannot pass around a "node" in a C program... for example as an argument to a function. Rather, you would have to pass a 'struct node' as your argument...
// this will throw a syntax error because "node" is not a data type,
// it's a structure type.
void myFunc( node* arg );
// while this will not because we're telling the compiler we're
// passing a struct of node
void myFunc( struct node* arg );
// On the other hand, you *can* use the typedef shorthand to declare
// passing a pointer to a custom data type that has been defined
// as 'struct node'
void myFunc( Node* arg );

what does typedef struct node *NODE indicate?

struct node
{
int coef;
int exp;
struct node *link;
};
typedef struct node *NODE;
It defines NODE as a synonym for the type struct node *, so when you'll be declaring a variable of type NODE you'll be actually declaring a pointer to struct node.
Personally, I don't think that such declaration is a good idea: you're "hiding a pointer" (which is almost always a bad idea), and, moreover, you are not highlighting this fact in any way into the new name.
It makes NODE a typedef for a struct node *.
NODE becomes an alias for struct node*.
EDIT: Okay, for the comment (if I write my answer as comment, it would be too long and not formatted):
There's no different way to write this. Here, typedef is used just to create a synonym/alias for pointer to struct node.
An example for usage would be:
void f()
{
// some code
NODE p = NULL;
// do something with p
// for example use malloc and so on
// like: p = malloc( sizeof( struct node ) );
// and access like: p->coef = ..; p->expr = ..
// do something with p and free the memory later (if malloc is used)
}
is the same as
void f()
{
// some code
struct node* p = NULL;
// do something with p
}
Using NODE makes it just shorter (anyway, I wouldn't advise such typedef, as you're hiding, that it's a pointer, not a struct or other type, as mentioned in #Matteo Italia's answer).
The format, you're referring: "typedef struct{}type_name format" is something else. It's kind of a trick in C, to avoid writing struct keyword (as it's obligatory in C, and NOT in C++). So
typedef struct node
{
//..
} NODE;
would make NODE alias for struct node. So, the same example as above:
void f()
{
// some code
NODE p;
// do something with p
// note that now p is automatically allocated, it's real struct
// not a pointer. So you can access its members like:
// p.coef or p.expr, etc.
}
is the same as
void f()
{
// some code
struct node p;
// do something with p
}
NOTE that now, p is NOT a pointer, it's struct node.
simply tells you can create pointer of node type using only NODE every time instead of writting struct node * everytime
what does typedef struct node *NODE indicate?
UPPERCASE IS NO GOOD
Reserve ALL UPPERCASE identifiers for MACROS.

Resources