通过使用C中的指针数组交换++ [英] Swap arrays by using pointers in C++

查看:205
本文介绍了通过使用C中的指针数组交换++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有指针的两个数组来,我需要交换双打。而不仅仅是阵列内的复制数据,这将是更有效的只是指针交换到所述阵列。我总是在IM pression数组的名字是本质上只是指针下,但下面code接收到一个编译器错误:

I have two arrays of pointers to doubles that I need to swap. Rather than just copy the data within the arrays, it would be more efficient just to swap the pointers to the arrays. I was always under the impression that array names were essentially just pointers, but the following code receives a compiler error:

double left[] = {1,2,3};
double right[] = {9,8,7};

double * swap = left;
left = right; // Error "ISO C++ forbids assignment of arrays"
right = swap; // Error "incompatible types in assignment of `double*' to `double[((unsigned int)((int)numParameters))]'"

创建动态数组可以解决问题,但不能在我的应用程序来完成。我如何使这项工作?

Creating the arrays dynamically would solve the problem, but can't be done in my application. How do I make this work?

推荐答案

数组是不一样的指针,并且不能在您所描述的方式进行交换。要做到指针交换技巧,你必须使用指针,无论是动态分配的内存,或使用指针来访问数据(丹尼尔描述的方式)。

Arrays are not the same as pointers and cannot be swapped in the way you describe. To do the pointer swap trick, you must use pointers, either dynamically allocate the memory, or use pointers to access the data (in the way Daniel has described).

这篇关于通过使用C中的指针数组交换++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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