473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

** vs 2-d array

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
Nov 13 '05 #1
6 1770
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
Nov 13 '05 #2

"Arijit Mukherjee" <ar****@lucent.com> 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.
Nov 13 '05 #3
Arijit Mukherjee <ar****@lucent.com> 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 (Zo**********@daimlerchrysler.com)
"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
Nov 13 '05 #4

"Arijit Mukherjee" <ar****@lucent.com> wrote in message
news:3F***************@lucent.com...
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
Nov 13 '05 #5
Malcolm wrote:

"Arijit Mukherjee" <ar****@lucent.com> 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
Nov 13 '05 #6
Arijit Mukherjee <ar****@lucent.com> 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.

Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.