-3

What is difference between different pointer allocation?

 struct node {
   char *name;
   struct node *itself;
 };

My question is -

Why it is important to malloc structure pointer variable name ? to point it something, while self_referential did not need to be dynamically allocated.

I mean both are null pointer...so why??

BEPP
  • 875
  • 1
  • 12
  • 36
rs_prog
  • 11
  • 5
  • 3
    Please place the question in the actual post. That being said, you need to allocate (sometimes) for the `char *` because it is a C style string, and often should not be referring to an external string. – owacoder Oct 09 '15 at 16:30
  • 1
    @owacoder: `char *` is **neither** a string, **nor** an _array_! – too honest for this site Oct 09 '15 at 17:07
  • @Olaf - I know it is neither a string nor an array. (I didn't think I mentioned arrays in my comment. Or did I?) In this case, though, it is being used for a name, which is obviously a **string**. – owacoder Oct 09 '15 at 18:43
  • @owacoder: you might know, but you forgot others - including OP - reading your comment do **not**. It is essential to be correct on a helper site. And not, it is not a string (type), but a `char array` which is **used** to **store** a C string. Yet it still is an array! – too honest for this site Oct 09 '15 at 21:44
  • @Olaf - I see your point. I apologize for the confusion. I suppose I should have said "`name` is being used as a pointer to a series of consecutive characters which collectively store a name." That avoids the whole array/pointer/string terminology altogether. ;) – owacoder Oct 09 '15 at 22:54
  • @owacoder: There is a name for "A cosecutive sequence of charcters": "array"... – too honest for this site Oct 09 '15 at 23:02

2 Answers2

2

Both pointers will need to be set to something. name must either be set to point to a string literal, an array of char, or a chunk of memory returned from malloc or calloc or realloc. itself will need to be set to another instance of struct node. Whether you need to use dynamic allocation or not depends on what you're trying to do.

In a singly-linked list, the itself pointer will hold the address of another instance of struct node. You can picture it as something like the following:

+---+---+    +---+---+    +---+---+    +---+---+   
|   |   |--->|   |   |--->|   |   |--->|   |   |---0
+---+---+    +---+---+    +---+---+    +---+---+    
  |            |            |            |             
  |            |            |            |            
  V            V            V            V            
+---+        +---+        +---+        +---+          
|'f'|        |'b'|        |'b'|        |'b'|        
+---+        +---+        +---+        +---+
|'o'|        |'a'|        |'l'|        |'l'|
+---+        +---+        +---+        +---+
|'o'|        |'r'|        |'e'|        |'u'|
+---+        +---+        +---+        +---+
| 0 |        | 0 |        |'t'|        |'r'|
+---+        +---+        +---+        +---+
                          |'c'|        |'g'|
                          +---+        +---+
                          |'h'|        |'a'|
                          +---+        +---+
                          | 0 |        | 0 |
                          +---+        +---+

We have 4 instances of struct node. The name members of four of them are pointing to strings, and the itself members of three of them are pointing to other instances of struct node.

Now, whether the strings or other node instances are dynamically allocated or not depends on your program. You can allocate an array of struct node and have each element explicitly point to the next one in turn:

struct node nodes[N] = {{NULL, NULL}}; 

for ( size_t i = 0; i < N-1; i++)
  nodes[i].itself = &node[i+1];

You can set each node to point to an already-existing array of char or a string literal:

char buffer[] = "foo";
node[i].name = buffer;
node[j].name = "blah";

Alternately, you can allocate everything dynamically:

/**
 * Adds a name to the head of a list
 */
void pushName( struct node **head, const char *name )
{
  struct node *newNode = malloc( sizeof *newNode );
  if ( newNode )
  {
    newNode->name = malloc( strlen( name ) + 1 );
    if ( newNode->name )
      strcpy( newNode->name, name );
    newNode->itself = *head;
    *head = newNode;
  }
}
...
struct node *list = NULL;
pushName( &list, "blurga" );
pushName( &list, "bletch" );
pushName( &list, "bar" );
pushName( &list, "foo" );

which gives us something like the diagram above.

It all depends on what you're going for.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

Why it is important to malloc structure pointer variable name ?

1. You can take input in it . Without allocating memory you will write to invalid location causing UB.

2. So that you can use it in functions like strlen , strcpy ,strcat (exception being strdup but that will also allocate memory). Otherwise , reason is same as above .

And it's not true that memory allocation is not required for struct pointer. You will need to do that while creating data structures without fixed size .

I mean both are null pointer...so why??

But you should not dereference null pointer ,if you so you get segmentation fault (if lucky).

ameyCU
  • 16,489
  • 2
  • 26
  • 41