[Solved] Need help in clarifying some memory references/pointers in C


Let’s see if I can take a go at this. First off, I recommend getting a simple, free c compiler to try these examples out. Most of these examples can be tested that way.

  1.   var -> a[var -> count]     vs   &var -> a[var -> count]

Beware that ‘var’ is a pointer to a data structure. We can assume that the structure at the very least has two elements: an array of ‘a’, and a value ‘count’. With this in mind “var->a[var->count]” represents the VALUE of the ‘count'(plus 1) element of the ‘a’ array within the structure pointed to by var. I say the ‘count’ (+1) value because the first element of the array is a[0], as you know. The term “&var->a[var->count]” represents the ADDRESS of the ‘count'(plus 1) element of the ‘a’ array within the structure pointed to by var.

  2.  p = &x[i]

This is setting p to the ADDRESS of the ith value of the ‘x’ array. ‘&varname’ should be interpreted as (ADDRESS of varname).

      p = a[i]   

Here ‘p’ is set to the VALUE of the ith value of the ‘a’ array.

      &p = &a[i]` 

This does not work. First the back-tick means nothing. Also, the attempt is to set the address of the pre-defined variable ‘p’ to a new value. ‘p’ itself is a variable that can hold many different values, but the ADDRESS of p is NOT variable. In case you’re
wondering, the expression should be read as “the address of p is equal to the address of a[i]”.

  3.   var->*mem     

Looks terribly wrong. I would say this is an attempt to write…

       *var->mem      

Which is a the VALUE of some variable pointed to by ‘var->mem’. In this case, ‘var’ is a pointer to a structure, which contains the pointer ‘mem’. In code it might look like this:

               int j;

               struct example
               {
                  int *mem;  // mem is an integer pointer.
               } x;

               struct example *var;

               j = 3;  // some value.

               x.mem = &j; // mem is set to point to j mem=ADDRESS of j;

               var = &x;   // var is pointing to the structure 'x'

               printf("%d\n",*var->mem);  // should print "3"

This next example is the same mistake as the first example.

       var -> *(mem) = 2;  

The parenthesis do nothing for you. Good luck getting this to compile. That said, there is nothing stopping you from doing this:

       *var->mem = 2;

but in the above code example, that’s the same as ‘j=2’

    *(*var)->mem     

This example is really tricky. It reads as the value of the value pointed to by var, which itself is a pointer to a structure containing ‘mem’. This implies that var would be declared as

                struct ***var; 

var is thus a pointer to a pointer to a pointer. I created a code snippet which compiles and illustrates how this could be used, but I don’t recommend actually using this level of indirection in your own code, especially if there’s any chance someone will read it. If you do, prepare to be hunted down and burnt at the stake like a witch.

#include <stdio.h>

struct lkj
{
    int *mem;
};

int main(void)
{
    int j;

    struct lkj bob;

    struct lkj ***var,**floyd,*sam;

    j = 3;
    bob.mem = &j;    
    sam = &bob;
    floyd = &sam;
    var = &floyd;

    printf("bob.mem = 0x08%lX *bob.mem=%d j=%d\n",
                (unsigned int)bob.mem,
                *bob.mem,j);

    printf("&bob = 0x08%lX\n",(unsigned int)&bob);

    printf("sam = 0x08%lX &sam=0x%08lX\n",
            (unsigned int)sam,(unsigned int)&sam);
    printf("floyd = 0x08%lX &floyd=0x%08lX\n",
            (unsigned int)floyd,(unsigned int)&floyd);

    printf("var = 0x%08lX *(*var) = 0x%08lX\n",
            (unsigned int)var,(unsigned int)*(*var));

    printf("(*(*var))->mem = 0x%08lX *(*(*var))->mem=%d\n",
            (unsigned int)(*(*var))->mem,
            *(*(*var))->mem);

    printf("(**var)->mem = 0x%08lX *(**var)->mem=%d\n",
            (unsigned int)(**var)->mem,
            *(**var)->mem);

    return 0;
}

solved Need help in clarifying some memory references/pointers in C