-1

I am having difficulty understanding how i can fix this this error "array1[i][j] = expression must be a pointer-to-object type. I have searched the error but i am unable to apply the solutions to my code snippet.

int main(){

    int array1[]= {1234,4321}; //{1234,4321};
    int array2[]= {2345,3214}; //{2345,3214};
    int counter = 0;
    int arr_element = sizeof(array1);
    int arr_index =  sizeof(array1)/sizeof(*array1); 

    for(int i = 0, count1 = arr_index; i < count1; i++ ){
        for(int j = 0, count2 = 4; j < count2; j++){
            cout << array1[i][j] << endl;

        }
    }
    return 0;
}

What i would like to do is be able to print out the elements in array1; for example, i would like this output: 1,2,3,4,4,3,2,1. From my understanding, the int a needs to be a pointer. I i added * in front of the array (*array1) and in front of the int (int**), but with no luck. Thank you for your time.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69
VickTree
  • 889
  • 11
  • 26
  • 2
    `1234` is an `int`, not an array. So why do you believe that you can access an `int` like an array? Would you really expect `int x = 3456; int y = x[2];` to work and return 3? – Werner Henze Mar 06 '19 at 16:59
  • There are numerous posts that explain how to get individual digits in an integer, see e.g., https://stackoverflow.com/a/3118505/8033585, https://stackoverflow.com/q/4615046/8033585, or https://stackoverflow.com/q/4207696/8033585, to name a few. – AGN Gazer Mar 06 '19 at 17:08

1 Answers1

2

Ypu are using one dimensional array, What you are wishing for is a 2dimensional array and that's how you declare it

 int array1[][]= {{1,2,3,4},{4,3,2,1}};
 int array2[][]= {{2,3,4,5},{3,2,1,4}}; 
Spinkoo
  • 2,080
  • 1
  • 7
  • 23