在 C++ 中分配指向数组的指针

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

Assigning pointers to arrays in C++

c++

提问by Bourne

I want to do something like this below:

我想在下面做这样的事情:

int main() {
   int a[10];
   int *d = generateArrayOfSize(10) // This generates an array of size 10 on the heap
   a = d;
   print(a); // Prints the first 10 elements of array.
}

However above code gives compilation error (incompatible types in assignment of ‘int*' to ‘int [10]'). What can I do to make the above code to work?

然而,上面的代码给出了编译错误('int*' 到 'int [10]' 的赋值中的类型不兼容)。我该怎么做才能使上述代码正常工作?

回答by Lightness Races in Orbit

Arrays are non-assignable and non-copyable, so you'd have to copy each element by hand (in a loop), or using std::copy.

数组是不可赋值和不可复制的,因此您必须手动(在循环中)复制每个元素,或使用std::copy.

回答by cdmh

If you're using C++, then use C++ arrays rather than C style arrays and pointers. Here's an example

如果您使用 C++,则使用 C++ 数组而不是 C 风格的数组和指针。这是一个例子

#include <array>
#include <iostream>

template<size_t N>
std::array<int, N> generateArrayOfSize(void)
{
    std::array<int, N> a;
    for (int n=0; n<N; ++n)
        a[n] = n;
    return a;
}

template<size_t N>
void print(std::array<int, N> const &a)
{
    for (auto num : a)
        std::cout << num << " ";
}

int main() {
   std::array<int, 10> a;
   std::array<int, 10> d = generateArrayOfSize<10>();
   a = d;
   print(a); // Prints the first 10 elements of array.
}

which outputs 0 1 2 3 4 5 6 7 8 9

哪个输出 0 1 2 3 4 5 6 7 8 9

回答by Pierre Fourgeaud

Arrays are not pointers.

数组不是指针。

You can't do :

你不能这样做:

int a[10];
int *d;
a = d;

Change it to :

将其更改为:

int *a;
int *d;
a = d;

Main differences between arrays and pointers in C programming :

C 编程中数组和指针的主要区别:

Pointer                                    | Array
-------------------------------------------|-------------------------------------------
A pointer is a place in memory that keeps  | An array is a single, pre allocated chunk
address of another place inside            | of contiguous elements (all of the same
                                           | type), fixed in size and location.
-------------------------------------------|-------------------------------------------
A pointer can point to a dynamically       | They are static in nature. Once memory is 
allocated memory. In this case, the memory | allocated , it cannot be resized or freed
allocation can be resized or freed later.  | dynamically.
-------------------------------------------|-------------------------------------------

You have a quite good explanation here : https://stackoverflow.com/a/7725410/1394283

你在这里有一个很好的解释:https: //stackoverflow.com/a/7725410/1394283

回答by nullptr

An array is nota pointer (although a name of an array often decays to a pointer to its first element).

数组不是指针(尽管数组的名称通常会衰减为指向其第一个元素的指针)。

To make the above code to work, you can declare aas a pointer: int *a;. The printfunction takes an int*(or a decayed array) anyway.

为了让上面的代码工作,你可以声明a为指针:int *a;。无论如何,该print函数都需要一个int*(或一个衰减的数组)。

If you really want to have two arrays and copy contents from one array to another, you should copy the data in a loop.

如果您确实想要拥有两个数组并将内容从一个数组复制到另一个数组,则应该在循环中复制数据。

回答by Root

This will print in this way when you assign a string reference to a pointer you have to use *ptr to print the value of a pointer otherwise in your case print(d)that is like cout< in c++ it will only print the location of the d[0].

当您将字符串引用分配给指针时,这将以这种方式打印,您必须使用 *ptr 打印指针的值,否则在您的情况下print(d)就像 cout< 在 C++ 中它只会打印位置d[0]。

 int ary[5]={1,2,3,4,5};
 int *d;
 d=ary;
 for(int i=0;i<5;i++)
 cout<<*(d+i);

回答by Ryan Williams

Little rusty with my C++ but try something like this.

我的 C++ 有点生疏,但尝试这样的事情。

int main() {
   int *a;
   int *d = generateArrayOfSize(10) // This generates an array of size 10 on the heap
   a = d;
   print(a); // Prints the first 10 elements of array.
}

回答by Yu Hao

Because array names are non-modifiable. So you can't do

因为数组名称是不可修改的。所以你不能做

a = d;

Declare it as a pointer like this:

将其声明为这样的指针:

int *a;