473,385 Members | 2,014 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,385 software developers and data experts.

How to define a pointer to multiple-subscripted arrays

I want to define a pointer to a multiple-subscripted array.
For instance, I have an array defined as below( I have reduced the
size of the array as well as the dimension, but I think it OK to ask
this simple case.)

/----
int array[ 2 ][ 2 ] = { 1, 2, 3, 4 }

\-----

Now I want a pointer to point to this array.

I tried blow
/----
#include <stdio.h>
int main()
{
int array[ 2 ][ 2 ] = {
1, 2,
3, 4
};

int *nPtr[ 2 ];

nPtr = array;
printf( "%d\n", nPtr[ 1 ][ 1 ] );
return 0;
}
\----

when compilation, the compiler say:

main.c:11: warning: assignment from incompatible pointer type
main.c:13: error: subscripted value is neither array nor pointer

Mar 8 '07 #1
10 5443
On Mar 8, 9:11 pm, Zhou Yan <zhouyan1...@gmail.comwrote:
I want to define a pointer to a multiple-subscripted array.
For instance, I have an array defined as below( I have reduced the
size of the array as well as the dimension, but I think it OK to ask
this simple case.)

/----
int array[ 2 ][ 2 ] = { 1, 2, 3, 4 }

\-----

Now I want a pointer to point to this array.

I tried blow
/----
#include <stdio.h>
int main()
{
int array[ 2 ][ 2 ] = {
1, 2,
3, 4
};

int *nPtr[ 2 ];

nPtr = array;
printf( "%d\n", nPtr[ 1 ][ 1 ] );
return 0;
}
\----

when compilation, the compiler say:

main.c:11: warning: assignment from incompatible pointer type
Yes, nptr is an array having two pointer members which points to int
type;
however, array is a two dimension array. They have different
type.
main.c:13: error: subscripted value is neither array nor pointer
changing statement "nptr = array;" to "nptr[1] = &array[1][0];" may
work...

Mar 8 '07 #2
thanks, I try now...
Mar 8 '07 #3
Yes, by this changing, it work properly. But I still do not
understand. Could you kindly give a brief explanation?

Also I tried to print all the 4 numbers and I followed this way, I
find I also need:

nPtr[ 0 ] = array[ 0 ];

and I checked array[ 0 ] is just the same as &array[ 0 ][ 0 ] by

printf( "%p\n%p\n", array[ 0 ], &array[ 0 ][ 0 ] );
printf( "%p\n%p\n", array[ 1 ], &array[ 1 ][ 0 ] );

Could you please kindly give me a explanation?
What is array[ 0 ] actually is? Is it an array which contain the the
first row of this array? Or is it a pointer, with the address of array[
0 ][ 0 ]. Or internally it is both?

If I define such an array of pointers, nPtr[ 2 ], I need to initialize
all the pointers in this array,right? Is it possible to define a
single array which can point to any address of a 2-D array? (I need to
ask such silly quetions...but I really know nothing and cannot find a
book talk about these)

Thank you
Mar 8 '07 #4
On Mar 8, 8:11 am, Zhou Yan <zhouyan1...@gmail.comwrote:
I want to define a pointer to a multiple-subscripted array.
For instance, I have an array defined as below( I have reduced the
size of the array as well as the dimension, but I think it OK to ask
this simple case.)

/----
int array[ 2 ][ 2 ] = { 1, 2, 3, 4 }

\-----

Now I want a pointer to point to this array.

I tried blow
/----
#include <stdio.h>
int main()
{
int array[ 2 ][ 2 ] = {
1, 2,
3, 4
};

int *nPtr[ 2 ];

nPtr = array;
printf( "%d\n", nPtr[ 1 ][ 1 ] );
return 0;
}
\----

when compilation, the compiler say:

main.c:11: warning: assignment from incompatible pointer type
main.c:13: error: subscripted value is neither array nor pointer

Change: int *nPtr[2]; // declares an array of 2 pointers to ints
To: int (*nPtr)[2]; // declares a pointer to an array of 2 ints

Recompile, run. The difference is subtle, but the result it totally
different.

http://www.cs.cf.ac.uk/Dave/C/node10.html

Matthew

Mar 8 '07 #5
Zhou Yan wrote:
I want to define a pointer to a multiple-subscripted array.
#include <stdio.h>
int main()
{
int array[ 2 ][ 2 ] = {
1, 2,
3, 4
};

int *nPtr[ 2 ];
int (*nPtr) [ 2 ];
>
nPtr = array;
printf( "%d\n", nPtr[ 1 ][ 1 ] );
return 0;
}
\----

when compilation, the compiler say:

main.c:11: warning: assignment from incompatible pointer type
The original code defined nPtr as an array, size 2, of pointer to int.
Adding the parentheses defines nPtr as a pointer to an array, size 2, of
int, which is compatible with an array of arrays (length 2) of int.

