-1

I want to store a string "hello" in an array of characters char arr[10]

when I run the following code:

#include <stdio.h>

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

}

The output is generated fine. But when this code below is run, I get an error

#include <stdio.h>

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

Why is that in the first case the string can be stored in the array and not in the second case? Is there a way I can store "hello" in the second case?

6
  • 3
    Use strcpy in the second case. Commented Dec 22, 2019 at 3:29
  • Don't forget to return 0; in the end of main. Commented Dec 22, 2019 at 3:39
  • Note: arr is an array of char, str is a pointer to char. someFunction(arr) is similar to someFunction(&arr[0]). [This implies array decays into a pointer] and hence, use strcpy to copy contents from one to another. char* strcpy(char* dest, const char* src); Commented Dec 22, 2019 at 3:46
  • 1
    @SaucyGoat: It’s implied for main. Commented Dec 22, 2019 at 3:47
  • 1
    See 6.3.2.1 Lvalues, arrays, and function designators An array is not a modifiable lvalue and cannot appear on the left side of an = sign. Commented Dec 22, 2019 at 6:55

5 Answers 5

1

You should use strncpy() or even better strlcpy() if you are on a BSD system or macOS and portability is not the main concern.

In C, char *p means a pointer to a character string. While writing the following

char arr[10];
char * str = "Hello";
arr = str;

you might have thought that the character string stored in the memory location pointed by str would be copied over to the buffer arr, but C does not do this for you.

The code below does what you want

#include <stdio.h>
#include <string.h>

#define BUFSIZ 10

int main(){
    char arr[BUFSIZ];
    const char *str=“Hello”; /* I used const here */

    strncpy(arr, str, BUFSIZ);

    printf(“%s\n”, arr);

    return 0;
}

Use strncpy or strlcpy instead of strcpy. Documentation for strncpy is here.

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

Comments

0

The strcpy is the way to do it, as noted above. You get an error for

arr = str;

in your given code because you are attempting to assign an array, and C does not allow arrays to be assigned. In your first part, the declaration you give

char arr[10] = "Hello";

is legal because it is considered an initialization (since it's in a declaration), not an assignment. That is allowed.

C is not always a bundle of consistency.

Comments

0

To copy a string in C, use strcpy:

char arr[10];
char * str = "Hello";
strcpy(arr, str);
printf("%s\n", arr);

Don't forget to #include <string.h>, which is required for you to have access to this function.

1 Comment

strcpy()...really? why not strnpy() or strlcpy()?
0

char arr[10] means that arr can be changed; char * str = "Hello" equals to const char* str = "Hello", so it can not be changed. so arr = str will occur error. You should check the memory allocation mechanism in C.

Comments

0

This is initialization of char array; there are special rules

char x[] = "foobar";

This is assignment to a pointer

char *p;
p = "foobar"; // char *p = "foobar"; initialization of pointer

There is no direct way to do assignment of a value to an array of char. You need to assign each individual element

char x[7];
x[0] = 'f'; x[1] = 'o'; x[2] = 'o';
x[3] = 'b'; x[4] = 'a'; x[5] = 'r'; x[6] = 0;

or use a function to do that for you

strcpy(x, "foobar");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.