public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: Pointers to int
       [not found] ` <20051028160530.GE6673@opaque.pepe>
@ 2005-10-28 18:22   ` ework0
  2005-10-28 18:31     ` Kevin P. Fleming
  2005-10-28 18:58     ` corey taylor
  0 siblings, 2 replies; 3+ messages in thread
From: ework0 @ 2005-10-28 18:22 UTC (permalink / raw)
  To: Christoph Bussenius, gcc-help

Hello,


Christoph Bussenius wrote:

>On Fri, Oct 28, 2005 at 07:19:26AM -0600, Fabio Andres Miranda wrote:
>  
>
>>Can anyone explain to the list how this pointers to int work:
>>   int *p;
>>   p = (int *)(array);
>>   for (i = 0; i < arraysize - 1; i += 4)
>>       *p++ = j - 8;
>>   *p = 0x0;
>>
>>P is defined as a pointer to a int. Then, it points to (the beginning ? 
>>) a char array.
>>What is the result of perform the instruction: *p++; ?
>>    
>>
>
>As postincrement (++) has higher priority than dereference (*), p++ is
>what will be evaluated first.  So p will be incremented by the size of
>an integer (probably 4).  After that, p will point to the second integer
>of the array, i. e. array[1].  Now you use * to access that very
>location and store j-8 there.
>
>  
>
Regarding this issue, I understand the priorities of operands, I dont 
have clear what does the '++' operator increments: a. The memory 
location (ej: If it was 0x01, now it is 0x02); b. The value contained in 
the memory location p is point to?

Can anyone provide a simpler illustrative examples regarding this 
operations? I think it would be great for the records of the list,

Best regards,

ework0

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Pointers to int
  2005-10-28 18:22   ` Pointers to int ework0
@ 2005-10-28 18:31     ` Kevin P. Fleming
  2005-10-28 18:58     ` corey taylor
  1 sibling, 0 replies; 3+ messages in thread
From: Kevin P. Fleming @ 2005-10-28 18:31 UTC (permalink / raw)
  To: gcc-help

ework0 wrote:

> Regarding this issue, I understand the priorities of operands, I dont 
> have clear what does the '++' operator increments: a. The memory 
> location (ej: If it was 0x01, now it is 0x02); b. The value contained in 
> the memory location p is point to?
> 
> Can anyone provide a simpler illustrative examples regarding this 
> operations? I think it would be great for the records of the list,

This is basic C knowledge, not in any way specific to GCC. There are 
tons of C reference books and web sites you can use to learn how it works.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Pointers to int
  2005-10-28 18:22   ` Pointers to int ework0
  2005-10-28 18:31     ` Kevin P. Fleming
@ 2005-10-28 18:58     ` corey taylor
  1 sibling, 0 replies; 3+ messages in thread
From: corey taylor @ 2005-10-28 18:58 UTC (permalink / raw)
  To: ework0; +Cc: gcc-help

Since you ask, it's not really a proper mechanism:

You should use the increment operators for the destination pointer in
the for statement instead of using the post increment operator with
the dereference.

Also, that whole statement doesn't quite make sense.  You probably
want to traverse 0 - (arraysize - 1) but you're actually checking for
less than (arraysize - 1) which mean you never get there.

Also, is the array a character array?  If so, then the increments
work, otherwise the arraysize might not represent the actual memory
block size (say for short it is 2 bytes each item).

Corey

On 10/28/05, ework0 <ework0@gmail.com> wrote:
> Hello,
>
>
> Christoph Bussenius wrote:
>
> >On Fri, Oct 28, 2005 at 07:19:26AM -0600, Fabio Andres Miranda wrote:
> >
> >
> >>Can anyone explain to the list how this pointers to int work:
> >>   int *p;
> >>   p = (int *)(array);
> >>   for (i = 0; i < arraysize - 1; i += 4)
> >>       *p++ = j - 8;
> >>   *p = 0x0;
> >>
> >>P is defined as a pointer to a int. Then, it points to (the beginning ?
> >>) a char array.
> >>What is the result of perform the instruction: *p++; ?
> >>
> >>
> >
> >As postincrement (++) has higher priority than dereference (*), p++ is
> >what will be evaluated first.  So p will be incremented by the size of
> >an integer (probably 4).  After that, p will point to the second integer
> >of the array, i. e. array[1].  Now you use * to access that very
> >location and store j-8 there.
> >
> >
> >
> Regarding this issue, I understand the priorities of operands, I dont
> have clear what does the '++' operator increments: a. The memory
> location (ej: If it was 0x01, now it is 0x02); b. The value contained in
> the memory location p is point to?
>
> Can anyone provide a simpler illustrative examples regarding this
> operations? I think it would be great for the records of the list,
>
> Best regards,
>
> ework0
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-10-28 18:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <4362255E.90303@racsa.co.cr>
     [not found] ` <20051028160530.GE6673@opaque.pepe>
2005-10-28 18:22   ` Pointers to int ework0
2005-10-28 18:31     ` Kevin P. Fleming
2005-10-28 18:58     ` corey taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).