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;
}
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 2 of 2.
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;
}
#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;
}
Kavita bhate said:
1 decade ago
Assigning null to pointer is not compulsory (Y/N) (explain)?
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.
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.
Swapnil said:
8 years ago
There will be a segmentation fault.
Right @Shiva.
Right @Shiva.
Prudhvi sai said:
6 years ago
It's a segmentation fault. It's point to a garbage value.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers