将一个数组值指向同一个数组的另一个值 - C ++

时间:2015-04-28 17:41:56

标签: c++ arrays pointers

想象一下,我有以下代码:

int* firstArray[4];
fill_n(firstArray, 4, 0);
//firstArray values are: 0, 0, 0, 0

int* secondArray[4];
fill_n(secondArray, 4, 1);
//secondArray values are: 1, 1, 1, 1

是否有办法将firstArray的第一个值链接到secondArray的第一个值,以便我这样做:

secondArray[0] = 20;
//secondArray values would be: 20, 1, 1, 1 (because I just changed it)
//firstArray values would be: 20, 0, 0, 0 (because it's pointing to the value I just changed)

就我经过测试和研究而言,我无法做到这一点。

P.S。:这是为了创建一个武士数独(其中共享一些数字块),所以当我修改2个sudokus共享的值时,它将在两者中更新。

3 个答案:

答案 0 :(得分:2)

你正在混淆概念。您甚至不需要声明第二个数组:

    int myarray [20];
    int * mypointer;
    mypointer = myarray;
   // then you can use both mypointer[] myarray[] the same way to access array elements

<强> The concept of arrays is related to that of pointers 即可。事实上,数组的工作方式非常类似于指向第一个元素的指针,实际上,数组总是可以隐式转换为正确类型的指针。 数组中的[]运算符与作为解引用运算符的方式相同,但具有根据数据类型自动推进指针的附加功能。这就是Array[1]引用与*(Array+1)

相同的值的原因

但是你要声明一个指向整数的指针数组,这意味着你不能在这个数组中“存储”整数值,而是存储整数所在地址的值。 此外,当您声明一个数组时,您实际上是在声明一个常量指针,因此您无法“窃取”它并将其指向另一个位置。

研究此代码及其输出 RUN THIS CODE

#include <iostream>
using namespace std;

void printarray (int arg[], int length) {
  for (int n=0; n<length; ++n)
    cout << arg[n] << ' ';
  cout << '\n';
}

int main ()
{
  int first[] = {5, 10, 15, 14, 13};

  printarray (first,3);
// int* third[] = {1,1,1}; Not accepted because int is not int*

  // storing the addresses of first as pointers in 2 different arrays
  int* third[] = {first,first+2,first+3};
  int* forth[] = {first,first+2,first+3};

  // the memory adress where the pointers TO first is stored
  cout << third << endl;
  cout << forth << endl;
  cout << &third << endl;
  cout << &forth << endl;
  // the memory adress where the pointer TO the value of first[0] is stored 
  cout << *third << endl;
  cout << *forth << endl;
  cout << third[0] << endl;
  cout << forth[0] << endl;
  // you are defrencing twice
  cout << *third[0] << endl;
  cout << *forth[0] << endl;
  cout << **third << endl;
  cout << **forth << endl;
  // assign once
  first[0] = 77;
  // applys to all values
  cout << first[0] << endl;
  cout << *third[0] << endl;
  cout << *forth[0] << endl;
  // better yet declare a int* and use it same way your array
  int* second;
  second = first;
  cout << first[0] << endl;
  cout << second[0] << endl;
  // again change value and the change is reflected everywhere
  second[0] = 99;
  cout << first[0] << endl;
  cout << second[0] << endl;
  cout << *third[0] << endl;
  cout << *forth[0] << endl;
}

<强>输出

5 10 15 
0x786378c0b860
0x786378c0b880
0x786378c0b860
0x786378c0b880
0x786378c0b840
0x786378c0b840
0x786378c0b840
0x786378c0b840
5
5
5
5
77
77
77
77
77
99
99
99
99

答案 1 :(得分:0)

以这种方式存储int**而不是int*,您可以让数组中的每个条目指向同一地址。

您将初始化第一个元素,例如:

int *first = new int;
*first = 1;
firstArray[0] = first;
secondArray[0] = first;

然后,如果你写*first = 20;,两个数组都将被更新。

您的问题是首先将其初始化为0然后再初始化为1.如果它们是相同的地址,则设置为1会将设置覆盖为0.

答案 2 :(得分:0)

你的问题不是自我连贯的:

  • 你写int* secondArray[4];secondArray是一个由4个指向int
  • 的数组
  • 你写secondArray[0] = 20:这里secondArray是一个包含4个int值的数组

Hyp 1:4个int值的数组

&secondArray[1]&secondArray[0] + 1。完全停止。数组在C ++中的工作方式。在这种情况下,如果secondArray[0]firstArray[0]相同,则2个数组位于相同的地址,实际上是相同的数组,您的示例变为:

int firstArray[4];
fill_n(firstArray, 4, 0);
//firstArray values are: 0, 0, 0, 0

/* int secondArray[4];  first elements could not be related and you should have : */
int *secondArray = firstArray;
fill_n(secondArray, 4, 1);
//secondArray values are: 1, 1, 1, 1
//firstArray values are also: 1, 1, 1, 1

Hyp 2:4指针数组

您可以让两个数组的第一个元素指向相同的值,而其他三个元素是独立的

int array[5];
int *firstArray[4];
int *secondArray[4];
firstArray[0] = &(array[0]);
for(int i=1; i<4; i++)
    firstArray[i] = secondArray[i] = &(array[1]);
}
secondArray[0] = &(array[4]);

然后:

int* firstArray[4];
fill_n(firstArray, 4, 0);
//firstArray values are: 0, 0, 0, 0

int* secondArray[4];
fill_n(secondArray, 4, 1);
//secondArray values are: 1, 1, 1, 1
//firstArray values are: 1, 0, 0, 0

*(secondArray[0]) = 20;
//secondArray values would be: 20, 1, 1, 1 (because I just changed it)
//firstArray values would be: 20, 0, 0, 0 (because it's pointing to the value I just changed)

这就是你要求的,但这是一个非常不寻常的要求,我不确定它真的是你需要的。