0

Is there any manual way to initialize the string in struct ? I used to initialize string in struct using strcpy function such as:

typedef struct {
    int id;
    char name[20];
    int age;
} employee;


int main()
{
    employee x;
    x.age=25;

    strcpy(x.name,"sam");
    printf("employee age is %d \n",x.age);
    printf("employee name is %s",x.name);

    return 0;
}
5
  • 2
    employee x = { .age = 25, .name = "sam"}; Commented Aug 19, 2017 at 23:30
  • Why? Since your question seems absurd, explain why. Commented Aug 19, 2017 at 23:46
  • to copy into a fixed-size array, you should always use strncpy(), or strncpy_s, if available. Commented Aug 20, 2017 at 0:12
  • 1
    @MichaëlRoy Disagree with "to copy into a fixed-size array, you should always use strncpy(), ..." Commented Aug 20, 2017 at 3:29
  • You can. But don't come posting questions here about buffer overflow. Commented Aug 20, 2017 at 3:31

3 Answers 3

7

Strictly speaking this

strcpy(x.name,"sam");

is not an initialization.

if to speak about the initialization then you can do it the following way

employee x = { .name = "sam", .age = 25 };

or

employee x = { .name = { "sam" }, .age = 25 };

This is equivalent to the following initialization

employee x = { 0, "sam", 25 };

or

employee x = { 0, { "sam" }, 25 };

Or you even can use a compound literal of the type employee to initialize the object x though that is not efficient.

Otherwise if is not an initialization but an assignment of the data member of the structure then indeed you have to use at least strcpy or strncpy.

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

2 Comments

Good answer aside from "compound literal ... though that is not efficient.". Certainly a good compiler will optimize that as well as other approaches.
Assignment can use a compound literal without strcpy() and friends.
-1

max - including trailing zero

char *mystrncpy(char *dest, const char *src, size_t max)
{
    char *tmp = dest;

    if (max)
    {
        while (--max && *src)
        {
            *dest++ = *src++;
        }

    *dest++ = '\0';
    }
    return tmp;
}

1 Comment

*dest++ = '\0'; belongs inside if(max)
-1

You can write your own version of strcpy:

void mycopy(char *dest, const char *source, size_t ndest)
{
    assert(ndest != 0);
    while (--ndest > 0 && (*dest++ = *source++))
        ;
}

You're not using strcpy anymore. Plus it is safer.

10 Comments

You should have clearly marked this as "sarcastic". I assume it is sarcastic. If not, then your employer should be frightened.
I have not seen so badly written simple function for years
going to bed enough for today
There need to be parentheses around the assignment since && has higher precedence than =.
Corner case weakness: mycopy(dest, source, 0) leads to buffer overrun. strncpy() does not have that problem.
|

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.