1
#include <stdio.h>

int main(void) {
    int arr[10];
    arr = "Hello";
    printf("%s",arr);
    return 0;
}

The above code shows compiler error:

t.c: In function ‘main’:
t.c:5:9: error: assignment to expression with array type
     arr = "Hello";
         ^
t.c:6:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat=]
     printf("%s",arr);
            ^

Whereas the below code works fine.

#include <stdio.h>

int main(void) {
    char arr[10] = "Hello";
    printf("%s",arr);
    return 0;
}

Both look identical to me. What am I missing here?

6
  • int arr[10] = { 'H', 'e', 'l', 'l', 'o' , 0, 0, 0, 0, 0 }; Commented Jan 27, 2017 at 8:24
  • char *arr = "Hello"; Commented Jan 27, 2017 at 8:25
  • char arr[] = "Hello"; Commented Jan 27, 2017 at 8:27
  • arr[0] = 'H'; arr[1] = 'e'; arr[2] = 'l'; arr[3] = 'l'; arr[4] = 'o'; Commented Jan 27, 2017 at 8:28
  • 1
    Both are errors. If your compiler does not give error for the second case then you need to reconfigure it. Commented Jan 27, 2017 at 8:35

2 Answers 2

6

They are not identical.

First of all, it makes zero sense to initialize an int array with a string literal, and in worst case, it may invoke undefined behavior, as pointer to integer conversion and the validity of the converted result thereafter is highly platform-specific behaviour. In this regard, both the snippets are invalid.

Then, correcting the data type, considering the char array is used,

  • In the first case,

      arr = "Hello";
    

is an assignment, which is not allowed with an array type as LHS of assignment.

  • OTOH,

      char arr[10] = "Hello";
    

is an initialization statement, which is perfectly valid statement.

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

5 Comments

My compiler gives the error (for the initialization form): z.c:2:8: error: initializing wide char array with non-wide string literal int x[10] = "foo";
The initialization is not "perfectly valid" because a char array cannot be the initializer for an int, this is a constraint violation
Ok, imperfectly valid, then. It's the thought that counts. It would be valid if it were a char array though!
it's not valid semantics because of the type mismatch. You seem to be suggesting the second one is less bad in some way than the first one but it isn't, they are both clear errors
@M.M I agree both are wrong, my target was to point out that, if the data type is fixed, the second one would work but the first one would not. How to reflect that in my answer, any suggestions?
-3

Don't know how your second code is working (its not working in my case PLEASE TELL ME WHAT CAN BE THE REASON) it is saying: array of inappropriate type (int) initialized with string constant

Since you can't just assign a whole string to a integer variable. but you can assign a single character to a int variable like: int a[5]={'a','b','c','d','d'}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.