-2

I have been doing well with the string functions I've been making recently. So then, I made my 3rd string function that is basically like strcpy():

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

const char *copy_str(const char *str);

int main() {
    char name[256];
    printf("What's your name? "); scanf("%255s", name);
    char result[256] = copy_str(name);
    printf("Hello, %s.\n", result);
}

const char *copy_str(const char *str) {
    return str;
}

I didn't try using the strcpy() function itself in the function I made, because I'm not really familiar with it. Then I got an error:

copy_str.c:9:10: error: array initializer must be an initializer list or string literal
    char result[256] = copy_str(name);
         ^

I fixed it this way:

int main() {
    char name[256];
    printf("What's your name? "); scanf("%255s", name);
    char result[256];
    strcpy(name, result);
    printf("Hello, %s.\n", result);
}

But then the output went like this:

What's your name? builderman
Hello, .

No string for some reason. Even if you typed in a different string, it would be the same result.

Q: Why did strcpy() ruin my string? What ways can I improve my function?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
BMPL
  • 35
  • 16
  • 5
    [man strcpy](https://man7.org/linux/man-pages/man3/strcpy.3.html): `char *strcpy(char *dest, const char *src);` Note the order of the args. – kaylum Sep 27 '20 at 09:45
  • 1
    `char result[256] = copy_str(name);` You can't assign a pointer to an array. You want to say this, `const char* result = copy_str(name);` – selbie Sep 27 '20 at 10:04

2 Answers2

2

The function strcpy has the following declaration

char *strcpy(char * restrict s1, const char * restrict s2);

That is the first parameter defines the array where the string pointed to by the second parameter is copied.

However in your program

int main() {
    char name[256];
    printf("What's your name? "); scanf("%255s", name);
    char result[256];
    strcpy(name, result);
    printf("Hello, %s.\n", result);
}

you are copying the non-initialized character array result into the array name.

You have to write

    strcpy( result, name );

instead of

    strcpy(name, result);

As for the first program then the function copy_str is redundant. You could just write

char result[256] = name;

However the initializer has the type char * due to the implicit conversion of an array to pointer to its first element. So such initialization of an array is incorrect. You may initialize a character array with a string literal like

char s[] = "Hello";

But you may not use a character pointer to initialize a character array like

char t[] = "Hello";
char s[] = t;

Your function copy_str could look the following way

char * copy_str( char *dsn, const char *src )
{
    for ( char *p = dsn; ( *p++ = *src++ ); );

    return dsn;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

I will try to answer your questions in parts.

  1. copy_str.c:9:10: error: array initializer must be an initializer list or string literal

    char result[256] = copy_str(name);

the meaning of this error is that in C syntax you cannot initialize an array the way that you did. you can only initialize an array of chars with either a literal like so:

char name[] = "Hello";

or an initialization list like so:

char name[] = {'H', 'e', 'l', 'l', 'o'};

you can't initialize an array with a pointer to a char (which is the return value of your function)

  1. strcpy doesn't magically copy an entire string to another one, it iterates over the string until it finds a NULL terminator '\0' so a simple implementation of strcpy can be:

    char *StrCpy(char *dest, const char *src)
    {
     char *returned_str = dest;
    
     while (*src != '\0')
     {
        *dest = *src;
        src++;
        dest++;
     }
     *dest = '\0';
    
     return returned_str;
    }
    

so strcpy iterates over the strings and copies each character from src to dest. a problem may arise when sending an array of characters that doesn't have a null terminator at the end of the array, which is how a string should be terminated in C.

Krutyi 4el
  • 98
  • 6
Gilad
  • 305
  • 1
  • 2
  • 8