是什么在功能* p和(* P)[3]之间的区别? [英] What is the difference between *p and (*p)[3] in the function?

查看:133
本文介绍了是什么在功能* p和(* P)[3]之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的编程和学习指针在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;
     }


  1. 在第一个程序 ** P 趣味使用()功能,我认为它应该是正确的,在功能我已经写了 *(*(p + 1)+1)来改变第一个数组的第一个元素。但在编写这个程序它显示错误:一元的无效类型参数'*'(有'诠释')。据我所知num是一个指针数组,并将其持有的地址 NUM [1] 这是再次举办的 NUM [地址1] [0]

  2. 在编译第二个程序编译器没有显示任何错误。和 *((P + 1)+ 1)= 0 正在改变第一阵列的第二个元素的值。为什么它是不断变化的零数组不是第一个数组的第一个元素的值的第二个元素的价值?如何?它应该是 *(*(P + 1)+1)= 0

  3. 在第三个节目的compler没有显示任何错误,它显示了正确的结果。怎么样?。这是什么 *(P)[3] 意思?

我搜索了这个,但无法找到满意的结果。


解决方案

所有程序都是病态的。你的编译器必须生成警告或错误消息,并且产生的任何可执行文件的输出是没有意义的。

他们是病态的,因为 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;
     }

  1. In the first program **p is used in the fun() function which I think it should be correct and in that function I've written *(*(p+1)+1) to change the first element of first array. But on compiling this program it's showing error: invalid type argument of unary '*' (have 'int'). As far as I know num is a pointer to array and it is holding the address of num[1] which is again holding the address of num[1][0].
  2. On compiling the second program compiler is showing no error. And *((p+1)+1)=0 is changing the value of 2nd element of first array. Why it is changing the value of 2nd element of zeroth array not the value of first element of first array? and How? It should be *(*(p+1)+1)=0.
  3. In the third program the compler is showing no error and it is showing the correct result. How?. What does *(p)[3] mean?

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]之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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