1

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.

8
  • 8
    "Works in C" -- With loads of warnings telling you that it is wrong. Commented Aug 26, 2015 at 10:00
  • 1
    int arr[]={1,2,3,4}; Commented Aug 26, 2015 at 10:03
  • 2
    possible duplicate of How to initialize an array in C Commented Aug 26, 2015 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. Commented Aug 26, 2015 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. Commented Aug 26, 2015 at 10:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I accidently changed the code of ideone link. Here is the actual problem - <ideone.com/684V5t> If you can explain the error message and why am I getting it.
@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.

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.