:: Re: [DNG] Making sense of C pointer…
Página Principal
Delete this message
Reply to this message
Autor: Emiliano Marini
Data:  
Para: Rainer Weikusat
CC: dng
Assunto: Re: [DNG] Making sense of C pointer syntax.
+1

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'




On Wed, Mar 30, 2016 at 6:04 PM, Rainer Weikusat <
rainerweikusat@???> wrote:

> Edward Bartolo <edbarx@???> writes:
>
> > Hi, thanks for your help.
> >
> > So: [ I am recalling this from memory to test where I am ]
> > a) type* pp; // declare a pointer pp
> > b) *pp = RHS; // assign RHS to the data pointed to by pp
> > c) type **ss; // declare a pointer to pointer. System only allocates
> > space for one address
>
> (1) Background
> --------------
>
> The C type system is built around the idea that certain kind of types can
> be
> derived from already existing types. These are pointers, arrays (and
> functions, but I'm going to omit them for simplicity). Eg, assuming type
> identifies and existing type,
>
> type *pt
>
> declares a pointer to objects of type type and
>
> type at[3];
>
> declares an array of objects of type type. When an object is defined,
> the system allocates storage for it in some suitable location, eg, on
> the stack or by reserving a register.
>
> (2)Typedef names
> ----------------
>
> It's possible to create a new name for an existing type with the help of
> typedef, eg, after
>
> typedef int *pint;
>
> pint can be used as type name to mean 'pointer to int'. Eg,
>
> pint pi;
>
> would define an object of type 'pointer to int' (the compiler will
> allocate storage for).
>
> Pulling this together
> ---------------------
>
> Assuming pint is declared as above. As per (1), new types can now
> derived from it, eg,
>
> pint *ppi; /* "Dear customer ..." */
>
> This defines a "pointer to pint", hence, the system allocates storage
> for one. But that's really the same as
>
> int **ppi;
>
> which is handled in exactly the same way: An object with a pointer type
> derived from some existing type is defined. Hence, storage for a pointer
> is reserved. That the existing type is itself a pointer type doesn't
> matter, for the given purpose, it's just 'some type'.
>
>
> > f) strings are character arrays, so they obey the rules governing arrays
>
> They're really pointers to character arrays. Because an array is
> converted to a pointer to its first element most of the time, a
> character array, say
>
> char chars[] = "12345";
>
> can be used as if it had been declared as a pointer. 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. In contrast to this.
>
> char *chars = "12345";
>
> causes the compiler to allocate storage for a writable char * and
> initialize that with the address of a string literal which is not
> required to be (and usually won't be) writeable.
> _______________________________________________
> Dng mailing list
> Dng@???
> https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng
>