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.

I don't understand multi dimensional arrays using pointers and pointers in arrays

Xenon02

New Coder
I tried to understand these two codes :


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;
}
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 :


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).
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom