定义了 int a[3] 后,int* b = a 和 int (*c)[3] = &a 之间有什么区别吗? [英] When int a[3] has been defined, is there any difference between int* b = a and int (*c)[3] = &a

查看:63
本文介绍了定义了 int a[3] 后,int* b = a 和 int (*c)[3] = &a 之间有什么区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图加深对 C 中指针相关语法的理解,并且我注意到如果我首先创建了一个 int 数组,int(*)[] 是一种给出指向该数组的指针,这似乎也可以通过简单地使用 int * 来完成.所以我很困惑下面代码中的指针 b 和指针 c 之间有什么区别?如果它们不同,我什么时候应该使用一种方式而不是选择另一种方式?

#include int main(void){int a[3];int* b = a;int (*c)[3] = &a;printf("%p\n", a);printf("%p\n", b);printf("%p\n", c);//这3个输出是相同的.}

我只是尝试添加几行代码来进一步理解这一点:

 a[2] = 3;printf("%i\n", a[2]);//= 3printf("%i\n", b[2]);//= 3

似乎 b 和 c 不同,因为 printf("%i\n", c[2]) 会产生错误.如何解释和理解以上所有内容?

解决方案

bc 都指向同一个内存但差别很大:

  1. 这是两种不同的类型(指向 int 的指针和指向 3 个 int 数组的指针)
  2. b 被取消引用到 b 指向的单个整数,而 c 被取消引用到 3 个 int 的数组>s 这意味着你可以为 *b (*b = 10;) 分配一个整数,但你不能为 c 做同样的事情(但仅限于指定索引 (*c)[0] = 10)
  3. 指针算法也不同 b + 1b 的值增加 sizeof(int)c + 1c 的值增加 3*sizeof(int)

<块引用>

如果它们不同,我什么时候应该使用一种方式而不是选择另一个?

与 C 中的任何其他类型一样,应根据您的需要和应用程序使用它.最常用的是 int *b; 选项,因为它为您提供了更大的灵活性.使用这样的指针,您可以处理任何大小的数组,它更常用且更具可读性.虽然指向数组的指针将您绑定到一个预定义大小的数组,但它的语法可读性较差(在我看来),因此更难审查/调试代码.根据我的经验,我从未见过使用指向数组的指针的生产代码,但这并不意味着如果您在应用程序中发现它的任何优势,就不能使用它.

I'm trying to deepen my understanding on syntax relevant to pointer in C, and I noticed that If I created an array of int first, int(*)[] is a way of giving a pointer to this array, which seemingly could also be done by simply using int *. So I'm confused what's the difference between pointer b and pointer c in Code below? If they're different, when should I use one way instead of choosing the other one?

#include <stdio.h>

int main(void)
{
    int a[3];    
    int* b = a;
    int (*c)[3] = &a;

    printf("%p\n", a);
    printf("%p\n", b); 
    printf("%p\n", c); //these 3 outputs are same.
} 

I just tried to add lines of code to further understand this:

    a[2] = 3;

    printf("%i\n", a[2]); // = 3
    printf("%i\n", b[2]); // = 3

It seems that b and c are different because printf("%i\n", c[2]) will generate an error. How to explain and understand all above?

解决方案

Both b and c point to the same memory but the difference is big:

  1. These are two different types (pointer to int and pointer to an array of 3 ints)
  2. b is dereferenced to a single integer pointed by b while c is dereferenced to an array of 3 ints which means you can assign an integer to *b (*b = 10;) but you can't do the same for c (but only with specified index (*c)[0] = 10)
  3. Pointer arithmetic is also different b + 1 will increase the value of b by sizeof(int) while c + 1 will increase the value of c by 3*sizeof(int)

If they're different, when should I use one way instead of choosing the other one?

As any other type in C, it should be used based on your needs and application. Most commonly used is the int *b; option since it gives you more flexibility. With such pointer you can handle array of any size, it is more commonly used and more readable. While pointer to an array binds you to an array of pre-defined size, its syntax is less readable (in my opinion) and thus will be harder to review/debug the code. In my experience I've never seen a production code which uses pointer to an array but this doesn't mean you cannot use it if you find any advantage of it in your application.

这篇关于定义了 int a[3] 后,int* b = a 和 int (*c)[3] = &amp;a 之间有什么区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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