Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

C [C] byte address range.

Xenon02

New Coder
Hello !

I am struggeling with understanding how this works :

Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int rows = 2;
    int columns = 5;
    int **matrix = (int **) malloc(rows * sizeof(int *));
    matrix[0] = (int *) malloc(rows * columns * sizeof(int));
    for (int i = 1; i < rows; i++)
    {
        matrix[i] = matrix[0] + i * columns;
        //printf("%p", *matrix[i]);
    }
     for (int i = 0; i < rows; i++)
    {
      
        printf("Indeks : %d, Address of (matrix) : %p\n", i, matrix+i);
        printf("Indeks : %d, Address of (*matrix) : %p\n", i, *(matrix+i));
    }
    
    return 0;
}


The results are like here :
Indeks : 0, Address of (matrix) : 0x55eff083e2a0
Indeks : 0, Address of (*matrix) : 0x55eff083e2c0
Indeks : 1, Address of (matrix) : 0x55eff083e2a8
Indeks : 1, Address of (*matrix) : 0x55eff083e2d4

From tutorial like here :

1697597255596.png

It shows that the addres of matrix should change every 4 bytes but it changes every 8 bytes in my code and it uses malloc. But for the *matrix if changes every 4 bytes and it is also malloc "array", so why in one malloc it changes every 8 bytes and in other 4 bytes they are both int and int value takes 4 bytes.

So how does he know that when I write matrix + 1 he know that I want to move 4 bytes to another number which is located in the picture 604 this is anotherr number. Like how does he convert it into 8 bytes or 4 bytes when both mallocs are pointers (I heard that pointer takes 8 bytes but (int*) malloc is also a pointer but it takes 4 bytes not 8)

Or another example with chars.

Code:
#include <stdio.h>

int main()
{
 
    char* arr[3] = { "geek", "Geeks", "Geeksfor" };
 
    for (int i = 0; i < 3; i++) {
        printf("Indeks : %d, Address of (arr) : %p\n",i , *(arr+i));
        printf("Indeks : %d, Address of (&arr) : %p\n",i , &arr[i]);
    }
 
    return 0;
}

The results :

Indeks : 0, Address of (arr) : 0x55ae7f302008
Indeks : 0, Address of (&arr) : 0x7fff60e25ef0
Indeks : 1, Address of (arr) : 0x55ae7f30200d
Indeks : 1, Address of (&arr) : 0x7fff60e25ef8
Indeks : 2, Address of (arr) : 0x55ae7f302013
Indeks : 2, Address of (&arr) : 0x7fff60e25f00

Now here also 4 bytes difference or more when looking at address arr (char has only 1 byte when there are 4 letters it takes 4 bytes logical). But the &arr has 8 bytes. Why ?

It doesn't make sense when I write &arr + 1 knowing that char is 1 byte then why it goes every 8 bytes ?
Same goes with the 1st example I am struggeling to understand when I type *matrix + 1 it will go 4 bytes but when I type matrix + 1 it will go 8 bytes how does he know when both malloc addreses are pointers which means they should be 8 bytes adresses
 
PS.
For the second example when I write size of(arr) it will say it has 8 bytes although for arr[0] it takes 4 bytes because there are 4 addresses for 4 letters. But it says it's size is 8 bytes what ??? Even though if I write (arr + 1) it will go 1 byte (for example arr[0] is 55ae7f302008 so are[1] 55ae7f302009) and so it is not 8 byte. If it was 8 byte size it would go 8 bytes. If I write arr +1 it would go 8 byte and not 1. Not only &arr is a pointer to the address *arr but also *arr is a pointer to a value. Because char arr is a array of pointers.
Two pointers but one is 8 byte and second is one byte but the systems says it is 8 byte.

I don't get it.
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom