I tried to understand these two codes :
This is a one dimensional array.
And I am confused of how malloc works and why it is also a pointer (int *) ?? Because I had more simple examples such as this one :
The pointer here ptr is pointing at the address of x[2] which is written as &x[2]. So why now the pointer points at the malloc which is also a pointer ?
In the Two-dimensional arrays I saw this example :
I don't understand what is written here. Why matrix is used twice ? Once as **matrix and one as matrix[0] ? And why **matrix changed into matrix[0] ?
Isn't **matrix the same as matrix[][] ??
Like in this example :
Also I don't understand arrays of pointers like here :
the arrays is in char and char can only store a single character so how ? arr[0] shows geek and not just a letter "g" ? And how does the *arr know what is the address of "geek" ?
This example shows :
That in the arrays of pointer I have to put the address of the variable but in char *arr I just put a string and not an addres like above (like &var instead in char arr there is "geek" and not address of this string).
Code:
int *pv = (int*) malloc(5 * sizeof(int));
for(int i=0; i<5; i++) {
pv[i] = i+1;
}
This is a one dimensional array.
And I am confused of how malloc works and why it is also a pointer (int *) ?? Because I had more simple examples such as this one :
Code:
#include
int main() {
int x[5] = {1, 2, 3, 4, 5};
int* ptr;
// ptr is assigned the address of the third element
ptr = &x[2];
printf("*ptr = %d \n", *ptr); // 3
printf("*(ptr+1) = %d \n", *(ptr+1)); // 4
printf("*(ptr-1) = %d", *(ptr-1)); // 2
return 0;
}
In the Two-dimensional arrays I saw this example :
Code:
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;
I don't understand what is written here. Why matrix is used twice ? Once as **matrix and one as matrix[0] ? And why **matrix changed into matrix[0] ?
Isn't **matrix the same as matrix[][] ??
Like in this example :
Code:
#include
int main() {
int i, x[6], sum = 0;
printf("Enter 6 numbers: ");
for(i = 0; i < 6; ++i) {
// Equivalent to scanf("%d", &x[i]);
scanf("%d", x+i);
// Equivalent to sum += x[i]
sum += *(x+i);
}
printf("Sum = %d", sum);
return 0;
}
Also I don't understand arrays of pointers like here :
Code:
#include
int main()
{
char* arr[3] = { "geek", "Geeks", "Geeksfor" };
for (int i = 0; i < 3; i++) {
printf("%s\n", arr[i]);
}
return 0;
}
the arrays is in char and char can only store a single character so how ? arr[0] shows geek and not just a letter "g" ? And how does the *arr know what is the address of "geek" ?
This example shows :
Code:
#include
int main()
{
// declaring some temp variables
int var1 = 10;
int var2 = 20;
int var3 = 30;
// array of pointers to integers
int* ptr_arr[3] = { &var1, &var2, &var3 };
// traversing using loop
for (int i = 0; i < 3; i++) {
printf("Value of var%d: %d\tAddress: %p\n", i + 1, *ptr_arr[i], ptr_arr[i]);
}
return 0;
}
That in the arrays of pointer I have to put the address of the variable but in char *arr I just put a string and not an addres like above (like &var instead in char arr there is "geek" and not address of this string).