Cast one array value to another value of the same array - C ++

Suppose 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

      

Will there be a way to bind the first value of firstArray to the first value of secondArray, so if I do this:

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 pointing to the value I just changed)

      

As far as I've tested and researched, I can't seem to do anything in this direction.

PS: This is for creating samurai sudoku (in which some pieces of numbers are split), so when I change the value that 2 sudokus separates, it will update in both.

+3


source to share


3 answers


You are confusing 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 the concept of pointers . In fact, arrays work very much like pointers to their first elements, and, in fact, an array can always be implicitly converted to a pointer of the correct type. The operator[]

on arrays acts the same as the de-reference operator, but with the added ability to automatically advance the pointer according to the data type. This is why itArray[1]

refers to the same meaning as*(Array+1)

however, you are declaring arrays of pointers to integers, which means that you cannot "store" integer values ​​in that array, but rather store the value of the address where the integer is located. Also, when you declare an array, you are essentially declaring a constant pointer, so you cannot "steal" it and make it a pointer to a different location.



Examine this code and 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

      

+1


source


Store int**

instead int*

such that each entry in the array points to the same address.

You would initialize the first element like:

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

      



Then, if you write *first = 20;

, both arrays will be updated.

The problem with your question is that you initialize to 0 first and then to 1. If they were the same address, then parameter 1 will be overwritten with 0.

0


source


Are you in doubt about self-consistency:

  • 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]

- &secondArray[0] + 1

. Full stop. This is how arrays work in C ++. In this case, if it secondArray[0]

matches firstArray[0]

, then the 2 arrays have the same address and are actually the same array, and your example would look like this:

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 the first element of both arrays with the same value and the other three 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 pointing to the value I just changed)

      

This is what you asked for, but this is a very unusual requirement and I am not sure if this is really what you are after.

0


source







All Articles