Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > ** vs 2-d array

Reply
Thread Tools

** vs 2-d array

 
 
Arijit Mukherjee
Guest
Posts: n/a
 
      08-04-2003
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
 
Reply With Quote
 
 
 
 
Arijit Mukherjee
Guest
Posts: n/a
 
      08-04-2003
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
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      08-04-2003

"Arijit Mukherjee" <(E-Mail Removed)> wrote in message
>
> 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.


 
Reply With Quote
 
Zoran Cutura
Guest
Posts: n/a
 
      08-04-2003
Arijit Mukherjee <(E-Mail Removed)> wrote:
> 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.

--
Z ((E-Mail Removed))
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
 
Reply With Quote
 
Arijit Mukherjee
Guest
Posts: n/a
 
      08-04-2003
Malcolm wrote:
>
> "Arijit Mukherjee" <(E-Mail Removed)> wrote in message
> >
> > 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[i][j] = abc + i*jmax + j
- then, that expression access should have worked - which did not. The
only way (other than abc[i][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
 
Reply With Quote
 
Alejo
Guest
Posts: n/a
 
      08-04-2003

"Arijit Mukherjee" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

--

Alejo


 
Reply With Quote
 
Kevin Easton
Guest
Posts: n/a
 
      08-05-2003
Arijit Mukherjee <(E-Mail Removed)> wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
How to combine 2 int Array into ONE int Array ? S300 Java 4 08-19-2003 07:04 PM
hashed array in array need the keys... and length Daniel Perl 1 08-14-2003 06:49 PM



Advertisments