473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how a pointer to null works .

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.

Nov 15 '05 #1
13 1297
maadhuu wrote:
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
Nov 15 '05 #2
On Mon, 01 Aug 2005 23:07:14 -0400, "maadhuu"
<ma************@yahoo.com> wrote in comp.lang.c:

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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #3
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

Nov 15 '05 #4
"sachin dooble" <sa***********@gmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #5
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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #6
In article <8r********************************@4ax.com>,
Jack Klein <ja*******@spamcop.net> wrote:
On Mon, 01 Aug 2005 23:07:14 -0400, "maadhuu"
<ma************@yahoo.com> wrote in comp.lang.c:

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);
}

Nov 15 '05 #7
Kenny McCormack wrote:
In article <8r********************************@4ax.com>,
Jack Klein <ja*******@spamcop.net> wrote:
On Mon, 01 Aug 2005 23:07:14 -0400, "maadhuu"
<ma************@yahoo.com> wrote in comp.lang.c:

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!!

--
"combination is the heart of chess"

A.Alekhine

Mail to:
sathyashrayan AT gmail DOT com

Nov 15 '05 #8
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
Nov 15 '05 #9
Default User wrote:
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.


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

--
"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." - Keith Thompson
Nov 15 '05 #10
sathyashrayan wrote:

Time to read a good C book!!


And here's a nice article you should read:
http://en.wikipedia.org/wiki/Sarcasm

-- Denis
Nov 15 '05 #11
On Tue, 02 Aug 2005 21:26:36 +0530, in comp.lang.c, sathyashrayan wrote:
:Kenny McCormack wrote:
:> In article <8r********************************@4ax.com>,
:> Jack Klein <ja*******@spamcop.net> wrote:
:> 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 Pradeep Chandran pradeep DOT chandran AT siemens.com
All opinions are mine and do not represent those of my employer.
Nov 15 '05 #12
On Tue, 02 Aug 2005 18:43:34 GMT, in comp.lang.c, CBFalconer wrote:
: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
Nov 15 '05 #13
In article <dc**********@news1.xnet.hr>,
Denis Kasak <gand13/SPAM/@gmail.com> wrote:
sathyashrayan wrote:

Time to read a good C book!!


And here's a nice article you should read:
http://en.wikipedia.org/wiki/Sarcasm


Yup - very good pointer there. Amazing what you can learn by spending
a few hours with wikipedia.

Nov 15 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: lawrence | last post by:
I posted before, but have now narrowed my problem down to this method. At the start of the method, I test to make sure that I have a resource, a pointer to data returned from a database. This test...
52
by: Douglas Garstang | last post by:
I can't believe I've been trying to work this out for hours now, and I can't believe I couldn't find someone asking for a similar solution in the newsgroups. No wonder I hate C so much, and every...
16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
49
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
17
by: matevzb | last post by:
I've ran into some fishy code that, at first glance, is buggy, but it seems to work correctly and none of the compilers I've tried (five so far, on various systems) gives any warnings. The code:...
27
by: rocco.rossi | last post by:
I've been trying to write a function capable of checking if a pointer value is set to NULL, and if it isn't, of deallocating and setting it's value to NULL regardless of the pointer's type. I...
2
by: Giorgos Keramidas | last post by:
On Sun, 05 Oct 2008 18:22:13 +0300, Giorgos Keramidas <keramida@ceid.upatras.grwrote: My apologies. I should have been less hasty to hit `post'. If showtext() is passed a null pointer, it may...
41
by: simonl | last post by:
Hi, I've been given the job of sorting out a crash in our product for which we have the crash information and an avi of the event (which can't possibly match but more of that later...) (btw this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.