如何定义指向多个下标数组的指针 [英] How to define a pointer to multiple-subscripted arrays
问题描述
我想定义一个指向多个下标数组的指针。
例如,我有一个如下定义的数组(我减少了
的大小数组以及尺寸,但我觉得可以问这个简单的情况是好吗。)
/ ----
int array [2] [2] = {1,2,3,4}
\ -----
现在我想要一个指向这个数组的指针。
我试过了打击
/ ----
#include < stdio.h>
int main()
{
int array [2] [2] = {
1,2,
3,4
};
int * nPtr [2];
nPtr =数组;
printf("%d \ n",nPtr [1] [1]);
返回0;
}
\ ----
编译时,编译器说:
main.c:11:警告:从不兼容的指针类型赋值
main.c:13:错误:下标值既不是数组也不是指针
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
推荐答案
3月8日晚上9:11,周燕< zhouyan1 ... @ gmail.comwrote:
On Mar 8, 9:11 pm, Zhou Yan <zhouyan1...@gmail.comwrote:
我想定义一个指向多个下标数组的指针。
例如,我有一个如下定义的数组(我减少了
数组的大小以及尺寸,但我认为可以这个简单的情况询问
。)
/ ----
int array [2] [2] = {1,2,3,4}
\ -----
现在我想要一个指针指向这个数组。
我试过了打击
/ --- -
#include< stdio.h>
int main()
{
int array [2 ] [2] = {
1,2,
3,4
};
int * nPtr [2];
nPtr = array;
printf("%d \ n",nPtr [1] [1] );
返回0;
}
\ ----
编译时,编译器说:
main.c:11:警告:从不兼容的指针类型分配
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
是的,nptr是一个有两个指针成员的数组,指向int
type;
但是,数组是一个二维数组。它们有不同类型的b $ b。
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:错误:下标值既不是数组也不是指针
main.c:13: error: subscripted value is neither array nor pointer
更改语句 ; nptr = array;" tonptr [1] =& array [1] [0];"可能
工作......
changing statement "nptr = array;" to "nptr[1] = &array[1][0];" may
work...
谢谢,我现在试试......
thanks, I try now...
是的,通过这种改变,它可以正常工作。但我仍然不理解b $ b。你能不能给出一个简短的解释?
我也尝试打印所有4个数字,我按照这种方式,我
找到我还需要:
nPtr [0] =数组[0];
和我检查数组[0]和&数组[0]相同] [0] by
printf("%p\\\
%p \ n",array [0],& array [0] [0]);
printf("%p\\\
%p \ n",array [1],& array [1] [0]);
请你帮我解释一下?
什么是数组[0]实际上是什么?它是一个包含此数组的第一行
的数组吗?或者它是一个指针,具有数组的地址[
0] [0]。或者在内部它都是?
如果我定义了这样一个指针数组,nPtr [2],我需要初始化
这个数组中的所有指针,对?是否可以定义一个可以指向二维数组的任何地址的单个数组? (我需要
问这样愚蠢的排队......但我真的什么都不知道,找不到一本
书谈这些)
谢谢
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
这篇关于如何定义指向多个下标数组的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!