什么是类型之间的区别 - 为int *和为int * [100]用C? [英] What's the difference between the types - int * and int *[100] in C?

查看:178
本文介绍了什么是类型之间的区别 - 为int *和为int * [100]用C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

均田一个小白,所以这里不杀我。

有何以下$ C $的区别CS?

  INT * P; //我的理解,它会创建一个指向大小为一个int变量。
为int * P [100]; //真的不知道这是什么。
INT(* p)的[100]; //我已经认识到,这是一个指向数组的指针。


解决方案

  1. 这是一个指向一个 INT

      INT * P;

    ┌────┐
    │int*│
    └────┘

    它应该在指向 INT ,是这样的:

    ┌────┐
    │int*│
    └─┃──┘
      ▼
    ┌───┐
    │int│
    └───┘


  2. 这是100指针的阵列 INT

     为int * P [100];

    也就是说,它为您提供了100球。

    <$c$c>┌────┬────┬────┬────┬────┬────┬┄
    │int*│int*│int*│int*│int*│int*│
    └────┴────┴────┴────┴────┴────┴┄

    每个指针应指向一个 INT ,也许是这样的:

    <$c$c>┌────┬────┬────┬────┬────┬────┬┄
    │int*│int*│int*│int*│int*│int*│
    └─┃──┴─┃──┴─┃──┴─┃──┴─┃──┴─┃──┴┄
      ▼▼▼▼▼▼
    ┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐┌┄
    │int││int││int││int││int││int││
    └───┘└───┘└───┘└───┘└───┘└───┘└┄

    当然,我们没有理由他们不能都指向同一 INT ,或什么的。

    您可能想,如果你想很多三分球,你可以很容易地使用指针数组
    遍历。你可以,例如,动态地分配对象,并在一个不同的对象有每个指针指向:

      P [0] = INT新(0);
    P [1] =新INT(0);
    // ...

    也许动态分配 INT s是不是最好的例子,但我认为这一点是明确的。


  3. 这是一个指针数组100 INT

      INT(* P)[100];

    也就是说,它可以让你仅1指针:

    ┌───────────┐
    │int(*)[100]│
    └───────────┘

    它应该在一个包含100阵列点 INT 取值:

    ┌───────────┐
    │int(*)[100]│
    └─┃─────────┘
      ▼
    ┌───┬───┬───┬───┬───┬───┬┄
    │int│int│int│int│int│int│
    └───┴───┴───┴───┴───┴───┴┄

    在一个阵列的名称;

    当您使用地址运算符( &AMP)你会得到一个指向数组的指针。例如:

      INT ARR [100] = {/ *一些初始值* /};
    INT(* p)的[100] =安培;改编;

    在这里,我已经采取了的地址改编阵列,这给了我一个指针数组。然后,如果您要访问的数组的元素,你必须取消引用指针第一:(* P)[3] 将访问元素3



边注:

永远记住, 数组不是指针 。正如我们刚才看到的,我们可以采取一个数组的地址来获得一个指向它,就像在C ++中的任何其他(非临时)对象。数组和指针之间的唯一特殊的连接是一个数组的名字可以被隐式转换为指向数组的第一个元素。这意味着以下是有效的:

  INT ARR [100] = {/ *一些初始值* /};
为int * p =改编;

指针 P 将在改编的第一个元素点。需要注意的是 P 是的的一个指针数组,而是一个指针数组的元素。

(另请注意,有没有这样的事,作为一个数组类型的函数参数。如果你喜欢写东西 INT P [] 作为函数的参数,它被转化编译器是为int *

Kinda of a noob so don't kill me here.

What's the difference between the following codes?

int *p;         //As i understand, it creates a pointer to an variable of size int.
int *p[100];    //Don't really know what this is.
int (*p)[100];  // I have come to understand that this is a pointer to an array. 

解决方案

  1. This is a pointer to an int:

    int *p;
    

    ┌────┐
    │int*│
    └────┘
    

    It should point at an int, something like this:

    ┌────┐
    │int*│
    └─┃──┘
      ▼
    ┌───┐
    │int│
    └───┘
    

  2. This is an array of 100 pointers to int:

    int *p[100];
    

    That is, it gives you 100 pointers.

    ┌────┬────┬────┬────┬────┬────┬┄
    │int*│int*│int*│int*│int*│int*│
    └────┴────┴────┴────┴────┴────┴┄
    

    Each pointer should point an int, perhaps like this:

    ┌────┬────┬────┬────┬────┬────┬┄
    │int*│int*│int*│int*│int*│int*│
    └─┃──┴─┃──┴─┃──┴─┃──┴─┃──┴─┃──┴┄
      ▼    ▼    ▼    ▼    ▼    ▼
    ┌───┐┌───┐┌───┐┌───┐┌───┐┌───┐┌┄
    │int││int││int││int││int││int││
    └───┘└───┘└───┘└───┘└───┘└───┘└┄
    

    Of course, there's no reason they can't all point at the same int, or whatever.

    You may want to use an array of pointers if you want many pointers that you can easily iterate over. You may, for example, dynamically allocate objects and have each pointer point at a different object:

    p[0] = new int(0);
    p[1] = new int(0);
    // ...
    

    Perhaps dynamically allocating ints isn't the best example, but I think the point is clear.

  3. This is a pointer to an array of 100 int:

    int (*p)[100];
    

    That is, it gives you just 1 pointer:

    ┌───────────┐
    │int(*)[100]│
    └───────────┘
    

    It should point at an array that contains 100 ints:

    ┌───────────┐
    │int(*)[100]│
    └─┃─────────┘
      ▼
    ┌───┬───┬───┬───┬───┬───┬┄
    │int│int│int│int│int│int│
    └───┴───┴───┴───┴───┴───┴┄
    

    You will get a pointer to an array when you use the address-of operator (&) on the name of an array. For example:

    int arr[100] = { /* some initial values */ };
    int (*p)[100] = &arr;
    

    Here, I've taken the address of the arr array, which gives me a pointer to that array. If you then want to access an element of the array, you have to dereference the pointer first: (*p)[3] will access element 3.


Side note:

Always remember that arrays are not pointers. As we have just seen, we can take the address of an array to get a pointer to it, just like any other (non-temporary) object in C++. The only special connection between arrays and pointers is that the name of an array can be implicitly converted to a pointer to the array's first element. That means the following is valid:

int arr[100] = { /* some initial values */ };
int* p = arr;

The pointer p will point at the first element in arr. Note that p is not a pointer to the array, but a pointer to an element of the array.

(Also note that there is no such thing as an array type function argument. If you write something like int p[] as a function argument, it is transformed by the compiler to be a int*.)

这篇关于什么是类型之间的区别 - 为int *和为int * [100]用C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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