** vs 2-d array

  • Thread starter Arijit Mukherjee
  • Start date
A

Arijit Mukherjee

All,
This piece of code works:

#include <stdio.h>

int main()
{
int abc[2][3] = {{1,2,3},{3,4,5}};
printf("The value of abc[2,2] %d\n", *((*abc) + 1));
}


While, this:

#include <stdio.h>

int main()
{
int abc[2][3] = {{1,2,3},{3,4,5}};
int **p = (int **)abc;
printf("The value of abc[2,2] %d\n", *((*p) + 1));
}

dumps core at printf.


Any reason why?

Thanks in anticipation,

Regards,
Arijit
 
A

Arijit Mukherjee

All,
BTW, I did not modify the printf statement - to say abc[1,2], please
ignore the index printed. Also, I used gcc version 3.0.4 for
compilation,

Thanks and regards,
Arijit
 
M

Malcolm

Arijit Mukherjee said:
BTW, I did not modify the printf statement - to say abc[1,2], please
ignore the index printed. Also, I used gcc version 3.0.4 for
compilation,
Firstly, C uses the syntax array[y][x] for a 2d array. Surprisingly,
array[y,x] is not an error. This is because of a little-used feature called
the comma operator, which is effectively a nop operator, and because
array[y] is an array, therefore a value, in its own right.

The important thing you need to know is that multi-dimensional array syntax
in C gets pretty confusing once you try to take pointers to arrays.

Further confusion is that an array of pointers looks like a 2d array, but
isn't the same thing at all.

int **twod means we have a pointer to a pointer. Often a pointer points not
to one value but to the first in a (1d) array of values, twod[0] and twod[1]
may be completely different pointers, widely separated in memory.
 
Z

Zoran Cutura

Arijit Mukherjee said:
All,
This piece of code works:

#include <stdio.h>

int main()
{
int abc[2][3] = {{1,2,3},{3,4,5}};
printf("The value of abc[2,2] %d\n", *((*abc) + 1));
}


While, this:

#include <stdio.h>

int main()
{
int abc[2][3] = {{1,2,3},{3,4,5}};
int **p = (int **)abc;
printf("The value of abc[2,2] %d\n", *((*p) + 1));
}

dumps core at printf.


Any reason why?

p is not appropriatly typed and initialized.

If p is meant to stay a single pointer you need to tell the compiler
the first dimension of the two-dimensional array it is meant to point
to, so that when you apply pointer arithmetic the correct numbers are
used.

int (*p)[3] = abc;

should do the job.
 
A

Arijit Mukherjee

Malcolm said:
Arijit Mukherjee said:
BTW, I did not modify the printf statement - to say abc[1,2], please
ignore the index printed. Also, I used gcc version 3.0.4 for
compilation,
Firstly, C uses the syntax array[y][x] for a 2d array. Surprisingly,
array[y,x] is not an error. This is because of a little-used feature called
the comma operator, which is effectively a nop operator, and because
array[y] is an array, therefore a value, in its own right.

The important thing you need to know is that multi-dimensional array syntax
in C gets pretty confusing once you try to take pointers to arrays.

Further confusion is that an array of pointers looks like a 2d array, but
isn't the same thing at all.

int **twod means we have a pointer to a pointer. Often a pointer points not
to one value but to the first in a (1d) array of values, twod[0] and twod[1]
may be completely different pointers, widely separated in memory.

I was trying to explain this the same way - arrays are contiguous block
- while a ** would mean that the first is pointing to an array of
pointers - and the location of the second level arrays are totally
separated. However, what I could not understand is that the code core-d
at the access part - not typecasting part.

Also, if the access to the arrays were like abc[j] = abc + i*jmax + j
- then, that expression access should have worked - which did not. The
only way (other than abc[j]), I could access was to use the pointer
arithmetic - and assume that abc was a **.

That's why I am really confused!!!!

Thanks and regards,
Arijit
 
A

Alejo

Arijit Mukherjee said:
All,
This piece of code works:

#include <stdio.h>

int main()
{
int abc[2][3] = {{1,2,3},{3,4,5}};
printf("The value of abc[2,2] %d\n", *((*abc) + 1));
}


While, this:

#include <stdio.h>

int main()
{
int abc[2][3] = {{1,2,3},{3,4,5}};
int **p = (int **)abc;
printf("The value of abc[2,2] %d\n", *((*p) + 1));
}

dumps core at printf.


Any reason why?

Thanks in anticipation,

Regards,
Arijit


1) The name of an array is pointer to the first element of the array.
2) A 2D array is an array of arrays.

Taking those tow points into account let's see an example:

Consider the folollowing declaration:

int A[3][5];

We have declared an array of 3 elements, where each element is an array of 5
integers. So the name of the array, A, is a pointer to an array of 5
integers.

int **ptr;

ptr is a pointer to a pointer to an integer.

ptr = A;

is wrong because A is not a pointer to a pointer to an integer.

int (*ptr_A)[5];

The declaration above declares a pointer, ptr_A, to an array of 5 integers.
So...

ptr_A = A;

is a correct initialization of the pointer;

Hope this helped :)
 
K

Kevin Easton

Arijit Mukherjee said:
All,
This piece of code works:

#include <stdio.h>

int main()
{
int abc[2][3] = {{1,2,3},{3,4,5}};
printf("The value of abc[2,2] %d\n", *((*abc) + 1));
}


While, this:

#include <stdio.h>

int main()
{
int abc[2][3] = {{1,2,3},{3,4,5}};
int **p = (int **)abc;
printf("The value of abc[2,2] %d\n", *((*p) + 1));
}

dumps core at printf.

In the expression:

int **p = (int **)abc;

The `abc' is evaluated as a pointer to the first element of the array -
a pointer to { 1, 2, 3} which is of type (int (*)[3]). You then cast
this to (int **) and store it in p.

When you evaluate *p, the start of the array { 1, 2, 3 } is loaded as if
it were a pointer value (int *) - but it's not. It's an array. Then
you do some pointer arithmetic on it, and try to dereference the result
- which is a pointer that makes no sense at all.

Having an int ** only makes sense if there's a real object of type "int
*" for it to point to. The correct solution is to use:

int (*p)[3] = abc;

- Kevin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top