English 中文(简体)
C++ 职能参数中的Array Pointer [复制]
原标题:C++ Array Pointer in Function Parameters [duplicate]

请允许我向我解释这些职能是否相同?

void doubleArray(int* values, int length)

void doubleArray(int values[], int length)

我不得不改变我的法典,以完全相同,但我不相信逻辑。 我预计至少必须改变职务中写的内容。 如果需要的话,全面法典如下:

  #include <iostream>

    using std::cout;
    using std::endl;

void doubleArray(int* values, int length);

int main() {

    int length = 10;
    int values[length];

    for (int i = 0; i < length; i++){
        values[i] = i;
    }

    doubleArray(values, length);

    for (int i = 0; i < length; i++) {
      cout << values[i] << endl;
   }

}

void doubleArray(int* values, int length) {
    for(int i = 0; i < length; i++){
        values[i] = values[i] * 2;
    }
}
问题回答

From https://en.cppreference.com/w/cpp/language/function#Parameter_list:

The type of each function parameter in the parameter list is determined according to the following rules:

[。]

2) If the type is "array of T" or "array of unknown bound of T", it is replaced by the type "pointer to T"

[。]

由于这些规则,以下职能声明完全相同:

int f(char[]);
int f(char* s);
int f(char* const);
int f(char* volatile s);

他们为什么也一样? 由于这符合C++(从C继承)的规则。

C的设计者认为,能够把一个阵列变成一个功能,是一种坏的想法。 他们认为,最好是对阵列的第一点进行点击。 问题(我认为)。

但是,他们还决定,他们不想在功能参数申报中放弃阵列。 因此,他们决定,当作为功能参数使用时,阵列表。 T param[] is only a different way of written the pointer form, T* param. 如果你指明阵列的大小(被忽略),那么,T param [10]<>> > 代码也是个点。 更值得怀疑(我认为)。

在C++中,通过<代码>std:vectorstd:array,你能够避免这一切。 在C,你不是这样幸运的。

附录:

int values[10];

分配给10个帐篷的一组记忆被分配在盒子上,可变的<条码>价值<> 代码>包含这种记忆阵列开始的地址,或以另一种方式处理阵列零件,即相同。

因此,

&values[0] == values

总是真实的。

同样,这两项声明也涉及同样的内容。

cout << values[5];
cout << *(values + 5);

因此, if we have a function parameter (or any other variable), it can be declared as of type int* or int[] and both will accept values, because both declarations are merely alternative ways of declaring the same thing - a pointer. .

这可能会造成混乱,因为这两项要求都有效:

void foo (int[] vals);

int values[10];
foo (values); // Passing an array

int x = 0;
int* px = &x:
foo (px);     // Passing a single object by address

So as a matter of style, if a function expects an array, it should take the array for (int[]) and a length. If it expects a single object it should take a pointer (int*).





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签