C语言 使用指向 char 数组的指针,可以访问该数组中的值吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7280379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:34:30  来源:igfitidea点击:

Using pointer to char array, values in that array can be accessed?

cpointers

提问by Pravi

I created ptr as pointer to an array of 5 chars.

我创建了 ptr 作为指向 5 个字符数组的指针。

char (*ptr)[5];

assigned it the address of a char array.

分配给它一个字符数组的地址。

char arr[5] = {'a','b','c','d','e'};
ptr = &arr;

using pointer ptr can I access the char values in this array?

使用指针 ptr 我可以访问这个数组中的字符值吗?

printf("\nvalue:%c", *(ptr+0));

It does not print the value.

它不打印值。

In my understanding ptr will contain the base address of array but it actually point to the memory required for the complete array (i.e 5 chars). Thus when ptr is incremented it moves ahead by sizeof(char)*5bytes. So is it not possible to access values of the array using this pointer to array?

根据我的理解,ptr 将包含数组的基地址,但它实际上指向完整数组所需的内存(即 5 个字符)。因此,当 ptr 增加时,它会向前移动sizeof(char)*5字节。那么是不是不可能使用这个指向数组的指针来访问数组的值呢?

采纳答案by nos

When you want to access an element, you have to first dereference your pointer, and then index the element you want (which is also dereferncing). i.e. you need to do:

当你想访问一个元素时,你必须先解引用你的指针,然后索引你想要的元素(这也是解引用)。即你需要做:

printf("\nvalue:%c", (*ptr)[0]);, which is the same as *((*ptr)+0)

printf("\nvalue:%c", (*ptr)[0]);,这与 *((*ptr)+0)

Note that working with pointer to arrays are not very common in C. instead, one just use a pointer to the first element in an array, and either deal with the length as a separate element, or place a senitel value at the end of the array, so one can learn when the array ends, e.g.

请注意,使用指向数组的指针在 C 中并不常见。相反,只使用指向数组中第一个元素的指针,并将长度作为单独的元素处理,或者在末尾放置一个标记值数组,因此可以了解数组何时结束,例如

char arr[5] = {'a','b','c','d','e',0}; 
char *ptr = arr; //same as char *ptr = &arr[0]

printf("\nvalue:%c", ptr[0]);

回答by Lundin

Most people responding don't even seem to know what an array pointer is...

大多数回应的人似乎都不知道数组指针是什么......

The problem is that you do pointer arithmetics with an array pointer: ptr + 1 will mean "jump 5 bytes ahead since ptr points at a 5 byte array".

问题是您使用数组指针进行指针算术运算:ptr + 1 表示“向前跳转 5 个字节,因为 ptr 指向 5 个字节的数组”。

Do like this instead:

这样做:

#include <stdio.h>

int main()
{
  char (*ptr)[5];
  char arr[5] = {'a','b','c','d','e'};
  int i;

  ptr = &arr;
  for(i=0; i<5; i++)
  {
    printf("\nvalue: %c", (*ptr)[i]);
  }
}

Take the contents of what the array pointer points at and you get an array. So they work just like any pointer in C.

获取数组指针指向的内容,您会得到一个数组。所以它们就像 C 中的任何指针一样工作。

回答by Hemaraj Jupudi

Use of pointer before character array

在字符数组前使用指针

Normally, Character array is used to store single elements in it i.e 1 byte each

通常,字符数组用于在其中存储单个元素,即每个元素 1 个字节

eg:

例如:

char a[]={'a','b','c'};

we can't store multiple value in it.

我们不能在其中存储多个值。

by using pointer before the character array we can store the multi dimensional array elements in the array

通过在字符数组之前使用指针,我们可以将多维数组元素存储在数组中

i.e.

IE

char *a[]={"one","two","three"};
printf("%s\n%s\n%s",a[0],a[1],a[2]);

回答by qbert220

Your should create ptr as follows:

您应该按如下方式创建 ptr:

char *ptr;

You have created ptr as an array of pointers to chars. The above creates a single pointer to a char.

您已创建 ptr 作为指向字符的指针数组。上面创建了一个指向 char 的指针。

Edit: complete code should be:

编辑:完整的代码应该是:

char *ptr;
char arr[5] = {'a','b','c','d','e'};
ptr = arr;
printf("\nvalue:%c", *(ptr+0));