C++,制作指向字符数组的指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9670385/
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-08-27 13:08:56  来源:igfitidea点击:

C++, Making a pointer to a char array

c++pointers

提问by James MV

In C++, I have a char array defined as:

在 C++ 中,我有一个 char 数组定义为:

char miniAlphabet[] = {'A','B','C','D','E', '
char miniAlphabet[] = {'A','B','C','D','E', '
char *p1 = miniAlphabet;

char *p2 = &(miniAlphabet[0]);
'};
'};

I want to modify the values of this array from other functions without passing it to those functions. This is where you use pointers right?

我想从其他函数修改这个数组的值而不将它传递给这些函数。这就是你使用指针的地方吗?

So my question is what is the correct way to make a pointer to this char array. Would I just make a pointer to the first element and then when I want to modify the values I step through memory until I hit the end character?

所以我的问题是制作指向这个 char 数组的指针的正确方法是什么。我会不会只创建一个指向第一个元素的指针,然后当我想修改值时,我会逐步遍历内存直到遇到结束字符?

回答by xanatos

char *p3 = &miniAlphabet[0];

These are equivalent.

这些是等价的。

char *p4 = &miniAlphabet[0] + 1;

Note that the ()are useless for the operators' precedence, so

请注意,()对于运算符的优先级是无用的,所以

char *p5 = &miniAlphabet[1];

In C/C++ the name of an array is a pointer to the first element of the array.

在 C/C++ 中,数组的名称是指向数组第一个元素的指针。

You can then use pointers' math to do some magic...

然后你可以使用指针的数学来做一些魔术......

This will point to the second element:

这将指向第二个元素:

char *p6 = miniAlphabet + 1;

like

喜欢

function changeArray(char[] myArray);
function changeArray(char* myArray);

or

或者

changeArray(miniAlphabet);

回答by Diego

The position of the array in memory is the same as the position of its first element. So for example, both function signatures below are equivalent:

数组在内存中的位置与其第一个元素的位置相同。例如,下面的两个函数签名是等效的:

changeArray(&(miniAlphabet[0]));

And you can call either version like this:

您可以像这样调用任一版本:

funcName(miniAlphabet);

...or

...或者

void funcName(char *miniAlphabet) {
   ...
}

回答by CFL_Jeff

If you pass an array to a function, you ARE passing by reference as an array variable is really just a pointer to the address of the first element in the array. So you can call a function like this:

如果您将数组传递给函数,那么您是通过引用传递的,因为数组变量实际上只是指向数组中第一个元素地址的指针。所以你可以调用这样的函数:

void foo(char *array)
{
    array[0] = 'z';
    array[2] = 'z';
}

...

char myArray[] = { 'a', 'b', 'c'};
foo(myArray);
//myArray now equals  {'z', 'b', 'z'}

and receive the array "by reference" like this:

并像这样“通过引用”接收数组:

##代码##

回答by gettingSmarter

In short, yes. When you create the array miniAlphabet is a pointer to the array of the size and type you defined. Which is also the pointer to the first element.

简而言之,是的。当您创建数组 miniAlphabet 是指向您定义的大小和类型的数组的指针。这也是指向第一个元素的指针。

An example of what you are suggesting.

你所建议的一个例子。

##代码##

also of note, dereferencing myArray gives you the value in the first element.

同样值得注意的是,取消引用 myArray 会为您提供第一个元素中的值。