Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pointer dereference

Reply
Thread Tools

pointer dereference

 
 
somenath
Guest
Posts: n/a
 
      08-09-2007
Hi All ,
I have some doubts fro the following program.

#include<stdio.h>

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

The Output of the program is
4
Could any body let me know how it prints 4?
Regards ,
Somanath

 
Reply With Quote
 
 
 
 
yifanernei
Guest
Posts: n/a
 
      08-09-2007
p is a point, pointed to type "char*"
"++p" move sizeof(char *) bytes

 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      08-09-2007
somenath <(E-Mail Removed)> writes:

> Hi All ,
> I have some doubts fro the following program.
>
> #include<stdio.h>
>
> int main(void)
> {
> char **p = 0;
> printf("%d\n", ++p);
> return 0;
> }
>
> The Output of the program is
> 4
> Could any body let me know how it prints 4?
> Regards ,
> Somanath
>


You will probably get a lot of clever answers, but I suspect you mean
"why it prints 4"?

Look carefully at this line:

printf("%d\n", ++p);

What does %d mean? Look it up.

What is p? p is a pointer to a char pointer. What does ++p do? It makes
p point to the NEXT char pointer. How long is a char pointer on your
machine? Probably 32 bits or 4 bytes.

So, if p was 0, then making it point to the NEXT pointer stored in
memory adds 4 to its value.

0+4=4.

As usual with this type of level, play around in a debugger and it will
all become clear.

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-09-2007
somenath wrote:
>
> Hi All ,
> I have some doubts fro the following program.
>
> #include<stdio.h>
>
> int main(void)
> {
> char **p = 0;
> printf("%d\n", ++p);
> return 0;
> }
>
> The Output of the program is
> 4
> Could any body let me know how it prints 4?


Sheer luck.
p is initialised as a null pointer.
(1 + NULL) is undefined.
Printing the value of a pointer type by using %d,
is also undefined.

--
pete
 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      08-09-2007
somenath wrote:
> Hi All ,
> I have some doubts fro the following program.


Not unreasonably. It's so horrid...

> #include<stdio.h>
>
> int main(void)
> {
> char **p = 0;


p is a pointer to pointer to char.
p is initialised to a null pointer. (Note that the internal
representation of a null pointer is not guaranteed to be all zeroes)

> printf("%d\n", ++p);


You increment p, moving it on by the size of a pointer to char.

You then lie to printf telling it you are passing an int.

> return 0;
> }
>
> The Output of the program is
> 4
> Could any body let me know how it prints 4?


The machine appears to
a) have a flat address space
b) represent pointers in a compatible form to ints
c) represent a null pointer as all zeroes
d) allow incrementing a null pointer
e) have 4-"cell" pointers to char

None of the above are guaranteed to be true.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-09-2007
somenath wrote:
>
> I have some doubts fro the following program.
>
> #include<stdio.h>
>
> int main(void)
> {
> char **p = 0;
> printf("%d\n", ++p);
> return 0;
> }
>
> The Output of the program is
> 4
> Could any body let me know how it prints 4?


It can print anything it desires, or explode. All these are
satisfactory examples of the undefined behaviour it exhibits. A
pointer is not a signed integer.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Charles Richmond
Guest
Posts: n/a
 
      08-09-2007
CBFalconer wrote:
> somenath wrote:
>> I have some doubts fro the following program.
>>
>> #include<stdio.h>
>>
>> int main(void)
>> {
>> char **p = 0;
>> printf("%d\n", ++p);
>> return 0;
>> }
>>
>> The Output of the program is
>> 4
>> Could any body let me know how it prints 4?

>
> It can print anything it desires, or explode. All these are
> satisfactory examples of the undefined behaviour it exhibits. A
> pointer is not a signed integer.
>


But since this program displays undefined behavior, it
has some probability, *no* matter how infinitesimal that
probability may be, of doing exactly what the programmer
wants.

This is the only chance that programmers have... who know
so little about C. (Slim and none, and Slim just left town.)

--
+----------------------------------------------------------------+
| Charles and Francis Richmond richmond at plano dot net |
+----------------------------------------------------------------+
 
Reply With Quote
 
Ravishankar S
Guest
Posts: n/a
 
      08-10-2007
Hi,

on a 32 bit machine all "pointer" vars are 32 bits (or 4 bytes). So a
"pointer to pointer" will always be incremented by 4 (pointer arithmetic
says that pointer is incremented by size the "type pointed to"). We can
also generalise that all pointers to pointer and pointers to pointers to
pointers and so will always be incremented by sizeof(pointer type) on that
machine.

Regards,
Ravishankar



"somenath" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hi All ,
> I have some doubts fro the following program.
>
> #include<stdio.h>
>
> int main(void)
> {
> char **p = 0;
> printf("%d\n", ++p);
> return 0;
> }
>
> The Output of the program is
> 4
> Could any body let me know how it prints 4?
> Regards ,
> Somanath
>



 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      08-10-2007
Ravishankar S wrote, On 10/08/07 09:59:

Please do not top-post, your reply belongs under or intermixed with the
text you are replying to. See almost all posts in this group for examples.

> on a 32 bit machine all "pointer" vars are 32 bits (or 4 bytes).


Define a 32 bit machine. Address, databus, registers and ALU are not
always all the same size. Many compilers used on the 80386DX (which
Intel said was a 32 bit processor) used 16 bit pointers. Other
processors have a 32 bit byte, so a 32 bit int is 1 byte.

> So a
> "pointer to pointer" will always be incremented by 4


See above for why this may not be true.

> (pointer arithmetic
> says that pointer is incremented by size the "type pointed to").


This is true.

> We can
> also generalise that all pointers to pointer and pointers to pointers to
> pointers and so will always be incremented by sizeof(pointer type) on that
> machine.


This is more specific than your previous statement!

<snip material that was being replied to.>
--
Flash Gordon
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      08-10-2007
Ravishankar S wrote:

[Please don't top post]

> "somenath" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>> Hi All ,
>> I have some doubts fro the following program.
>>
>> #include<stdio.h>
>>
>> int main(void)
>> {
>> char **p = 0;
>> printf("%d\n", ++p);
>> return 0;
>> }
>>
>> The Output of the program is
>> 4
>> Could any body let me know how it prints 4?


> Hi,
>
> on a 32 bit machine all "pointer" vars are 32 bits (or 4 bytes).


This is not guaranteed, though I don't know of a contrary case.

> So a "pointer to pointer" will always be incremented by 4


Four bytes, not just four. Major difference.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function pointer dereference security Nyang A. Phra C Programming 13 12-20-2007 02:03 AM
Function pointer dereference security Nyang A. Phra C Programming 0 11-11-2007 04:52 AM
pointer dereference somenath C Programming 34 07-18-2007 01:33 PM
Can't dereference pointer to stack? TuAmigoFiel@gmail.com C++ 9 02-11-2006 06:31 AM
NULL Pointer Dereference Denis Palmeiro C Programming 10 07-16-2003 12:33 PM



Advertisments