C Programming - Pointers - Discussion

Discussion Forum : Pointers - Yes / No Questions (Q.No. 2)
2.
Is this a correct way for NULL pointer assignment?
int i=0;
char *q=(char*)i;
Yes
No
Answer: Option
Explanation:
The correct way is char *q=0 (or) char *q=(char*)0
Discussion:
10 comments Page 1 of 1.

Yuvaraj said:   6 years ago
Yes, you are right @Satish.

Abhishek said:   8 years ago
#include<stdio.h>
int main()
{
int i=0;
char *q=(char*)i;
if(q==NULL)
printf("#");
return 0;
}

This code run fine and print # this means q is my null pointer. So what is wrong in this declaration?

Kalyan said:   1 decade ago
int main()
{
int i=0;
char *q=(char*)i;
printf("%d\n",i);
return 0;
}

If we run in this code in Turbo C, one error is occurs ie., "q" is defined but not used. For that problem, what we have to do?

Caveman said:   1 decade ago
We are trying to convert an integer to character type which is not a valid type conversion even though the other way round is perfectly ok. Reason being that type conversion happens from narrower to wider data type only.
(1)

Radha said:   1 decade ago
@Ritesh.

Whatever code you have executed in gcc but in c and c++ compiler it not working.

But if we write Q instead of i in printf it would get executed.

Ritesh_IIITA said:   1 decade ago
Friends: in gcc compiler it is working fine and correct but I am not aware about turbo C compiler so if any one is having turbo C compiler then please try this code and discuss its output:

#include<stdio.h>

int main()
{
int i=0;
char *q=(char*)i;
printf("%d\n",i);
return 0;
}

Satish said:   1 decade ago
char *q;
q=(char*)&i;

It is Correct or not?

Likitha said:   1 decade ago
How are the two statements different?

Sowmya said:   1 decade ago
The use of null pointers is mainly to tell the user that "no real world entity" is present for that data type at that particular instant of access!!

Terminating a linked list or a chain of records with a NULL value helps us knowing where the chain of data ends and also to know whether the set of records is empty or it holds some value.

Karthik said:   1 decade ago
what is the use of null pointers?

Post your comments here:

Your comments will be displayed after verification.