C ++:传递数组的方法和数组的指针相同吗? [英] C++: Is the same passing an array than a pointer to array?

查看:138
本文介绍了C ++:传递数组的方法和数组的指针相同吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我猜想k1的值不会在主空间。但后来我意识到一个数组是一个指针,所以有什么区别吗?我认为是一样的,但也许任何人都可以找到一些其他的技术差异。也许更快传递指针?

At first I would guess that the values of k1 won't be in the main space. But then I realized that an array is a pointer, so is there any difference? I think that is the same, but maybe anyone can find some other technical differences. Maybe is faster passing a pointer?

#include <iostream>

using namespace std;

void g(double [],int );
void f(double* [],int );

int main()
{

    int n = 10;
    double *k1, *k2;

    k1 = new double[n];
    k2 = new double[n];

    g(k1,n);
    f(&k2,n);

    for(int i = 0;i <n;i++)
    {
        cout << k1[i]<< " ";
        cout << k2[i] << endl;
    }

    delete [] k1;
    delete [] k2;

    return 0;
}


void g(double h[],int n)
{
     for(int i = 0;i <n;i++)
        h[i]=i;
}

void f(double* h[],int n)
{
     for(int i = 0;i <n;i++)
        (*h)[i]=i;
}


推荐答案

首先,不是指针。这是一个10倍的数组:

First of all, arrays are not pointers. This is an array of 10 doubles:

double a[10];

这是一个双重指针:

double *p;

这是一个指向10个双精度数组的指针:

This is a pointer to an array of 10 doubles:

double (*pa)[10];

这是一个指向双精度数组的指针:

And this is an array of pointers to doubles:

double *pa[10];

数组和指针只有在声明为函数参数时才相同。但数组可以转换(衰减)为数组的第一个元素的指针:

Arrays and pointers are only the same when declared as function arguments. But arrays can be converted (decay) into pointers to the first elemento of the array:

double a[10];
double *p = a;
double *q = &a[0]; //same as p

在您的示例代码中:

double *x = new double[10];

您正在创建一个10个双精度的动态数组,并获取该数组的第一个元素的指针。您还可以创建一个新数组并获取指向数组的指针:

You are creating a dynamic array of 10 doubles and getting a pointer to the first element of that array. You could also create a new array and get a pointer-to-array:

double (*x)[10] = new double (*)[10];

但是这种奇怪的语法很少用。

But this show of weird syntax is seldom useful.

关于函数,这是一个取双精度数组的函数:

About functions, this is a function taking an array of doubles:

void g1(double h[], int n);

这是一个指针指向10个双精度数组的函数:

And this is a function taking a pointer to an array of 10 doubles:

void g2(double (*h)[10]);

在指针到数组的情况下,你需要指定数组的大小,创建一个指向未知大小数组的指针。

In the pointer-to-array case you need to specify the size of the array, because you cannot create a pointer to an unknown-size array.

这就是为什么数组实际上是作为指针传递给函数的。但指向数组的第一个成员的指针,而不是指向数组本身的指针。因此,第一个函数实际上与这个函数相同:

That's why arrays are actually passed to functions as pointers. But pointers to the first member of the array, not pointers to the array itself. So the first function is actually identical to this one:

void g1(double *h, int n);

由于你只将指针传递给数组的第一个成员以指定数组的大小,在一个附加的参数中。优点是你可以传递任何大小的数组。甚至数组的拼接:

Since you only pass the pointer to the first member of the array (pointer to double) you need also to specify the size of the array, in an additional paramenter. The advantage is that you can pass arrays of any size. And even splices of an array:

double x[20];
g1(x + 10, 5); //pass x[10..15]

关于哪一个更快,他们都是一样的,它们实际上是指向同一存储器地址的指针。请注意,数组只有通过复制才能传递,如果它们是结构的一部分,并且结构体通过值传递。

About which one is faster, they are both the same, they are actually pointers to the same memory address. Note that arrays are only passed by copy if they are part of a struct, and the struct is passed by value.

我的建议是:如果你使用C坚持惯用的C,在必要时使用指向数组的第一个成员的指针,并避免指针到数组的语法如果可能。如果你使用C ++使用容器,迭代器和范围。

My recommendation is: if you use C stick to the idiomatic C, use pointers to the first member of the array when necessary, and avoid the pointer-to-array syntax when possible. If you use C++ use containers, iterators and ranges.

这篇关于C ++:传递数组的方法和数组的指针相同吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