简体   繁体   English

C ++ char指向char变量的指针

[英]C++ char pointer to char variable

I'm given a method header as so: 我得到了这样的方法标题:

char* thisMethod(char* input){}

is it possible to say, 是否可以说,

char var = input[0];

? I know that "input" will be in the form of a char array. 我知道“输入”将采用char数组的形式。

I'm obviously new to C++ pointers are throwing me off. 我显然不是C ++指针的新手,这使我不知所措。 I tried researching how to work with char pointer function arguments but couldn't find anything specific enough to help. 我尝试研究如何使用char指针函数参数,但是找不到足够具体的方法来提供帮助。 Thanks for the help. 谢谢您的帮助。

There is a misconception that may lead you into further troubles: 有一个误解可能会导致您陷入更多麻烦:

I know that "input" will be in the form of a char array. 我知道“输入”将采用char数组的形式。

NOPE: By the scope of that function, input is a pointer to a character . 注意:在该函数的范围内, input指向字符指针 The function has no way to know where such a pointer comes from. 该函数无法知道此类指针的来源。

If it has been taken from an array upon calling the function than it will be a pointer to the first element of that array . 如果它是在调用函数时从数组中获取的,那么它将是pointer to the first element of that array

Because pointer have an arithmetic that allows to add offsets and because the [] operator applied to pointers translates as a[b] = *(a+b) by definition, in whatever code, if a is a pointer, *a and a[0] are perfect synonymous. 因为指针的算术,允许添加的偏移量,并且因为[]应用于指针操作者翻译为a[b] = *(a+b)按定义,在任何代码,如果是a指针, *aa[0]是完美的代名词。

Think to an array as a sequence of boxes and a pointer as your hand's index finger 将数组视为一系列盒子 ,将指针视为您的手的食指

adding an offset to a finger (like finger+2 ) means " re-point it aside " and de-referencing it (like *finger ) means "look inside what it points to ". 手指添加偏移量(如finger+2 )表示“将其重新指向一旁 ”,而取消引用(如*finger )则表示“在其指向的内部看”。

The [] operator on pointers is just a shortcut to do both operations at once. 指针上的[]运算符只是同时执行两项操作的快捷方式。

Arrays are another distinct thing. 数组是另一件事。 Don't think to them when dealing with pointers, since -in more complex situations, like multidimensional array or multi-indirection pointers - the expression a[b][c] won't work anymore the same way. 在处理指针时,请不要去想它们,因为-在更复杂的情况下,例如多维数组或多向指针-表达式a [b] [c]将不再以相同的方式工作。

As long as it's inside the function and input points to something valid ( not NULL / nullptr and not a garbage location ) then doing char var = input[0]; 只要它在函数内部并且input指向有效的东西(不是NULL / nullptr而不是垃圾位置),然后执行char var = input[0]; is just fine. 很好 It's the same as char var = *input . char var = *input

PS If it's supposed to be a string I recommend using std::string . PS如果应该是字符串,我建议使用std::string

There are two ways to get a value from a pointer, * and [] . 有两种方法可以从指针获取值: *[] The following are equivalent: 以下是等效的:

char var1 = *input;
char var2 = input[0];

Using the brackets is more common when you know you were passed an array, since it allows you to supply an index. 当您知道已传递数组时,使用方括号更为常见,因为它允许您提供索引。 You need some way of knowing where the end of the array is so that you don't attempt any access past it, your function is missing that important detail. 您需要某种方式来了解数组的末尾位置,以便您不会尝试通过数组进行任何访问,因为函数缺少了这一重要细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM