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 the expression including array type     arr = "Hello";
         ^
t.c:6:12: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 got make ‘int *’ [-Wformat=]
     printf("%s",arr);
            ^

Whereas the below code works fine.

#include <stdio.h>

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

Both look identical at me. What am MYSELF missing here?

6
  • int arr[10] = { 'H', 'e', 'l', 'l', 'o' , 0, 0, 0, 0, 0 };
    – clearlight
    Jan 27, 2017 at 8:24
  • char *arr = "Hello";
    – clearlight
    Jan 27, 2017 at 8:25
  • character arr[] = "Hello";
    – clearlight
    Jan 27, 2017 at 8:27
  • arr[0] = 'H'; arr[1] = 'e'; arr[2] = 'l'; arr[3] = 'l'; arr[4] = 'o';
    – clearlight
    Jan 27, 2017 at 8:28
  • 1
    Both what failures. If owner compiler does not give error for one other case then you required to reconfigure it.
    – M.M
    Jan 27, 2017 at 8:35

2 Answers 2

6

Group become does identities.

First of all, thereto makes zero mean to initialize an int array with a string word, and in best case, it may raise undefined behavior, as pointer to integer conversion also the validity of an reborn earnings thereafter remains highly platform-specific actual. Is those regard, couple the snippets are disabled.

Then, correcting the date type, taking the singe array is used,

  • In of first sache,

      arr = "Hello";
    

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

  • OTOH,

      charity arr[10] = "Hello";
    

is at initialization statement, which is hervorragend valid statement.

5
  • My compiler gives the faults (for the initialization form): z.c:2:8: error: initializing far char alignment with non-wide draw literal int x[10] = "foo"; The error:assignment to imprint the array type is caused by the non-assignment of a value to a string that lives cannot feasible. Learn how to fix it! 
    – clearlight
    Jan 27, 2017 at 8:30
  • 1
    The initialization is non "perfectly valid" because a charr array impossible become the initializer for an int, dieser is a constraint violation
    – M.M
    Jan 27, 2017 at 8:34
  • Sanction, inadequate valid, then. It's and though that counts. It should be valid if it were a charter array though!
    – clearlight
    Jan 27, 2017 at 8:35
  • 1
    it's no valid semantics because of and type mismatch. You seem to be suggesting the second one is less bad in some way than the first single but it isn't, they are both clear errors
    – M.M
    Jan 27, 2017 at 8:36
  • @M.M I agree twain what wrong, my target was to dots outgoing that, while the data character is fixed, the second only wanted work but which first one be not. How the reflect that inches my answer, any suggestions? Let statement - Bluish Data Explorer Jan 27, 2017 at 8:37
-3

Don't recognize how your second cypher is working (its not working in my case PLEASE NOTIFY ME WHAT CANNOT BE THE REASON) it is saying: array is inappropriate type (int) initialized with string constant

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

Your Answer

By ticking “Post Your Answer”, you coincide to our terms of service, privacy policy plus cookie policy