in Programming in C retagged by
9,484 views
17 votes
17 votes
Consider the following $\text{ANSI C}$ program:
#include <stdio.h>
#include <stdlib.h>
struct Node{
        int value;
        struct Node *next;};
int main( ) {
    struct Node *boxE, *head, *boxN; int index=0;
    boxE=head= (struct Node *) malloc(sizeof(struct Node));
    head → value = index;
    for (index =1; index<=3; index++){
        boxN = (struct Node *) malloc (sizeof(struct Node));
        boxE → next = boxN;
        boxN → value = index;
        boxE = boxN; }
for (index=0; index<=3; index++) {
    printf(“Value at index %d is %d\n”, index, head → value);
    head = head → next;
    printf(“Value at index %d is %d\n”, index+1, head → value); } }

Which one of the following statements below is correct about the program?

  1. Upon execution, the program creates a linked-list of five nodes
  2. Upon execution, the program goes into an infinite loop
  3. It has a missing $\textsf{return}$ which will be reported as an error by the compiler
  4. It dereferences an uninitialized pointer that may result in a run-time error
in Programming in C retagged by
by
9.5k views

3 Comments

Is it option D?
1
1
Yes ama.
0
0
Yes , because the other 3 options don’t make sense.
0
0

2 Answers

18 votes
18 votes
Best answer

Lets see the first for loop:

for (index =1; index<=3; index++){
        boxN = (struct Node *) malloc (sizeof(struct Node));
        boxE -> next = boxN;
        boxN -> value = index;
        boxE = boxN; }

After this we get a linked list of size $4$ with head pointing to its beginning, and $\textsf{boxE}$ and $\textsf{boxN}$ pointing to the last node and the next pointer of the last node being uninitialized. 

Now the second for loop will do printing as follows until the second $\textsf{printf}$ of the final iteration.

  • Value at index 0 is 0
  • Value at index 1 is 1
  • Value at index 1 is 1
  • Value at index 2 is 2
  • Value at index 2 is 2
  • Value at index 3 is 3
  • Value at index 3 is 3

After this, the $\text{head}$ pointer being uninitialized will be having random content which gets treated as an address. So, when head -> value happens it is basically reading data from uninitialized memory location and so can result (not saying will result because by chance the uninitialized memory can be a valid location) in runtime error. 

To correct the error, we just have to add an extra line of code as given below:

for (index =1; index<=3; index++){
        boxN = (struct Node *) malloc (sizeof(struct Node));
        boxE -> next = boxN;
        boxN -> value= index;
    boxN-> next = NULL;
        boxE = boxN; }

Correct option: D

selected by
by

4 Comments

@Sachin Mittal 1 @Arjun @Shaik Masthan Sir after modifying the code still it will give null pointer dereferencing bcz

   0→ 1 → 2 → 3→ NULL

At the last when index=3 at that time head = head → next;  and ” head = NULL” and “NULL→ value” is only null pointer dereferencing.

 

6
6

@samarpita, yes. Last de reference could generate runtime error.

0
0

@samarpita This is wrong. At index = 3, head = head->next; the head is not NULL it can point to any location (either valid or out of scope).

1
1
8 votes
8 votes
Answer is $(d)$.

The first loop of the code creates a linked-list of size $4$. Then in the second loop will print –

Value at index 0 is 0

Value at index 1 is 1

Value at index 1 is 1

Value at index 2 is 2

Value at index 2 is 2

Value at index 3 is 3

Value at index 3 is 3

After this, the $head$ pointer will point to some random address and tries to access $value$ within it assuming it as a struct of type $Node$, which results in an error specified in option $(d)$.

1 comment

option c is not selected?
2
2