指向int数组的指针 [英] pointer to an int array

查看:176
本文介绍了指向int数组的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨伙计,

#include< stdio.h>

int main()

{

int(* p)[10];

int arr [10];

int i;


p = arr; / *< - 编译警告* /

for(i = 0; i< = 12; i ++){/ * i< = 12代替编写代码* /

*((* p)+ i)= i;


printf("%d \ n",p [0] [i]);

}


返回0;

}


在上面的程序中,编译器提供以下内容警告


$ gcc pointer2array.c

pointer2array.c:函数`main'':

pointer2array.c:8 :警告:从不兼容的指针类型分配


为什么警告?


O''kay和int的用法是什么(* p )[10]声明类型?

编译器是否对数组边界执行任何检查?

如果我声明int arr [12] ...

-Neo

Hi Folks,
#include<stdio.h>
int main()
{
int (*p)[10];
int arr[10];
int i;

p = arr; /* <-- compiler warning */
for(i=0; i <= 12; i++){ /* i <=12 delibrately written code */
*((*p) + i) = i;

printf("%d\n", p[0][i]);
}

return 0;
}

In the above program compiler is giving following warning

$gcc pointer2array.c
pointer2array.c: In function `main'':
pointer2array.c:8: warning: assignment from incompatible pointer type

Why the warning?

O''kay and what is the use of int (*p)[10] type of declaration ?
Does the compiler perform any check on the array bounds?
lets say if i declare int arr[12]...
-Neo

推荐答案

gcc pointer2array.c

pointer2array.c:在函数`main'中':

pointer2array.c:8:警告:从不兼容的指针类型分配


为什么警告g?

O''kay和int(* p)[10]类型的声明有什么用?

编译器是否执行任何检查在阵列边界上?

让我说如果我声明int arr [12] ...

-Neo
gcc pointer2array.c
pointer2array.c: In function `main'':
pointer2array.c:8: warning: assignment from incompatible pointer type

Why the warning?

O''kay and what is the use of int (*p)[10] type of declaration ?
Does the compiler perform any check on the array bounds?
lets say if i declare int arr[12]...
-Neo


2005年1月3日星期一12:44:42 + 0530,Neo < ti *************** @ yahoo.com>

在comp.lang.c中写道:
On Mon, 3 Jan 2005 12:44:42 +0530, "Neo" <ti***************@yahoo.com>
wrote in comp.lang.c:
嗨伙计,

#include< stdio.h>
int main()
{
int(* p)[10];
int arr [10];
int i;

p = arr; / *< - 编译器警告* /


表达式中数组的名称,而不是'

操作数的'' sizeof''或''&''运算符,转换为指向

的第一个元素。因此,在上面的语句中,您将int(arr [0])的

地址分配给指向数组的指针。但是arr [0]是一个

int,而不是一个整数数组。


替换为:


p =& arr;

for(i = 0; i< = 12; i ++){/ * i< = 12代替编写代码* /
*((* p) + i)= i;

printf("%d \ n",p [0] [i]);
}

返回0;
}

在上面的程序中编译器发出以下警告

Hi Folks,
#include<stdio.h>
int main()
{
int (*p)[10];
int arr[10];
int i;

p = arr; /* <-- compiler warning */
The name of an array in an expression, other than when it is the
operand of the ''sizeof'' or ''&'' operators, is converted to a pointer to
its first element. So in the statement above, you are assigning the
address of an int (arr[0]) to a pointer to an array. But arr[0] is an
int, not an array of ints.

Replace with:

p = &arr;
for(i=0; i <= 12; i++){ /* i <=12 delibrately written code */
*((*p) + i) = i;

printf("%d\n", p[0][i]);
}

return 0;
}

In the above program compiler is giving following warning


gcc pointer2array.c
pointer2array .c:在函数`main'':
pointer2array.c:8:警告:从不兼容的指针类型分配

为什么警告?

O'' kay和int(* p)[10]类型的声明有什么用?
编译器是否对数组边界执行任何检查?
如果我声明int arr [12]。 。
gcc pointer2array.c
pointer2array.c: In function `main'':
pointer2array.c:8: warning: assignment from incompatible pointer type

Why the warning?

O''kay and what is the use of int (*p)[10] type of declaration ?
Does the compiler perform any check on the array bounds?
lets say if i declare int arr[12]...




-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt。 comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


这篇关于指向int数组的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