-1

i have just started learning C and i am confused on how to write a string into a simple char array in C. i understand that C does not have a string data type, and i read that most websites declare string in one of this way

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char greeting[] = "Hello";

however what if i have initialized a char array of

char greeting[50];

and i try to give it a string value.

char greeting[50];
greeting = "Zack";

i am given a error, why?

1
  • In C, arrays are a const reference. You can't change them. Commented Nov 3, 2014 at 13:07

5 Answers 5

2

strcpy is designed to copy "strings". Use as follows:

char greeting[50];
strcpy(greeting, "Zack");

Be aware of potential overruns if the destination isn't big enough.

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

Comments

2

Use standard function strcpy declared in header <string.h> that to copy a string in a character array

#include <string.h>

//...

char greeting[50];
strcpy( greeting, "Zack" );

There are other functions declared in header <string.h> that also can be used to copy strings in character arrays. For example

memcpy
strncpy
strcat
strncat

An example of using strcat instead of strcpy

#include <string.h>

//...

char greeting[50];

greeting[0] = '\0';
strcat( greeting, "Zack" );

Arrays have no the assignment operator. So the compiler issues an error for this code snippet

char greeting[50];
greeting = "Zack";

But there is a trick when you may assign character arrays. To do that you need to enclose a character array in a structure and use a compound literal. For example. Contrary to arrays structures have the assignment operator that performs member-wise copy of structure.

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

int main(void) 
{
    struct A { char greeting[50]; } s;
    s = ( struct A) { "Zack" };

    puts( s.greeting );

    return 0;
}

The output will be

Zack

That this code would be compiled you need a compiler that supports C99.

Comments

1

You use strcpy():

char greeting[50];

strcpy(greeting, "Zack");

This will copy the four characters and the terminating zero into greeting.

Beware that it knows nothing about the available space at greeting, so it's perfectly possible to do the wrong thing.

Comments

1

You're getting an error because you're basically trying to assign a string constant to an array. The array greeting decays to a pointer, but it's a char *const pointer.

You need to use strcpy to copy the literal.

Comments

1

In C, the folowing code will print "foo":

char greeting[50];
if(greeting == &greeting[0]){
    printf("foo");
}
else{
    printf("bar");
}

This happens because the variable of the array (greeting) contains a pointer to the first element of it (greeting[0]).

So, if you asign:

greeting = "Zack";

You are saying: Let greeting have the address of where the literal string "Zack" is in the memory.

As changing the address of a array is not allowed, you get an error.

4 Comments

And that's why you must follow @doctorlove answer to make this assignment
@user3204469 It is a wrong statement that arrays contain pointers to their first elements.
@Baldrickk, I understood that the question was: "why is this statement wrong?" not "how to copy a string to an array?". Sorry by my misunderstanding.
@user3204469 it seems you edited the answer within the grace period and it hadn't updated when I commented. Looks fine now, though it should say "the array variable (greeting) degrades to a pointer to the first element" because as Vlad says, the array doesn't contain a pointer to itself...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.