指向整数数组和普通整数数组的指针 [英] pointer to array of integers and normal array of integers

查看:124
本文介绍了指向整数数组和普通整数数组的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在KR C书的第112页中,内容如下:

In KR C book page 112 it says that following:

int (*arr1)[10];

是指向10个整数的数组的指针.我不明白上面和之间的区别:

is a pointer to an array of 10 integers. I don't get what's difference between above and:

int arr2[10];

1- arr2本身不是指向10个整数的数组的指针吗? (因为数组的名称本身就是一个指针.)

1- Isn't arr2 itself a pointer to array of 10 integers? (Because name of an array is a pointer itself.)

2-如果数组的名称是数组的地址和指向该数组的指针,那么arr1arr2都是指向整数数组的指针,不是吗?

2- If the name of an array is the array address and pointer to that array, then both arr1 and arr2 are pointer to array of integers, isn't this true?

推荐答案

arr2本身不是10个整数数组的指针吗?

Isn't arr2 itself a pointer to array of 10 integers?

不,它是一个数组.

不是数组的名称就是数组的地址和指向该数组的指针吗?

Isn't the name of an array the array address and pointer to that array?

可以将数组名称转换为指向第0个元素的指针 (而不是整个数组).

Array names can be/are converted to pointers to their 0th element (not the entire array).

那么arr1和arr2都是整数数组的指针吗?

So both arr1 and arr2 are pointer to array of integers?

否.

  1. arr1是指向10个整数的数组的指针.
  2. arr2是10个整数的数组.在大多数情况下,它会转换为指向整数的指针(而不是指向数组的指针).
  1. arr1 is a pointer to an array of 10 integers.
  2. arr2 is an array of 10 integers. In most contexts it converts to a pointer to an integer (not a pointer to an array).


例如检查以下错误示例:


Check this wrong example for instance:

#include <stdio.h>

int main(void)
{
    int arr2[10] = {0};
    arr2[5] = 747;

    int (*arr1)[10] = {0};
    arr1[5] = 747;

    return 0;
}

在这里,我将arr1arr2都视为同一件事",但出现此错误:

Here I am treating both arr1 and arr2 as the "same thing", and I got this error:

C02QT2UBFVH6-lm:~ gsamaras$ gcc -Wall main.c 
main.c:9:13: error: array type 'int [10]' is not assignable
    arr1[5] = 747;
    ~~~~~~~ ^
1 error generated.

但如果我这样做:

arr1[0][5] = 747;

它将通过编译!当然与(*arr1)[5] = 747;相同.

it will pass compilation! Same with (*arr1)[5] = 747; of course.

这篇关于指向整数数组和普通整数数组的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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