linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Pointers to int
@ 2005-10-28 13:19 Fabio Andres Miranda
  2005-10-28 16:05 ` Christoph Bussenius
  2005-10-29 15:44 ` Glynn Clements
  0 siblings, 2 replies; 4+ messages in thread
From: Fabio Andres Miranda @ 2005-10-28 13:19 UTC (permalink / raw)
  To: linux-c-programming

Hello,

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++; ?
First, what is it? It adds 1 to what?

Thanks for a detailed explanation,

fabolo

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

* Re: Pointers to int
  2005-10-28 13:19 Pointers to int Fabio Andres Miranda
@ 2005-10-28 16:05 ` Christoph Bussenius
  2005-10-28 16:09   ` Christoph Bussenius
  2005-10-29 15:44 ` Glynn Clements
  1 sibling, 1 reply; 4+ messages in thread
From: Christoph Bussenius @ 2005-10-28 16:05 UTC (permalink / raw)
  To: Fabio Andres Miranda, linux-c-programming

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.

HTH,
Christoph

-- 
``There's no dark side of the moon, really
Matter of fact, it's all dark''

--Pink Floyd

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

* Re: Pointers to int
  2005-10-28 16:05 ` Christoph Bussenius
@ 2005-10-28 16:09   ` Christoph Bussenius
  0 siblings, 0 replies; 4+ messages in thread
From: Christoph Bussenius @ 2005-10-28 16:09 UTC (permalink / raw)
  To: Christoph Bussenius; +Cc: Fabio Andres Miranda, linux-c-programming

On Fri, Oct 28, 2005 at 06:05:30PM +0200, Christoph Bussenius wrote:
> 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.

Sorry, I was mistaken.  As the result of a POSTincrement is the value
before incrementing, the result will be like:
*p = j-8;
p++;

Christoph

-- 
``There's no dark side of the moon, really
Matter of fact, it's all dark''

--Pink Floyd

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

* Re: Pointers to int
  2005-10-28 13:19 Pointers to int Fabio Andres Miranda
  2005-10-28 16:05 ` Christoph Bussenius
@ 2005-10-29 15:44 ` Glynn Clements
  1 sibling, 0 replies; 4+ messages in thread
From: Glynn Clements @ 2005-10-29 15:44 UTC (permalink / raw)
  To: Fabio Andres Miranda; +Cc: linux-c-programming


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.

When used as an expression, an array variable evaluates to a pointer
to its first element.

> What is the result of perform the instruction: *p++; ?

The expression "*p++ = j - 8" will store j-8 in the (int) location
referenced by p, then increment p so it points to the next location
(i.e. increments the address by sizeof(int)).

> First, what is it? It adds 1 to what?

It evaluates to the p, incrementing p as a side-effect.

It "adds 1" to p in the sense that p will point to the next int.
Adding a value to a pointer scales the value by the size of the type
being pointed to, e.g. if p has type "pointer to T" for some type T,
then the expression:

	p + n

is equivalent to:

	(T*)((char*)p + n * sizeof(T))

-- 
Glynn Clements <glynn@gclements.plus.com>

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

end of thread, other threads:[~2005-10-29 15:44 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-10-28 13:19 Pointers to int Fabio Andres Miranda
2005-10-28 16:05 ` Christoph Bussenius
2005-10-28 16:09   ` Christoph Bussenius
2005-10-29 15:44 ` Glynn Clements

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 NNTP newsgroup(s).