:: Re: [DNG] Making sense of C pointer…
Forside
Slet denne besked
Besvar denne besked
Skribent: aitor_czr
Dato:  
Til: Emiliano Marini, Rainer Weikusat, dng
Emne: Re: [DNG] Making sense of C pointer syntax.
Hi,

On 03/31/16 13:38, Emiliano Marini <emilianomarini82@???> wrote:
> Besides, * and [] are interchangeable. You can define a string as an array
> and use it later as a pointer:
>
> char s[] = "hola";
> char x = s[1];     // Here x = 'o'
> char y = *(s+2);   // Here y = 'l'

>
> And vice versa:
>
> char *s = "hola";
> char x = s[0];     // Here x = 'h'
> char y = *(s+3);   // Here y = 'a'


This is right, and the following sintax are analogous:

      *(p+i) <--->  p[i]


*(*(p+i)+j) <---> p[i][j]

This last one in the case of a double pointer.

> On Wed, Mar 30, 2016 at 6:04 PM, Rainer Weikusat <
> rainerweikusat@???> wrote:
>
> There's one
> >important difference:
> >
> >char chars[] = "12345";
> >
> >cause the compiler to reserve six bytes (five chars + terminating 0) in
> >some location the program can write to. Afterwards,
> >
> >*chars = R;
> >
> >could be used to change the string to R2345.


This is because

*chars <---> *(chars+0) <---> chars[0]

are analogous. So, chars[0] changes from 1 to R.

Aitor.