-2

Good evening, quick question regarding the following code:

// Start of main
int i,j;
int row,col;

printf("Enter the values for row and col:\n");
scanf("%d%d",&row,&col);
int **arr=(int**)malloc(row*(sizeof(int*)));

for(i=0; i<row; i++)
{
 *(arr+i) = (int*)malloc(sizeof(int)*col);
}

// .. code after this snippet

Why am I able to use the variable arr as a pointer to a pointer and then re-use it as a pointer inside the for loop ?? Why do I not have to create two seperate variables ?

000
  • 26,951
  • 10
  • 71
  • 101
Shady Programmer
  • 794
  • 4
  • 9
  • 22
  • 3
    A pointer is a pointer is a pointer. It doesn't matter what it points to, it's still a pointer. – Some programmer dude Sep 09 '15 at 12:58
  • 4
    Do not cast the result of `malloc` and friends in C! – too honest for this site Sep 09 '15 at 12:59
  • 4
    Also, remember that you can use array indexing syntax with pointers too, and that `*(arr + i)` is equivalent to `arr[i]`, which often is more intuitive (and a little less to write). – Some programmer dude Sep 09 '15 at 12:59
  • What do you mena by "re-use"? You dereference it, but you can also assign a new value. You seem to missed some basic concepts - no offence! May I recommend to read a C book? Pointers are used in (almost) every non-trivial C program, so you really have to understand them well. Every good book treats this subject en detail. – too honest for this site Sep 09 '15 at 13:13
  • Olaf - It so happens that I'm actually reading "C Programming : A modern approach 2nd Edition" by K.N. King right now so thank you for your concern about my basics ... I'm also aware how important pointers are so thank you for that too. My confusion has risen by array notation and pointer arithmetic equivalent to it. I'm trying to find every possible scenario where one equals another because currently it's a big chaos in my head. Other than that I'm okay with most of the basics :) – Shady Programmer Sep 09 '15 at 14:45
  • You shouldn't allocate pointer-to-pointer lookup tables like these anyway. If that book told you to do so, you may have to consider burning it with fire. Instead, allocate a real 2D array in adjacent memory. `int (*arr)[row][col] = malloc(sizeof int[row][col]);`. – Lundin Sep 09 '15 at 15:07
  • Lundin - The code above wasn't an example in the book, it was my own experimenation combined with some research on stackoverflow. I wanted to create a 2D array with variable length of columns, I don't see why that method is a bad idea ? Also in relation to the line of code you wrote, I don't think the sizeof operator is going to work for 2D array properly ? – Shady Programmer Sep 09 '15 at 15:13

2 Answers2

2

Inside the for loop, you are not using arr as a pointer-to-an-int; you are using *(arr+i) as a pointer-to-an-int, which is consistent with dereferencing arr, which is a pointer-to-a-pointer-to-an-int.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
1

You are not "reusing" arr as both a pointer to pointer and as a pointer. arr has type int **, but then you do some pointer arithmetic and dereference the pointer with *(arr + i), and that expression has type int *.

Also, as mentioned by Joachim in the comments, *(arr + i) is equivalent to arr[i]. Using this notation makes it more clear exactly what you're doing.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Okay, I see my confusion now, I thought that after declaring and defining an int **arr then later on in the code I can only de-refference it to **arr, not, *arr. Thank you, upvote. – Shady Programmer Sep 09 '15 at 14:27