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.

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.

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.

Thilagavathi said:   1 decade ago
Is it possible to assign a value directly to a pointer.

Priyanka said:   1 decade ago
@shiva

How can it leads to segmentation fault?

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.

Rohini said:   1 decade ago
@Shiva.

There is no segmentation fault.

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.

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.

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.


Post your comments here:

Your comments will be displayed after verification.