5

I was experimenting with ways to initialize arrays and strings in C, and found that:

char *str = "ABCDE";

perfectly initializes the string with no errors or warnings, but:

int *array = {1,2,3,4,5};

gives me warnings and eventually dumps core. It really bugs me now and I would like to know why this sort of declaration works for characters but doesn't for integers...

EDIT: I'm using the gcccompiler.

7
  • Arrays are not pointers. That's why you can't initialize a pointer like an array. Commented Sep 22, 2012 at 18:57
  • 4
    Never assign string literals to char*. Commented Sep 22, 2012 at 19:44
  • @CatPlusPlus As general as stated, that is bad advice. It's perfectly fine and done in billions of programs. Commented Sep 22, 2012 at 20:32
  • 4
    @Jens: What? No, you cannot modify the memory behind a string literal. EVER. It's undefined behaviour (though it's pretty much guaranteed crash). const is there to catch the bug before it can happen. Commented Sep 22, 2012 at 23:50
  • 2
    @Jens: No, that's exactly what I'm saying. Never do that, because you are lying (string literals are not mutable, and you're using a mutable type). I know C programmers are bad at type safety, but you can avoid bugs in this case, so there is no reason to not do that. char *foo = "foo"; in any form would never ever pass a code review if I had anything to say. Commented Sep 23, 2012 at 12:38

2 Answers 2

7

It will work for ints by doing this:

int array[] = {1,2,3,4,5};

or this:

int *array = (int[]){1,2,3,4,5};

"string" tells the compiler all the information it needs (size,type) to instantiate the string (aka an array of bytes with a NULL terminator). A naked {} does not unless you declare it as a compound literal. Adding the ints[] tells the compiler that the initiated data is an array of ints.

As Nathan pointed out in the comments there are subtle differences to the two statements.

The first, defines an array of 5 ints on the stack. This array can be modified and lives until the end of the function.

The second, 1) defines an anonymous array of five ints on the stack 2) defines a pointer 'array' to the first element of the anonymous array on the stack. The pointer should not be returned since the memory is on the stack. Also the array is not inherently const like a string literal.

EDIT: Replaced cast with compound literal as pointed out by commentator.

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

10 Comments

Just would like to add these two statements are quit different, the first creates an array in stack, and copies the array into it, the second declares a pointer in the stack and sets is value to a literal array. Because of this the second one would really be const int *array = (int[])...
@James: The warning read initialization makes pointer from integer without a cast, so the second declaration you gave really shows why. Thank you for you answer.
@NathanDay: Could you point me to some resource where I could find out more about those things you mentioned, that happen "behind the scene"?
@CatPlusPlus perhaps instead of a snarky comment you could explain what you think is wrong.
@Cheersandhth.-Alf Compiles and runs as I explained with both gcc and clang. I welcome explanation and correction. I am here to learn just as much as everyone else.
|
4

The string literal decays to a const pointer to a char. Whereas this is an array {1,2,3,4,5} in C and does not decay. So you will have to use the syntax for creating arrays in C, like so:

int a[] = {1,2,3,4,5} ;

Then you can point to it:

int a[] = {1,2,3,4,5} ;
int *p = a;

Because the name of an array is the address of the array or the first element. Hope that helps.

Comments

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.