我是新的编程和学习指针在C.阵列有一个在下面的节目。
第一个程序
#包括LT&;&stdio.h中GT; INT乐趣(); 诠释的main() { INT NUM [3] [3] = {21,325,524,52,0,6514,61,33,85}; 有趣(NUM); 的printf(%d个*(*(NUM + 1)+1)); *(*(NUM + 1)+ 1)= 0; 的printf(%d个*(*(NUM + 1)+1)); 返回0; } INT乐趣(INT ** P) { *(*(P + 1)+ 1)= 2135; 返回0; }第二方案
#包括LT&;&stdio.h中GT; INT乐趣(); 诠释的main() { INT NUM [3] [3] = {21,325,524,52,0,6514,61,33,85}; 有趣(NUM); 的printf(%d个*(*(NUM + 1)+1)); *(*(NUM + 1)+ 1)= 0; 的printf(%d个*(*(NUM + 1)+1)); 返回0; } INT乐趣为(int * P) { *((P + 1)+ 1)= 2135; 返回0; }第3项目
#包括LT&;&stdio.h中GT; INT乐趣(); 诠释的main() { INT NUM [3] [3] = {21,325,524,52,0,6514,61,33,85}; 有趣(NUM); 的printf(%d个*(*(NUM + 1)+1)); *(*(NUM + 1)+ 1)= 0; 的printf(%d个*(*(NUM + 1)+1)); 返回0; } INT乐趣(中间体(* p)的[3]) { *(*(P + 1)+ 1)= 2135; 返回0; }我搜索了这个,但无法找到满意的结果。
解决方案所有程序都是病态的。你的编译器必须生成警告或错误消息,并且产生的任何可执行文件的输出是没有意义的。
他们是病态的,因为 INT [3] [3] 是不是与 INT ** 兼容,也不与为int * ,也没有与为int * [3] 。
要通过 INT [3] [3] 来一个函数,该函数必须接受 INT(*)[3] ,没有别的(当然,除了无效* )。
这是因为阵列可以转换为一个指针数组的第一个元素。 (在C语法, NUM 可以用来指试验#[0] )。
在C语言中,只有真正的一维数组;型的阵列 INT [3] [3] 被认为是3个元素,其中每个的3个整数的数组的数组。
因此,一个指向 NUM 的第一个元素是一个指向3个整数数组,这被写成 INT(* P) [3] 。你可以写:
INT(* P)[3] =试验#[0];或简写为同一件事:
INT(* P)[3] = NUM;NB。你不断地写 *(*(NUM + 1)+1))这是难以阅读。取而代之的是, NUM [1] [1] 看起来更清晰。
在C, X [Y] 始终完全等同于 *(X + Y)。
I'm new in programming and learning pointers in array in C. Have a look at the below programmes.
1st program
#include<stdio.h> int fun(); int main() { int num[3][3]={21,325,524,52,0,6514,61,33,85}; fun(num); printf("%d",*(*(num+1)+1)); *(*(num+1)+1)=0; printf("%d",*(*(num+1)+1)); return 0; } int fun(int **p) { *(*(p+1)+1)=2135; return 0; }2nd program
#include<stdio.h> int fun(); int main() { int num[3][3]={21,325,524,52,0,6514,61,33,85}; fun(num); printf("%d",*(*(num+1)+1)); *(*(num+1)+1)=0; printf("%d",*(*(num+1)+1)); return 0; } int fun(int *p) { *((p+1)+1)=2135; return 0; }3rd program
#include<stdio.h> int fun(); int main() { int num[3][3]={21,325,524,52,0,6514,61,33,85}; fun(num); printf("%d",*(*(num+1)+1)); *(*(num+1)+1)=0; printf("%d",*(*(num+1)+1)); return 0; } int fun(int (*p)[3]) { *(*(p+1)+1)=2135; return 0; }
I had searched about this but couldn't found the satisfactory result.
解决方案All of your programs are ill-formed. Your compiler must produce warning or error messages, and the output of any executable produced is meaningless.
They are ill-formed because int[3][3] is not compatible with int **, nor with int *, nor with int *[3].
To pass int[3][3] to a function, the function must accept int (*)[3] and nothing else (well, except for void *).
This is because arrays can be converted to a pointer to the first element of the array. (In C syntax, num can be used to mean &num[0]).
In C, there are only truly one-dimensional arrays; an array of type int[3][3] is considered to be an array of 3 elements, each of which is an array of 3 ints.
So a pointer to the first element of num is a pointer to an array of 3 ints, which is written as int (*p)[3]. You could write:
int (*p)[3] = &num[0];or the shorthand for the same thing:
int (*p)[3] = num;
NB. You continually write *(*(num+1)+1)) which is difficult to read. Instead of this, num[1][1] seems much clearer.
In C, x[y] is always exactly equivalent to *(x+y).
更多推荐
是什么在功能* p和(* P)[3]之间的区别?
发布评论