3

Imagine I have the following code:

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

Would there be a way to link the first value of firstArray to the first value of secondArray so that if I do:

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)

As far as I've tested and researched I can't do something along those lines.

P.S.: This is to create a Samurai Sudoku (in which some chunks of numbers are shared), so when I modify a value that 2 sudokus share it would be updated in both.

2
  • 2
    Take a look at std::reference_wrapper. Commented Apr 28, 2015 at 17:48
  • 2
    The value 1 is likely not a valid value to assign to an int*... Perhaps you've abstracted your actual use case a bit too much... Commented Apr 28, 2015 at 17:59

3 Answers 3

2

You are mixing up concepts. You don't even need to declare a second array just do this:

    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. In fact, arrays work very much like pointers to their first elements, and, actually, an array can always be implicitly converted to the pointer of the proper type. The [] operator in arrays acts the same way as a as a de-referencing operator, but with the added ability of automatically advancing the pointer according to the data type. This is why Array[1] references the same value as *(Array+1)

however you are declaring an arrays of pointers to integers, which means that you can't "store" integer values in this array, but rather store the value of the address where the integer is located. Additionally when you declare an array you are essentially declaring a constant pointer, so you can not "steal" it and make it pointer to another location.

Study this Code and it's output (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;
}

OUTPUT

5 10 15 
0x786378c0b860
0x786378c0b880
0x786378c0b860
0x786378c0b880
0x786378c0b840
0x786378c0b840
0x786378c0b840
0x786378c0b840
5
5
5
5
77
77
77
77
77
99
99
99
99
Sign up to request clarification or add additional context in comments.

Comments

0

Store int**s instead of int*s this way you can have each entry in the array point to the same address.

You would initialize the first element lets say as:

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

Then if you write *first = 20; both arrays will be updated.

There is a problem with your question that you initialize first to 0 and then to 1. If they were the same address then setting to 1 would overwrite the setting to 0.

Comments

0

You question is not self coherent :

  • you write int* secondArray[4]; : secondArray is an array of 4 pointers to int
  • you write secondArray[0] = 20 : here secondArray is an array of 4 int values

Hyp 1 : arrays of 4 int values

&secondArray[1] is &secondArray[0] + 1. Full stop. That the way arrays work in C++. In that case if secondArray[0] is the same as firstArray[0] then the 2 arrays are at same address and are in fact the same array and your example becomes :

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 : arrays of 4 pointers

You can have first element of both array point to same value while the three others are independent

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]);

Then :

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

and

*(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)

That is what you asked for, but it is a very unusual requirement and I'm not sure it is really what you need.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.