0

I have problem initializing array in C++14.

int *arr={1,2,3,4};     // Works in C but does not work in C++

I can't understand the error message it shows

prog.cpp: In function 'int main()':
prog.cpp:6:26: error: scalar object 'arr' requires one element in initializer
     int *arr = {1, 22, 5, 20};   //NOT WORKING IN C++

What is the exact meaning of initializer in the error message?

Link of code of C++ which is not working http://ideone.com/684V5t

Link of Same code in C which works http://ideone.com/RAL3te

If anyone can explain why this is happening it would be nice. Thank you.

sian
  • 153
  • 1
  • 10
  • 8
    "_Works in C_" -- With loads of warnings telling you that it is wrong. – Spikatrix Aug 26 '15 at 10:00
  • 1
    int arr[]={1,2,3,4}; – Ajay Aug 26 '15 at 10:03
  • 2
    possible duplicate of [How to initialize an array in C](http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c) – Ajay Aug 26 '15 at 10:04
  • 1
    gcc gives me this feedback to your C-Code: `warning: initialization makes pointer from integer without a cast [enabled by default] int *arr={1,2,3,4};`. And this right. You can't put a integer-array initialisation on a int pointer. – Alex44 Aug 26 '15 at 10:09
  • 2
    `int *arr = (int[]){1, 2, 3, 4}` will work in C, but not in C++. Without proper tagging, I don't know if that's an answer. – Quentin Aug 26 '15 at 10:12
  • 1
    It doesn't exactly *work* - you're only printing a compile-time value, and [this code](http://ideone.com/daTt1w) "works" in exactly the same way. Did you [look at the pointer's value](http://ideone.com/5rQaqh) or [attempt to use it](http://ideone.com/0FRoaI)? (And you're not initialising an array, your'e initialising a pointer.) – molbdnilo Aug 26 '15 at 10:22
  • What are you trying to achieve exactly ? – Jabberwocky Aug 26 '15 at 10:26
  • I was trying to find the number of elements stored in an array when the array is declared using pointers, while I encountered this error in initialising the array. The same code produced different output in C and C++. – sian Aug 26 '15 at 10:57

1 Answers1

4

This doesn't work in C++ because arr is not a variable of type class and therefore there is no size member:

#include <iostream>
using namespace std;

int main() {
    // your code goes here
    int arr[] = {1, 22, 5, 20};         //NOT WORKING IN C++ < yes it does
    cout << arr.size();          // << this does not "work" in C++
    return 0;
}

BTW the error message:

prog.cpp:7:12: error: request for member 'size' in 'arr', which is
               of non-class type 'int [4]' cout<<arr.size();

is quite explicit.

And this should not compile (or you should get at least warnings), neither in C neither in C++:

int *arr={1,2,3,4};

You want this (C and C++):

int arr[] = {1, 22, 5, 20};
printf("%d", sizeof(arr)/sizeof(*arr));

Output will be 4.

In C this may compile depending on the compiler, but you should get warnings:

#include <stdio.h>

int main() {
    // your code goes here
    int *arr={1,2,3,4};
    printf("%d\n", sizeof(arr));
    printf("%d\n", sizeof(*arr));
    printf("%p\n", arr);        
    return 0;
}

The output is :

4
4
0x1

because sizeof(arr) is 4 and sizeof(*arr) which is the same as sizeof(int) because arris an int pointer (assuming a 32 bit system).

The arrpointer will contain 1 because of nthe 1st 1 in the initializer list.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Sorry, I accidently changed the code of ideone link. Here is the actual problem - If you can explain the error message and why am I getting it. – sian Aug 26 '15 at 10:47
  • @SHWETANK, because it's just plain wrong. In that example `arr` is a pointer and it's nonsense to initialoze it with more than _one_ value. See also the comments of your questions. – Jabberwocky Aug 26 '15 at 11:25