The first index then selects the row, while the second selects the
element within the row.
--
Thad
Mar 9 '07 #6
Zhou Yan wrote:
Yes, by this changing, it work properly. But I still do not
understand. Could you kindly give a brief explanation?

Also I tried to print all the 4 numbers and I followed this way, I
find I also need:

nPtr[ 0 ] = array[ 0 ];

and I checked array[ 0 ] is just the same as &array[ 0 ][ 0 ] by

printf( "%p\n%p\n", array[ 0 ], &array[ 0 ][ 0 ] );
printf( "%p\n%p\n", array[ 1 ], &array[ 1 ][ 0 ] );

Could you please kindly give me a explanation?
What is array[ 0 ] actually is? Is it an array which contain the the
first row of this array? Or is it a pointer, with the address of array[
0 ][ 0 ]. Or internally it is both?
The full explanation is below.
If I define such an array of pointers, nPtr[ 2 ], I need to initialize
all the pointers in this array,right?
Right.
Is it possible to define a single array which can point to any
address of a 2-D array?

Well, you got one answer on how to get a segment of the array, now for
the full explanation:

An 2D array (as you have allocated) is not an array of pointers to an
array of elements, it is in fact a bunch of elements allocated in one chunk.

array is of type int[][2] and is convertible to a pointer to an array of
two elements (int (*)[2]). You can declare a variables of this type
like this:

// Get from zeroth 'row' on down
int (*var0)[2] = array; // or
int (*var0)[2] = array+0; // or
int (*var0)[2] = &array[0];
// Get from first 'row' on down
int (*var1)[2] = array+1; // or
int (*var1)[2] = &array[1];

Note: not int *var[2] as this is an array of int pointers of two
elements. Note also that you cannot cast to or define this type this
directly, something in the language/parser prevents this. To
cast/return, you need to generate a typedef like so:

typedef (*ptrToTwoElementArray_t)[2];

(something that I just figured out now, thanks to you. That has
been bugging me forever, as I've never been able to return or cast
to something like this. Thanks very much!)

array[0] is of type int[] and is convertible to a pointer to an array of
elements (int *). You can also declared a variable of this type
like this:

// Get zeroth 'row'
int *var0 = *(array);
int *var0 = *(array+0);
int *var0 = array[0];
// Get first 'row'
int *var1 = *(array);
int *var1 = *(array+1);
int *var1 = array[1];

This is why your code:

printf( "%p\n%p\n", array[ 0 ], &array[ 0 ][ 0 ] );
printf( "%p\n%p\n", array[ 1 ], &array[ 1 ][ 0 ] );

gave you the results that you found.

array[0][0] is of type int and is convertible to an int. You can also
declare a variable of this type like this:

int var = array[0][0];

This should be obvious.

When you dereference an array (declared as you described) using the []
operators, the compiler knows that this is not an array of pointers to
an array of ints, but that it is a big blob of ints and that it can
calculate the offset to the ints via the formula x+y*MaxX.
(I need to ask such silly quetions...but I really know nothing and
cannot find a book talk about these)
Don't think your question silly. I've been trying to figure this out
for 20 years. :) The only silly question is the unasked one. I
certainly learned something from answering this question, I hope that I
explained it sufficiently for you to too.
Adrian
Mar 9 '07 #7
Adrian Hawryluk <ad**************************@nospam.comwrites:
Zhou Yan wrote:
>Yes, by this changing, it work properly. But I still do not
understand. Could you kindly give a brief explanation?

Also I tried to print all the 4 numbers and I followed this way, I
find I also need:

nPtr[ 0 ] = array[ 0 ];

and I checked array[ 0 ] is just the same as &array[ 0 ][ 0 ] by

printf( "%p\n%p\n", array[ 0 ], &array[ 0 ][ 0 ] );
printf( "%p\n%p\n", array[ 1 ], &array[ 1 ][ 0 ] );

Could you please kindly give me a explanation?
What is array[ 0 ] actually is? Is it an array which contain the the
first row of this array? Or is it a pointer, with the address of array[
0 ][ 0 ]. Or internally it is both?

The full explanation is below.
>If I define such an array of pointers, nPtr[ 2 ], I need to initialize
all the pointers in this array,right?

Right.
>Is it possible to define a single array which can point to any
address of a 2-D array?

Well, you got one answer on how to get a segment of the array, now for
the full explanation:

An 2D array (as you have allocated) is not an array of pointers to an
array of elements, it is in fact a bunch of elements allocated in one
chunk.

array is of type int[][2] and is convertible to a pointer to an array of
two elements (int (*)[2]). You can declare a variables of this type
like this:

// Get from zeroth 'row' on down
int (*var0)[2] = array; // or
int (*var0)[2] = array+0; // or
int (*var0)[2] = &array[0];
// Get from first 'row' on down
int (*var1)[2] = array+1; // or
int (*var1)[2] = &array[1];

