简体   繁体   English

C ++-指针函数参数

[英]C++ - pointer function parameter

I asked a question here: C++ - class issue 我在这里问了一个问题: C ++-类问题

What I'm still not getting is the pointer parameter here: 我仍然没有得到的是这里的指针参数:

void setInfo(char *strName,int id,double wage)

Where it is called by: 调用者:

abder.setInfo("Abder-Rahman",123,400);

I know that the name of the array is a pointer. 我知道数组的名称是一个指针。 But, why should we have to have a pointer data type? 但是,为什么我们必须要有一个指针数据类型? Cannot we use a char[] in the function parameter list? 我们不能在函数参数列表中使用char[]吗? As I think I got an error when I tried that. 我认为尝试此操作时遇到错误。

Thanks. 谢谢。

But, why should we have to have a pointer data type? 但是,为什么我们必须要有一个指针数据类型?

Because the size of the arguments to the function needs to be known when the function is compiled. 因为在编译函数时需要知道函数参数的大小。 Ie the function needs to know how far up the list of arguments in memory to find id and wage . 也就是说,该函数需要知道内存中的参数列表有多远才能找到idwage

Cannot we use a char[] in the function parameter list? 我们不能在函数参数列表中使用char []吗?

Yes you can, but it is still passed by pointer. 是的,您可以,但是仍然通过指针传递。 None of the additional attributes the data has as part of being an array (such as sizeof() returning the total size of the array) are preserved inside the called function. 数据作为数组的一部分而具有的其他任何属性(例如sizeof()返回数组的总大小)都不会保留在调用的函数内。 The ability to use [] in the function signature is just an indicator for you that the item passed should be an array of some type (rather than a pointer to, say, a structure). 在函数签名中使用[]的能力只是一个指示,表明所传递的项目应该是某种类型的数组(而不是指向结构的指针)。

For reference, from the C++03 standard, § 8.3.5 3: 作为参考,来自C ++ 03标准, 第8.3.5节 3:

After determining the type of each parameter, any parameter of type “array of T” or “function returning T” is adjusted to be “pointer to T” or “pointer to function returning T,” respectively. 在确定每个参数的类型之后,将类型为“ T的数组”或“返回T的函数”的任何参数分别调整为“指向T的指针”或“返回T的函数指针”。

Furthermore, an array declared with [] is an incomplete object type ( § 8.3.4 1), though this shouldn't cause a problem when declaring a function parameter as it will be converted to a pointer. 此外,用[]声明的数组是不完整的对象类型( 第8.3.4 1 ),尽管在声明函数参数时不会引起问题,因为它将被转换为指针。

char[] is also a pointer. char []也是一个指针。 And yes, you could use it. 是的,您可以使用它。

An array and a pointer are very much related - you can even use a pointer like an array, like 数组和指针非常相关-您甚至可以像数组一样使用指针,例如

void foo(char *ptr) {
    ptr[2] = 'x';
}

In your case, you get a compilation error (or warning) because string literals (like "foo" ) are considered const, ie they may not be modified, and since a function that doesn't take a const pointer doesn't make a promise not to change it, the compiler refuses to hand it a string literal. 在您的情况下,您会遇到编译错误(或警告),因为字符串文字(例如"foo" )被认为是const,即它们可能不会被修改,并且因为不使用const指针的函数不会使承诺不进行更改,编译器拒绝将其传递给字符串文字。

You'd need to do 你需要做

void setInfo(const char *strName,int id,double wage)

In general, you should always use const for pointer and reference arguments unless you're planning to modify the object. 通常,除非打算修改对象,否则应始终将const用于指针和引用参数。

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

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