3
char array[12];
sprintf(array, "%s %s", "Hello", "World");
printf(array); // prints "Hello World"

Is there a way to do it without using sprintf or strcpy?

4
  • 2
    array[0]='H'; array[1]='e'; /*...*/ array[11]=0; Commented May 10, 2020 at 20:36
  • What's wrong with strcpy? Commented May 10, 2020 at 20:57
  • A loop is more or less what sprintf does under the hood. Commented May 10, 2020 at 22:33
  • 3
    printf(array); is bad practice, btw. What if array has a percent sign in it? Use fputs(array, stdout); or printf("%s", array); instead to print a string to standard output. Commented May 10, 2020 at 23:17

5 Answers 5

5

You cannot assign a string to an array directly, but you can initialize an array from a string literal:

char array[] = "Hello World";  // this defines array with a size of 12 bytes

If you later want to store a different string to array, you must use a string copy function such as these:

strcpy(array, "Hello Buddy");      // assuming array has at least 12 bytes
memcpy(array, "Hello Buddy", 12);  // assuming array has at least 12 bytes
snprintf(array, sizeof array, "%s %s", "Hello", "Buddy");

or you can assign characters one at a time:

array[6] = 'B';
array[7] = 'u';
array[8] = 'd';
array[9] = 'd';
array[10] = 'y';
array[11] = '\0';

Note that it is preferred to use snprintf instead of sprintf to avoid potential buffer overruns. Also avoid passing a variable array to printf, which would cause undefined behavior if it contains % characters. Always use a constant format string:

printf("%s\n", array);
Sign up to request clarification or add additional context in comments.

2 Comments

How about a loop?
So, basically, it's not possible in a handy way (without strcpy and snprintf) , since assigning EACH char value is not an handy way. Thanks for reply!
4

You can do

char array[] = "Hello World";

1 Comment

That's not an assignment, it's an initialization
1

Given two strings and a buffer big enough to hold both, you can use a loop to copy the content:

const char *s1 = "Hello";
const char *s2 = "World";
char s[12];

int i;

for(i = 0; i < 5; i++) {
    s[i] = s1[i];
}
s[i++] = ' ';
for(i = 0; i < 5; i++) {
    s[i + 6] = s2[i];
}
s[i + 6] = '\0';

If it's a case like command line arguments, where you have an array of pointers with a count, you can use a pair of nested loops for the whole thing:

char *argv[] = {
    "Hello",
    "World",
    NULL
};
int argc = 2;

char s[12];

int i, j, k;

j = 0;
for(k = 0; k < argc; k++) {
    for(i = 0; argv[k][i]; i++) {
        s[j++] = argv[k][i];
    }
    s[j++] = ' ';
}
if(k) {
    s[--j] = '\0';
} else {
    s[0] = '\0';
}

1 Comment

@chqrlie. Thanks for those catches. I believe I've fixed them. I appreciate the debugging at a time when I couldn't.
0

a way to assign a string to an array:

#define MAX_LEN 12

int main( void )
{
    ...
    char array[ MAX_LEN ];
    ...
    memset( array, '\0', MAX_LEN );
    memcpy( array, "hello world", strlen( "hello world" ) );

5 Comments

This is just a convoluted and totally unsafe way do avoid strcpy, so it's pretty pointless. And the memset could be avoided, providing you make a slight changement in the line with memcpy.
@Jabberwocky, The OP ask how to do it without using strcpy() That is exactly what I provided, So this answer exactly answers the OPs question. So it is not pointless. I never indicated it was a 'safe' method
We should not give poor advice here.
@Jabberwocky, I could have declared the array via: char array[ MAX_LEN ] = {\0'}; But decided not to do so for flexibility/reusability.
What I mean to say is not using strcpy is a poor choice. His question is like, I want to add two numbers but I don't want to use the + operator. I can be done, but why?
-1

Eventhough I could not understand what you really meant, from your title, I understand that you ask how to assign a value to a string. Thus I will write the code of it. I hope it helps.

char array[] = "Hello World!";

or you can assign more than one values like:

char array[12];
array[0]="H";
array[1]="e";

and can go till the last point. Yet they will be static, you will not be able to copy them by using strcpy. If you want, you have to use malloc which is memory allocation for the array of strings.

1 Comment

You are assigning pointers to strings to elements 0 and 1 of array. Well, sort of, because they most probably won't fit in them.

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.