Note: not int *var[2] as this is an array of int pointers of two
elements. Note also that you cannot cast to or define this type this
directly, something in the language/parser prevents this. To
cast/return, you need to generate a typedef like so:

typedef (*ptrToTwoElementArray_t)[2];

(something that I just figured out now, thanks to you. That has
been bugging me forever, as I've never been able to return or cast
to something like this. Thanks very much!)

array[0] is of type int[] and is convertible to a pointer to an array of
elements (int *). You can also declared a variable of this type
like this:

// Get zeroth 'row'
int *var0 = *(array);
int *var0 = *(array+0);
int *var0 = array[0];
// Get first 'row'
int *var1 = *(array);
int *var1 = *(array+1);
int *var1 = array[1];

This is why your code:

printf( "%p\n%p\n", array[ 0 ], &array[ 0 ][ 0 ] );
printf( "%p\n%p\n", array[ 1 ], &array[ 1 ][ 0 ] );

gave you the results that you found.

array[0][0] is of type int and is convertible to an int. You can also
declare a variable of this type like this:

int var = array[0][0];

This should be obvious.

When you dereference an array (declared as you described) using the []
operators, the compiler knows that this is not an array of pointers to
an array of ints, but that it is a big blob of ints and that it can
calculate the offset to the ints via the formula x+y*MaxX.
>(I need to ask such silly quetions...but I really know nothing and
cannot find a book talk about these)

Don't think your question silly. I've been trying to figure this out
for 20 years. :) The only silly question is the unasked one. I
certainly learned something from answering this question, I hope that
I explained it sufficiently for you to too.
Adrian
Thank you very much for you explanation. I will print this message
out to read a few times to get the whole idea...

Thanks!
Mar 9 '07 #8
Zhou Yan wrote:
Yes, by this changing, it work properly. But I still do not
understand. Could you kindly give a brief explanation?

Also I tried to print all the 4 numbers and I followed this way, I
find I also need:

nPtr[ 0 ] = array[ 0 ];

and I checked array[ 0 ] is just the same as &array[ 0 ][ 0 ] by

printf( "%p\n%p\n", array[ 0 ], &array[ 0 ][ 0 ] );
printf( "%p\n%p\n", array[ 1 ], &array[ 1 ][ 0 ] );

Could you please kindly give me a explanation?
What is array[ 0 ] actually is? Is it an array which contain the the
first row of this array? Or is it a pointer, with the address of array[
0 ][ 0 ]. Or internally it is both?

If I define such an array of pointers, nPtr[ 2 ], I need to initialize
all the pointers in this array,right? Is it possible to define a
single array which can point to any address of a 2-D array? (I need to
ask such silly quetions...but I really know nothing and cannot find a
book talk about these)

Thank you
Hi Zhou Yan,

Would you mind if I used your name in my blog? I would like to credit
the question to you.
Adrian
Mar 9 '07 #9
Zhou Yan wrote:
>
Thank you very much for you explanation. I will print this message
out to read a few times to get the whole idea...

Thanks!
I don't blame you, I certainly took me a while to figure it out. :)
Adrian
Mar 9 '07 #10
It's of course Ok... You helped me so much...
But perhaps, I hope you can use "Matthew Zhou" or "Matthew", my
English name instead of my Chinese Name. -:)
Mar 9 '07 #11

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

Similar topics

97
by: s | last post by:
Can I do this: #define MYSTRING "ABC" .. .. .. char mychar = MYSTRING; .. .. ..
12
by: Riley DeWiley | last post by:
I am looking for a graceful way to declare a string const that is to be visible across many files. If I do this: //----hdr.h const char * sFoo = "foo"; //file.cpp
2
by: ooze | last post by:
typedef unsigned long PARAM; typedef PARAM SAP; typedef struct qnode { struct qnode *next; struct qnode *prev; } QNODE;
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
5
by: Peter Demeyer | last post by:
I can't manage to define a simple pointer to an array. The following C++ code doesn't work in C# (compiler error: "You can only take the address of unfixed expression inside of a fixed statement...
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
3
by: robin liu | last post by:
What's the difference between these two declarations ? 1) typedef void (*pf)(void); 2) typedef void f(void); the first declaration is define a function pointer, what is the second ? define a...
8
by: mailursubbu | last post by:
Hi, Will it be possible to #define a char pointer... It means if write some thing like #define CHAR_PTR (char *) // I know this wont work I should be able to use CHAR_PTR to define a...
6
by: Cric | last post by:
Hi, how can be the declaration of a pointer which points to dynamic array of pointers and each pointer from this array points to an dynamic array of pointers which points to character strings...
1
by: sophia | last post by:
Dear all, the following are the differences b/w #define and typedef ,which i have seen in Peter van der lindens book. is there any other difference between thes two ? The right way to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.