how a pointer to null works .

M

maadhuu

hello ,

i want to know the behaviour of the pointer under the following
condition.
the code snippet is as follows :

#include<stdio.h>

int main()
{
int *p = 0;
++p;
printf("%d",p);
return 0;
}

what will be the output ???? is is always the same ???? say, size of
integer on this machine is 4.

thanx
ranjan.
 
A

akarl

maadhuu said:
i want to know the behaviour of the pointer under the following
condition.
the code snippet is as follows :

#include<stdio.h>

int main()
{
int *p = 0;
++p;
printf("%d",p);
return 0;
}

what will be the output ???? is is always the same ???? say, size of
integer on this machine is 4.

The symbol 0 in pointer contexts is the null pointer constant. How it's
represented internally may vary so the output is *not* always the same.

August
 
J

Jack Klein

On Mon, 01 Aug 2005 23:07:14 -0400, "maadhuu"

There is no such thing as a "pointer to null". There is a null
pointer, and there can be null pointers of any pointer type. A null
pointer does not point to address 0, and it does not point to "null",
in fact it is specifically defined NOT to point to anything that your
program has the right to access.
hello ,

i want to know the behaviour of the pointer under the following
condition.
the code snippet is as follows :

#include<stdio.h>

int main()
{
int *p = 0;
++p;

The line above attempts to perform pointer arithmetic on a null
pointer. This is not allowed by the C standard and has undefined
behavior. So from here on, C does not know or care what happens.
printf("%d",p);

Oops, more undefined behavior here. The "%d" conversion specifier for
printf() requires an argument of type int, not any kind of pointer.
If you want to print the value of a valid pointer, which yours is not
after you try to increment it, you need to cast it to a pointer to
void and use the "%p" conversion specifier:

printf("%p\n", (void *)p);

Notice that I also added a '\n' on the printf() statement. If the
last line sent to stdout does not end in a '\n', the C standard does
not require it to ever appear, and on some platforms it does not.
return 0;
}

what will be the output ???? is is always the same ???? say, size of
integer on this machine is 4.

The output will be whatever it is if you compile and run the program,
assuming it outputs anything at all. The behavior of the increment is
undefined, just like division by 0 in mathematics. As far as the C
language is concerned, anything at all that happens is just as right
or wrong as anything else.
 
S

sachin dooble

ranjan
not very much sure but in turbo there will be compilation error.
u can't have int *p=0 since here u have only declaration not
definition .
so u are trying to increase a undefined pointer .
also in 4th line u are printing the value of the pointer in
decimal format which will be in -ve
this is because address should be printed in %u or %x form.
so overall there will be a compilation error that's what i
feel.
Do inform me plz
 
K

Keith Thompson

sachin dooble said:
ranjan
not very much sure but in turbo there will be compilation error.
u can't have int *p=0 since here u have only declaration not
definition .
so u are trying to increase a undefined pointer .
also in 4th line u are printing the value of the pointer in
decimal format which will be in -ve
this is because address should be printed in %u or %x form.
so overall there will be a compilation error that's what i
feel.
Do inform me plz

Context, dammit! Don't assume that your readers can see the article
to which you're replying.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

And please don't use abbreviations like "u" for "you" and "-ve" for
"negative". They just make what you write more difficult to read.

If you had been following this newsgroup, you would have seen this
advice many many times.

The code in the original post was:

#include<stdio.h>

int main()
{
int *p = 0;
++p;
printf("%d",p);
return 0;
}

There's actually nothing in that program that would cause a
compilation error (did you try it?). There are two instances of
undefined behavior. See Jack Klein's followup for a good explanation.
 
F

Flash Gordon

sachin dooble wrote:

Please provide context. It is possible to do this through Google and
both CBFalconer and Keith regulary post instructions on how to do this
so you have no excuse for not having seen them.

Copied from another post (since I don't have the original):
>i want to know the behaviour of the pointer under the following
> condition.
> the code snippet is as follows :
>
> #include<stdio.h>
>
> int main()
> {
> int *p = 0;
> ++p;
> printf("%d",p);
> return 0;
> }
>
> what will be the output ???? is is always the same ???? say, size of
> integer on this machine is 4.

ranjan
not very much sure but in turbo there will be compilation error.
u can't have int *p=0 since here u have only declaration not
definition .

Wrong. That line of code is entirely correct.

Also, please don't use stupid abreviations line "u" or "plz". All they
do is make it harder to read your posts.
so u are trying to increase a undefined pointer .

Wrong. The OP is trying to increment the null pointer, which results in
undefined behaviour.
also in 4th line u are printing the value of the pointer in
decimal format which will be in -ve

Wrong. I've worked on systems where all pointers in to RAM will have
positive integer representations less than 8191. The printf is wrong
because %d is for an integer and p is a pointer.
this is because address should be printed in %u or %x form.

Wrong. %p is the correct format specifier for a pointer and even then it
has to be cast to void*. You could cast it to an unsigned integer type,
but there is no guarantee that ANY integer type, signed or unsigned, is
large enough. The correct line would be
printf("%p",(void*)p);
so overall there will be a compilation error that's what i
feel.
Do inform me plz

You feel wrong. There is undefined behaviour due to incrementing a null
pointer and using the wrong format specifier, but no diagnostic is required.
 
K

Kenny McCormack

On Mon, 01 Aug 2005 23:07:14 -0400, "maadhuu"

There is no such thing as a "pointer to null".

Wrong. Observe:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char null = 'A';
char *p = &null;

printf("p is a pointer to null. The value it points to is: %c\n",*p);
exit(0);
}
 
S

sathyashrayan

Kenny said:
Wrong. Observe:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char null = 'A';
char *p = &null;

printf("p is a pointer to null. The value it points to is: %c\n",*p);
exit(0);
}

Time to read a good C book!!



--
"combination is the heart of chess"

A.Alekhine

Mail to:
sathyashrayan AT gmail DOT com
 
D

Default User

Keith Thompson wrote:

And please don't use abbreviations like "u" for "you" and "-ve" for
"negative". They just make what you write more difficult to read.


I'll say. I had no idea what "-ve" meant.




Brian
 
C

CBFalconer

Default said:
I'll say. I had no idea what "-ve" meant.

Must be a generational thing. That is the one abbreviation I have
no problem with. And I am ancient.
 
R

R Pradeep Chandran

Kenny McCormack wrote:
:> In article <[email protected]>,
:> There is no such thing as a "pointer to null".
:>
:> Wrong. Observe:
:>
:> #include <stdio.h>
:> #include <stdlib.h>
:>
:> int main(void)
:> {
:> char null = 'A';
:> char *p = &null;
:>
:> printf("p is a pointer to null. The value it points to is: %c\n",*p);
:> exit(0);
:> }
:>
:
:Time to read a good C book!!

Or is it time to get a sense of humour?

Have a nice day,
Pradeep
 
R

R Pradeep Chandran

Default User wrote:
:> Keith Thompson wrote:
:> I'll say. I had no idea what "-ve" meant.
:Must be a generational thing. That is the one abbreviation I have
:no problem with. And I am ancient.

Not necessarily. -ve and +ve would be familiar to people who work with
embedded systems. Since you regularly hang out at such places, it is not
surprising that you have no problem with it.

Have a nice day,
Pradeep
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,915
Members
47,456
Latest member
JavierWalp

Latest Threads

Top