Why list node is pointer type what's difference will be without pointer?
typedef struct node {
char data;
struct node *nextptr;
} node_t;
typedef node_t *listnode;
it is pointer type because you have n elements, and for every element you now it data, and pointer for next element. n you may not know, so you use dynamic array of your structures. you can declare you your array using *. and after that you can give him memory. if you know n, it may be typedef node_t listnode[n];
In dynamic allocation of memory, you need a pointer to store the base address of memory space allotted. Thus declaring a pointer to the structure of type node_t helps you to go with dynamic memory allocation.
Related
I'm having trouble understanding pointers with nodes and arrays. assume we have this structure:
typedef struct node
{
int x;
struct node *next;
}node;
if I have an array of node pointers like:
node *table[50];
can I say:
table[0] = malloc(26 * sizeof(node*));
is it possible?
I mean table[0] is a pointer to a node but malloc will return a pointer to a pointer of a node.
In fact, I want to make more than one (for each pointer in the new array I want to create a new array of node pointers and at last each element of the last array will have a linked list)
Hope I was clear and excuse me for my bad English.
malloc is allocate space for your data.
table = (node*)malloc(50 * sizeof(node));
now table is a pointer to a space in memory that can store 50 node.
then you can use this as an array:
table[n]
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?
I am learning about uses of data structures and came across some doubts:
struct node
{
int data;
struct node *next;
}*head;
Question1: In the above structure, what does declaring a struct node *next within the structure mean?. And how is it different from simply declaring a pointer variable within structure as int *next
Question2: We can see that, at the end of the structure definition, a pointer *head is declared of type node and is used to access the structure members if I am right. Is this same as declaring it like this:
struct node *head;
Any help with this would be great. Thank you all in advance.
"struct node *next" declares a variable called next which points to a "struct node". In other words, a singly linked list.
You are correct in that the statement does two things:
Declares a struct called node (with an int called data and a
pointer to the next struct node)
Declares a variable called head pointing to the struct node.
These could have been done separately as follows:
struct node
{
int data;
struct node *next;
};
struct node *head;
This is very commonly how Linked Lists are declared in C. Recall — a linked list is a series of nodes which contain data as well as a link (in this case, a pointer) to the next node in the series.
So, each object of type struct node should contain a pointer to another struct node. This is characterized by declaring a field next in the struct with type struct node *.
As for your second question: the code you provided both defines struct node (allowing you to instantiate objects of type struct node), and declares a pointer to such a struct called head. You could certainly define the variable head the way you asked, but you would still need to define the struct somewhere earlier in the code. This is usually done in a separate header file, but could be done in the same .c source code file for brevity.
Now, note that both these methods will not actually create a struct node object. All you're doing is declaring a pointer to one. The object doesn't yet exist anywhere since you never allocated space for it. To do that, you'd need to use malloc:
struct node {
int data;
struct node * next;
} * head;
head = malloc(sizeof(struct node));
You could also declare a runtime stack instance of the struct, which just means you won't need to explicitly free the memory you allocated with malloc(). The memory allocated for the struct is deallocated when you return from the function, or when your program terminates if declared outside of any functions.
Declaring next as struct node *next means that it points to a struct node, presumably the next node in the list. If it were instead declared as int *next then it would point to an int, which it doesn't seem there's any need for.
The answer to question 2 is yes. They are simply combining the definition of struct node with the declaration of head. They could be split apart as you indicated.
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"?
0i have declared a struct as follows:
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
and i have a linked list with a pointer called first pointing to it.
and i have also declared an arraye of the above type NODE NODE* array[312500];
and now i want to make array[0] point to the linked list instead of first, so i have used array[0]->ptr=first;
but this way gives me a segmentaion fault!!! what might be the problem!!!
You declared an array of pointers, but never allocated memory for any of the pointers.
NODE* array[312500];
Is an array of 312500 pointers of type NODE*
If you wanted just NODEs, then say
NODE array[312500];
Else, you will need to say something like
array[0] = (NODE*) malloc(sizeof(NODE));
And then you can alter its ptr
You forgot to allocate memory to the array.
You just have an array of pointers.So array[0]->ptr will give a segmentation fault.
Allocate memory to the array first :
for(i=0;i<312500;++i)
{
array[i]=(NODE*) malloc(sizeof(NODE));
}
Or at least to array[0] by the same syntax to use array[0]->ptr.