-5

Why in C++,variable declared as an array can be added or subtracted with whole number but cannot be incremented(++) or decremented(--) or multiply by number etc., even though the variable stores the starting address of array? Eg

#include <iostream>
using namespace std;
int main()
{
     int x[] = {12,33,41,55,68};
     cout<<x<<"\n";          // Output : 0x7af786c84320
     cout<<x+1<<"\n";        // Output : 0x7af786c84324
     cout<<x+1000<<"\n";     // Output : 0x7af786c852c0
     cout<<x-1000<<"\n";     // Output : 0x7af786c83380
     cout<<x*1<<"\n";        /* Output : invalid operands of types  
                           'int[5]' and 'int' to binary 'operator*' */
     cout<<x*2<<"\n"; 
     cout<<x++<<"\n";        /* Error :  lvalue required as increment 
                           operand */
     cout<<x--<<"\n";
     x=x;                    //Error : invalid array assignment
     cout<<x<<"\n";
     return 0;
 }

It will be better if anyone can explain what happens when an array is declared in detail. And why among all arithmetic operation only '+' and '-' are valid not '*' or other.

  • 2
    Firstly, `int x ={2,3,4,5,6};` doens't compile. – HolyBlackCat Aug 26 '18 at 12:55
  • `int main{ int` looks like a syntax error. – melpomene Aug 26 '18 at 12:56
  • int main without () does not compile either. – Öö Tiib Aug 26 '18 at 12:56
  • 1
    Setting aside the typos in the shown code, an array cannot be incremented because that's how C++ works. "Increment an array" is logically meaningless, in C++. What does that mean "increment an array". The statement makes no sense. On the other hand, if you have a pointer to an array, you can certainly increment that pointer. But a pointer itself is not an array. – Sam Varshavchik Aug 26 '18 at 12:57
  • Possible duplicate of [What happens if I increment an array variable?](https://stackoverflow.com/questions/7334627/what-happens-if-i-increment-an-array-variable) – Shubham Aug 26 '18 at 12:58
  • sorry for typos but i posted that question from mobile thats why some error was there . @SamVarshavchik "increment an array variable" you should read first before commenting, you are like reading "increment" and saying what to increment it does not make sense. – Manish Kumar Singh Aug 26 '18 at 13:36
  • I'm not the one who's telling you this doesn't make sense. It's your compiler who told you that, in the first place. That's what a compilation error means, after all. A compilation error is your compiler telling you "your code doesn't make sense". There's nothing wrong with writing code that doesn't make sense. Everyone does that more often than they do writing code that does make sense. – Sam Varshavchik Aug 26 '18 at 13:54
  • That's what i am asking , i know that it will produce error that's why in code comment i have written invalid but i want to know why ? How array is defined internally and why such operations are not possible, If you know then it will be great help if you can explain the same. – Manish Kumar Singh Aug 26 '18 at 14:18
  • Because an array is not a pointer. Why do you assume two things with different names have to behave identical? – too honest for this site Aug 26 '18 at 14:31
  • If you don't know what is array and how to use it then read answers to https://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c – Öö Tiib Aug 26 '18 at 14:31
  • How would you define addition, `+`, or multiplication, `*`, with an array? Would you add only the first element? Multiply each element by a scalar? Add two arrays? Multiply 2 arrays? What happens when two arrays are not the same length? – Thomas Matthews Aug 26 '18 at 17:56

3 Answers3

2

A few things to note:

  1. Replace int main with int main() (to properly declare a function)
  2. Replace int x={2,3,4,5,6} with int x[]={2,3,4,5,6}; (to properly declare an array)
  3. Arrays cannot be incremented, but POINTERS to arrays can be. (see below)
  4. I'm assuming you want to print the VALUE of the first element of the array, not the ADDRESS of the first element.
  5. Ensure to terminate all lines with a semicolon.
  6. Technically, the main function should return 0 upon successful completion.

With all of that said, the following code should accomplish that.

int main() {
    int *y, x[] = {2,3,4,5,6};
    y = x;
    cout << *y++;
    return 0;
}

Now, examining this line by line

  1. Declare a pointer to an int (y), declare an int array (x), and initialize that array.
  2. Set y equal to x (i.e. store the address of the array in y).
  3. Dereference y (i.e. get the value of the integer at that address), print it, and then increment y to point at the next element in x.
1

No, the variable does not store the address of anything; it stores the whole array.

Incrementing a pointer makes sense, but an array is not a pointer.

When you say

int x[] = {2,3,4,5,6};

what you get in memory is

[ 2 ][ 3 ][ 4 ][ 5 ][ 6 ]

(5 integers in a row). x refers to the whole thing; there is no indirection through a pointer or anything.

melpomene
  • 84,125
  • 8
  • 85
  • 148
1

Most arrays contain something that can not be incremented or multiplied and so incrementing or multiplying arrays does not make any sense on general case.

There is (very rarely used) std::valarray template that assumes that its elements are arithmetic types and supports arithmetic operations to whole array:

#include <iostream>
#include <valarray>

int main()
{
    std::valarray<int> x = {2,3,4,5,6};

    x += 1;
    for (auto v : x) std::cout << v << " ";
    std::cout << "\n";

    x *= 2; 
    for (auto v : x) std::cout << v << " ";
    std::cout << "\n";
}

Output is:

3 4 5 6 7 
6 8 10 12 14 
Öö Tiib
  • 10,809
  • 25
  • 44