c语言中指针数组和数组指针_C中的指针和数组

c语言中指针数组和数组指针

When an array is declared, compiler allocates sufficient amount of memory to contain all the elements of the array. Base address i.e address of the first element of the array is also allocated by the compiler.

声明数组时,编译器分配足够的内存以包含数组的所有元素。 基地址,即数组第一个元素的地址也由编译器分配。

Suppose we declare an array arr,

假设我们声明一个数组arr

int arr[5] = { 1, 2, 3, 4, 5 };

Assuming that the base address of arr is 1000 and each integer requires two bytes, the five elements will be stored as follows:

假设arr的基址为1000,并且每个整数需要两个字节,则五个元素的存储方式如下:

address of array

Here variable arr will give the base address, which is a constant pointer pointing to the first element of the array, arr[0]. Hence arr contains the address of arr[0] i.e 1000. In short, arr has two purpose - it is the name of the array and it acts as a pointer pointing towards the first element in the array.

此处,变量arr将给出基址,该基址是指向数组第一个元素arr[0]的常量指针。 因此, arr包含arr[0]的地址,即1000 。 简而言之, arr有两个用途-它是数组的名称,它充当指向数组中第一个元素的指针。

arr is equal to arr等于 &arr[0] by default &arr[0]默认

We can also declare a pointer of type int to point to the array arr.

我们还可以声明一个int类型的指针来指向数组arr

int *p;
p = arr;  
// or, 
p = &arr[0];    //both the statements are equivalent.

Now we can access every element of the array arr using p++ to move from one element to another.

现在,我们可以使用p++访问数组arr每个元素,以从一个元素移动到另一个元素。

NOTE: You cannot decrement a pointer once incremented. p-- won't work.

注意:一旦增加,就不能减少指针。 p--不起作用。

指向数组的指针 (Pointer to Array)

As studied above, we can use a pointer to point to an array, and then we can use that pointer to access the array elements. Lets have an example,

如上所述,我们可以使用一个指针指向一个数组,然后可以使用该指针访问数组元素。 让我们举个例子

#include <stdio.h>

int main()
{
    int i;
    int a[5] = {1, 2, 3, 4, 5};
    int *p = a;     // same as int*p = &a[0]
    for (i = 0; i < 5; i++)
    {
        printf("%d", *p);
        p++;
    }
    
    return 0;
}

In the above program, the pointer *p will print all the values stored in the array one by one. We can also use the Base address (a in above case) to act as a pointer and print all the values.

在上面的程序中,指针*p将一一打印存储在数组中的所有值。 我们也可以用基地址( a在上述情况下)作为一个指针和打印所有的值。

Using Array name as pointer in C

The generalized form for using pointer with an array,

将指针与数组配合使用的一般形式,

*(a+i)

is same as:

与:

a[i]

指向多维数组的指针 (Pointer to Multidimensional Array)

A multidimensional array is of form, a[i][j]. Lets see how we can make a pointer point to such an array. As we know now, name of the array gives its base address. In a[i][j], a will give the base address of this array, even a + 0 + 0 will also give the base address, that is the address of a[0][0] element.

多维数组的形式为a[i][j] 。 让我们看看如何使指针指向这样的数组。 众所周知,数组的名称给出了它的基地址。 在a[i][j]a将给出该数组的基地址,即使a + 0 + 0也将给出基地址,即a[0][0]元素的地址。

Here is the generalized form for using pointer with multidimensional arrays.

这是将指针与多维数组配合使用的通用形式。

*(*(a + i) + j)

which is same as,

与...相同

a[i][j]

指针和字符串 (Pointer and Character strings)

Pointer can also be used to create strings. Pointer variables of char type are treated as string.

指针也可以用来创建字符串。 char类型的指针变量被视为字符串。

char *str = "Hello";

The above code creates a string and stores its address in the pointer variable str. The pointer str now points to the first character of the string "Hello". Another important thing to note here is that the string created using char pointer can be assigned a value at runtime.

上面的代码创建一个字符串并将其地址存储在指针变量str 。 现在,指针str指向字符串“ Hello”的第一个字符。 这里要注意的另一件事是,可以在运行时为使用char指针创建的字符串分配一个值。

char *str;
str = "hello";      //this is Legal

The content of the string can be printed using printf() and puts().

可以使用printf()puts()来打印字符串的内容。

printf("%s", str);
puts(str);

Notice that str is pointer to the string, it is also name of the string. Therefore we do not need to use indirection operator *.

注意, str是指向字符串的指针,它也是字符串的名称。 因此,我们不需要使用间接运算符*

指针数组 (Array of Pointers)

We can also have array of pointers. Pointers are very helpful in handling character array with rows of varying length.

我们也可以有指针数组。 指针在处理长度可变的字符数组时非常有用。

char *name[3] = { 
    "Adam",
    "chris",
    "Deniel"
};
//Now lets see same array without using pointer
char name[3][20] = { 
    "Adam",
    "chris",
    "Deniel"
};
Pointer with character array in c

In the second approach memory wastage is more, hence it is prefered to use pointer in such cases.

在第二种方法中,内存浪费更多,因此,在这种情况下,最好使用指针。

When we say memory wastage, it doesn't means that the strings will start occupying less space, no, characters will take the same space, but when we define array of characters, a contiguos memory space is located equal to the maximum size of the array, which is a wastage, which can be avoided if we use pointers instead.

当我们说内存浪费时,这并不意味着字符串将开始占用更少的空间,不是,字符将占用相同的空间,但是当我们定义字符数组时,连续的内存空间位于等于字符的最大大小的位置。数组,这是一种浪费,如果改为使用指针,则可以避免这种情况。

翻译自: https://www.studytonight.com/c/pointers-with-array.php

c语言中指针数组和数组指针

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值