:: Re: [DNG] Making sense of C pointer…
Startseite
Nachricht löschen
Nachricht beantworten
Autor: KatolaZ
Datum:  
To: Edward Bartolo
CC: dng
Betreff: Re: [DNG] Making sense of C pointer syntax.
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.
>


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. 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 ]