指向int数组的指针

编程入门 行业动态 更新时间:2024-10-22 16:49:48
本文介绍了指向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> 在comp.lang.c中写道: On Mon, 3 Jan 2005 12:44:42 +0530, "Neo" <ti***************@yahoo> 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 主页: JK-Technology.Com 常见问题解答 comp.lang.c www.eskimo/~scs/C-faq/top.html comp.lang.c ++ www.parashift/c++-faq-lite/ alt。 comp.lang.learn.c-c ++ www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

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

更多推荐

指向int数组的指针

本文发布于:2023-07-29 00:47:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235687.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   指针   int

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!