指向数组的问题 [英] problem with pointer to array
问题描述
i不知道为什么下面提到的程序给错了o / p.
#include< stdio.h>
#include< stdlib.h>
#include< string.h>
ARRAY_SIZE 10
int func(void * x)
{
int(* y)[ARRAY_SIZE];
y = x;
printf("%d,%d",y [0],y [1]);
返回0;}
int main(无效)
{
int arr [ARRAY_SIZE] = {1,2};
int( * p)[ARRAY_SIZE];
p =& arr;
func(p);
返回0;
}
O / P -10272722,-18928292
" an *** **********@gmail.com" <一个************* @ gmail.com>写道:我不知道为什么下面提到的程序给出错误的o / p。
#include< stdio.h>
#include < stdlib.h>
#include< string.h>
你不能在< stdlib.h>中使用任何东西。或者< string.h>。
ARRAY_SIZE 10
我认为这应该是#define ARRAY_SIZE 10。
int func(void * x)
{
int(* y)[ARRAY_SIZE];
y = x;
printf("%d,%d" ,y [0],y [1]);
返回0;}
y是指向10个整数数组的指针。 y [0]是一个10个整数的数组,
衰减到一个指向10个整数数组的指针。您使用%d打印此
指针值格式,它需要一个int,而不是指针。
(我不是100%肯定我已经掌握了所有细节。)未定义
行为。
另外,你应该在输出的末尾打印''\ n''字符;
否则它不保证你的输出将出现。
int main(无效)
{arr [ARRAY_SIZE] = {1,2};
int(* p)[ARRAY_SIZE];
p =& arr;
func(p);
返回0;
}
O / P -10272722,-18928292
声明指向数组的指针很少有意义。通常你需要一个指向元素类型的
;你可以使用这样的指针来访问数组的其余部分。你可以传递数组的长度作为
a单独的参数。
这是一个更直接的方法来做你想做的事情:
#include< stdio.h>
#define ARRAY_SIZE 10
int func (int * arr)
{
printf("%d,%d \ n",arr [0],arr [1]);
返回0;
}
int main(无效)
{
int arr [ARRAY_SIZE] = {1,2};
func(arr);
返回0;
}
输出:
1,2
-
Keith Thompson( The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>
圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>
我们必须做点什么。这是事情。因此,我们必须这样做。
您好
************* @ gmail.com 写道:
#include< stdio。 h>
#include< stdlib.h>
#include< string.h>
ARRAY_SIZE 10
不能编译。你可能的意思是#define ARRAY_SIZE 10
int func(void * x)
{
int(* y)[ARRAY_SIZE];
y = x ;
printf("%d,%d",y [0],y [1]);
好吧,想一想:y是一个指向大小为ARRAY_SIZE的int数组的指针。
然后y [0]相当于(* y),这是一个int数组。
指针y不引用任何数组,所以y [1]没有任何意义上的
。
要获得_ * y_的第一个和第二个条目,你必须分别写(* y)[0]或(* y
[1]。
Markus
> #include< stdio.h>#include< stdlib.h>
#include< string.h>
ARRAY_SIZE 10
int func(void * x)
{/> int(* y)[ARRAY_SIZE];
y = x;
printf("%d,% d",y [0],y [1]);
返回0;}
int(* y)[ARRAY_SIZE];不是必需的。
int * y =(int *)x;绰绰有余。
Hi ,
i don''t know why the below mentioned program is giving wrong o/p.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
ARRAY_SIZE 10
int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=x;
printf("%d, %d",y[0],y[1]);
return 0;}
int main (void)
{
int arr[ARRAY_SIZE]={1,2};
int (*p)[ARRAY_SIZE];
p=&arr;
func(p);
return 0;
}
O/P -10272722,-18928292
"an*************@gmail.com" <an*************@gmail.com> writes:i don''t know why the below mentioned program is giving wrong o/p.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
You don''t use anything in either <stdlib.h> or <string.h>.
ARRAY_SIZE 10
I presume this was supposed to be "#define ARRAY_SIZE 10".
int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=x;
printf("%d, %d",y[0],y[1]);
return 0;}
y is a pointer to an array of 10 ints. y[0] is an array of 10 ints,
which decays to a pointer to an array of 10 ints. You print this
pointer value with a "%d" format, which expects an int, not a pointer.
(I''m not 100% certain I''ve gotten all the details right.) Undefined
behavior.
Also, you should print ''\n'' character at the end of your output;
otherwise it''s not guaranteed that your output will appear.
int main (void)
{
int arr[ARRAY_SIZE]={1,2};
int (*p)[ARRAY_SIZE];
p=&arr;
func(p);
return 0;
}
O/P -10272722,-18928292
It''s rarely makes sense to declare pointers to arrays. Usually you
want a pointer to the element type; you can use such a pointer to
access the rest of the array. You can pass the length of the array as
a separate parameter.
Here''s a more straightforward way to do what you''re trying to do:
#include <stdio.h>
#define ARRAY_SIZE 10
int func(int *arr)
{
printf("%d, %d\n", arr[0], arr[1]);
return 0;
}
int main(void)
{
int arr[ARRAY_SIZE] = {1,2};
func(arr);
return 0;
}
Output:
1, 2
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Hi
an*************@gmail.com wrote:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
ARRAY_SIZE 10
That won''t compile. You probably meant "#define ARRAY_SIZE 10"
int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=x;
printf("%d, %d",y[0],y[1]);
Well, think about it: y is a pointer to array of int of size ARRAY_SIZE.
Then y[0] is equivalent to (*y), which is an array of int.
The pointer y does not refer to an array of anything, so y[1] doesn''t make
any sense.
To get the first and second entry of _*y_, you have to write (*y)[0] or (*y
[1], respectively.
Markus
> #include <stdio.h>#include <stdlib.h>
#include <string.h>
ARRAY_SIZE 10
int func(void *x)
{
int (*y)[ARRAY_SIZE];
y=x;
printf("%d, %d",y[0],y[1]);
return 0;}
int (*y)[ARRAY_SIZE]; is not required.
int *y = (int*)x ; is more than enough.
这篇关于指向数组的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!