:: Re: [DNG] Making sense of C pointer…
Top Pagina
Delete this message
Reply to this message
Auteur: Hendrik Boom
Datum:  
Aan: dng
Onderwerp: Re: [DNG] Making sense of C pointer syntax.
On Thu, Mar 31, 2016 at 03:18:17PM +0100, KatolaZ wrote:
> On Thu, Mar 31, 2016 at 03:12:32PM +0200, Edward Bartolo wrote:
> > Hi, thanks for taking time to reply,
> >
> > KatolaZ wrote:
> > >> c) type **ss; // declare a pointer to pointer. System only allocates
> > >> space for one address
> >
> > > C pointers are *always* one variable, precisely a variable large
> > > enough to store a memory address on the current architecture.
> >
> > I think, I did not understand what you want to say. As far as I can
> > imagine a pointer to pointer is something like this:
> > [pointer1]------>[pointer2]------->[data] int the case of data** dt.
> >
> > OR
> >
> > [pointer1]------>[pointer2] in the case of void** ptr.
> >


Your example would be correct *only if* and pointer1, pointer2
actually contained proper addresses of memory instead of containing
uninitialized junk or NULL.

And that, I think, is what KatolaZ is tryng to point out.
>
> Nope. This is totally wrong. What you have in memory after a
> declaration:
>
> type *a;
>
> is just *one variable*, namely a variable able to contain a memory
> address.


Yes. It is only when you actually assgn a pointer to a that your
pointer----->data

diagram becomes valied.

-- hendrik

> When you declare:
>
> type **b;
>
> you still have exactly *one variable* allocated in memory, namely one
> variable able to contain a memory address, and not *two variables* as
> in your example above. This does not depend on what "type" is, so
> each of the following declarations:
>
> int *a;
> double ***b;
> void ****c;
> myowntype **d;
>
> will result in the allocation of exactly *one variable*, namely a
> variable large enough to contain a pointer (i.e., a memory address). I
> don't want to confuse you, but in my example myowntype might also be
> declared as:
>
> typedef char*** myowntype;
>
> and still the declaration:
>
> myowntype **d;
>
> will reserve exactly one variable in memory, namely a variable large
> enough to contain a memory address.
>
> The declaration is used only by the compiler to understand what is the
> implicit semantic of the pointer arithmetic to be used with that
> pointer, and to check that you are not palying nastly with it, so that
>
> int *p;
> ...
> p += 1;
>
> assigns to p the address of the memory location which is 1*sizeof(int)
> bytes after the old address stored into p, while:
>
> double *p;
> ...
> p +=2;
>
> will assign to p the address of the memory location which is
> 2*sizeof(double) bytes after the old address stored in p.
>
> You have not broken the pointers spell, yet. I warmly suggst you to
> read a good introduction to C pointers, but the only thing that comes
> to my mind is the Kernighan and Ritchie, which I admit is not the
> easiest book around (although it is certainly the best on the
> subject).
>
> My2Cents
>
> KatolaZ
>
> --
> [ Enzo Nicosia aka KatolaZ --- GLUG Catania -- Freaknet Medialab ]
> [ me [at] katolaz.homeunix.net -- http://katolaz.homeunix.net -- ]
> [ GNU/Linux User:#325780/ICQ UIN: #258332181/GPG key ID 0B5F062F ]
> [ Fingerprint: 8E59 D6AA 445E FDB4 A153 3D5A 5F20 B3AE 0B5F 062F ]
> _______________________________________________
> Dng mailing list
> Dng@???
> https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng