I am struggling with understanding how this works:
#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;
}
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:
https://i.postimg.cc/FRZbkLLR/obraz-2023-10-18-050416732.png[
^]
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 another 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
char
s.
#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 does it go every 8 bytes ?
Same goes with the first example. I am struggling 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.
I'm just very confused how these bytes work and how does he know that when I write matrix + 1, it goes by 4 bytes. When I have char * arr it is like pointer to a pointer because address of arr is a pointer to the inside of arr which is also a pointer and this pointer points to the address of a value like "G". So two pointers but one is 1 byte per character, and another pointer is 1 byte ahead but the size of pointers should be 8 bytes ... I don't get it at all how it works and how the arithmetics works there.
What I have tried:
I tried reading about arithmetics and I understood something about
sizeof(int)
or
sizeof(int *)
but I don't get it at all. Same goes with that pointer should be either 4 bytes or 8 bytes, but in my case, it is 1 byte per character for pointer ... or maybe I don't understand the notation such as matrix + i, but I understand that the address of matrix is shifted by + i with
sizeof
. The difference between two addresses of matrix is 8 bytes so +1 is shifting by 8 bytes but when I write
*(matrix+i)
, the difference between
*(matrix+0)
and
*(matrix+1)
is only 5 bytes (because 4 characters + \0 character) the other
*(matrix+2)
is 6 bytes difference.