Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm back with this example again with a question.
C#
#include <stdio.h>

int main()
{
 
    char* arr[3] = { "geek", "Geeks", "Geeksfor" };

    for (int i = 0; i < 3; i++) {
        printf("*(arr+%d) : %p\n", i, *(arr+i));
    }
    printf("\n");
    for (int i = 0; i < 3; i++) {
        printf("  arr+%d  : %p\n", i, arr+i);
    }
    printf("\n");
    for (int i = 0; i < 3; i++) {
        printf(" &arr+%d  : %p\n", i, &arr+i);
    }
    printf("\n");
 
    return 0;
}


And I had this answer:

  1. printf("Indeks : %d, Address of (arr) : %p\n",i , *(arr+i));
  2. printf("Indeks : %d, Address of (&arr) : %p\n",i , arr+i);
  3. printf("Indeks : %d, Address of (&arr) : %p\n",i , &arr+i);

The tag arr + i is the address of the pointer at location i in the array arr. So pointer 0, 1 and 2 (3 items).

  1. You take the content of arr + i, which is a pointer which points to a string of characters and will be allocated by the compiler. Its actual value has nothing to do with pointer arithmetic, it is purely a random address as allocated by the compiler.

  2. You take the actual pointer at arr + i and print its address. And since it is one of three pointers, they will each be 8 bytes distant from their predecessor.

  3. You are printing the address of the array itself, plus 0, 1 and 2. And since the array is 3 pointers in size, this address will increase by the size of the array itself.


But why does it shift 3*8 bytes ?
Doesn't it work like that each element of array has a specific address like here? : https://i.postimg.cc/k5Zv8mpk/obraz-2023-10-18-210634239.png[^] in my case &arr+i shows only the sum of 3 arrays which is 3*8 bytes why ?

What if I used **ptr? Like here char**ptr = &arr ??? If I type ptr + 1, it shifts also by 24 bytes? How does he know that he must shift by 24 bytes or rather how does he know he has to go through 3 elements of arrays?

Example with:

If I had a normal array like val[2], then typing val+1 will shift it by 4 bytes. What is the arithmetic behind that &arr is 3bytes*sizeof(char**) ? And the other one that usually is 8 bytes or 4 bytes. Because I believed that &arr + 1 is an address of arr + 1 and &arr + 0 is an address of arr + 0. So what is the address of arr + 1 ?

What I have tried:

I tried understanding it from many websites ;>
Posted
Updated 19-Oct-23 5:11am
v4

1 solution

Lets take a look at the expressions one by one.

We start with char *arr[3] = { "geek", "Geeks", "Geeksfor" }; We've declare arr as an array of 3 pointers to char. For a 64 bit CPU, each pointer requires 8 bytes, so the extent of arr is 24 bytes.

Next we examine the value of *(arr + i). Since arr is of type pointer to char, the expression arr + i also has the type pointer to char. The compiler knows that the size of a pointer is 8 bytes, so the value of (arr + i) is the value of arr + (i * sizeof( char *)). This is exactly equivalent of writing arr[i] (weird C aside: its also equivalent of i[arr], because - math). When we dereference that value, we get the value of the pointer to the required string.

Next we have arr+i. In this case, we are getting the pointer to the i'th element of arr. As noted above, a pointer takes 8 bytes, so each pointer in the array is 8 bytes further along in memory.

Lastly we have &arr + i. Again, arr is of type array 3 of pointer to char. In this case, we are moving ahead in memory the sizeof(arr), which is 24 bytes.

Addendum: If you try this in 32 bit mode, then the size of a pointer is 32 bits, 4 bytes, and the offsets and sizes of objects are adjusted by the compiler as needed.
 
Share this answer
 
v2
Comments
Member 16116753 18-Oct-23 18:43pm    
I just found out I was a bit stupid as well. And that Assigning *type**)ptr to &arr will give an error because it is an 3*8 bytes or something it should be (type**)ptr = arr.

Because it works the same with normal array like value[5]. If I write value + i I get the value of the addres, if I write *(value + i) I get the value from that addres, and if I write &value+i it is the whole array just like with the char *arr[3] and it's &arr+i.

The difference between them is that in char *arr[] I can get two addresses, one is the pointer address in arr+i and the addres of the value which is *(arr+i), in normal aaray like int value[] if I type value+i i get the addres value and that's all. In *(value+i) I get the value only and not the address.

I don't know if I am quite correct. But that's all ;>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900