C Programming - Pointers - Discussion

Discussion Forum : Pointers - Point Out Errors (Q.No. 1)
1.
Point out the compile time error in the program given below.
#include<stdio.h>

int main()
{
    int *x;
    *x=100;
    return 0;
}
Error: invalid assignment for x
Error: suspicious pointer conversion
No error
None of above
Answer: Option
Explanation:
While reading the code there is no error, but upon running the program having an unitialised variable can cause the program to crash (Null pointer assignment).
Discussion:
15 comments Page 1 of 2.

Sam sparks said:   1 decade ago
A NULL pointer assignment is a run time error It occurs due to various reasons one is that your program has tried to access an illegal memory location. Illegal location means either the location is in the operating systems address space or in the other processes memory space. In stdio.h NULL is defined as 0.

So whenever your program tries to access 0th location the operating system kills your program with run time assignment error because the 0th location is in the operating systems address space and operating system doesn't allow access to its address space by user program.

Prathik said:   1 decade ago
IT will most likely crash, Its known as unspecified behavior as per standards. When the program runs, it is allocated memory, and since it is not set to NULL, it stores a garbage value.

De-referencing that garbage valued memory location is most unlikely to be in your current process space. And hence throws a segmentation error. However if you just got lucky and the garbage value stored in the memory allocated for x is residing in your process space, then the program works as if nothing is wrong.

Kishor Jyoti Sarma said:   1 decade ago
Pointer pointing is pointing nowhere, so there will be segmentation fault.

#include<stdio.h>
#include<malloc.h>
int main()
{
int *x=NULL;
x=malloc(sizeof(int));// Now a block of memory is allocated dynamically . Now pointer is pointing to that memory block.
*x=100;// assigning the constant 100 to that block.
printf("%d",*x);
return 0;
}

Ripu said:   1 decade ago
Since pointer is pointing to unknown location having garbage value. Program will run but never give expected value and your program will get crashed.

Ritika Gera said:   1 decade ago
It will cause segmentation fault as you cannot assign a constant value to a pointer. How do you know for sure that such an address exists in memory.

Shreyans said:   1 decade ago
The crash is because of the 100th location which is not surely belongs to same program/process's address space in which currently its running.

Wikiok said:   1 decade ago
In Win7/GCC it runs, no crash.
But if the compiler set x to NULL in the definition line, then it will crash.

Tim said:   1 decade ago
Obviously, on most machines, this will cause a crash. That was not one of the choices.

Shiva said:   1 decade ago
there is no way to assign a constant to pointer directly.it cause segmentation fault.

Kavita bhate said:   1 decade ago
Assigning null to pointer is not compulsory (Y/N) (explain)?


Post your comments here:

Your comments will be displayed after verification.