Hello !
I am struggeling with understanding how this works :
The results are like here :
From tutorial like here :

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.
The results :
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
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 :

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