copy string into a structure

all i am trying to do here is to copy a string input given by user into an arry decleared inside a structure.but my code is not working. anyone please explain to me why this code isnt working

    #include<stdio.h>
    #include<string.h>
    typedef struct{
    int count;
    char name[20];
    }student;
    int main()
    {
    student *p;
    p=(student *)malloc(sizeof(student));
    char input_name[20];
    gets(input_name);
    strcpy(p->name[(p->count)++],input_name);
    printf("%s",p->name[(p->count)++]);
    getch();
    }

Dani AI

Generated

Two common pitfalls here are unnecessary dynamic allocation and unsafe input. If you only need a single struct, prefer automatic storage and read directly into the member buffer. Also remember that char name[20] can hold at most 19 characters plus the terminating '\0'. gets was removed from the C standard in C11 because it cannot be used safely; use fgets instead and strip the trailing newline if present. getch() is non-standard (from conio); if you just want to pause, consider getchar() instead. See fgets, gets (removed), and getchar.

Example of a simple, safe pattern:

typedef struct {
    int count;
    char name[20];
} student;

int main(void) {
    student s = {0};                            /* count and name zero-initialized */
    if (fgets(s.name, sizeof s.name, stdin)) {
        s.name[strcspn(s.name, "\n")] = '\0';   /* remove trailing newline */
        puts(s.name);
    }
    return 0;
}

If you do allocate dynamically for multiple students, allocate an array (e.g., malloc(n * sizeof *p)), check for NULL, and keep a separate index you increment when adding a new record; do not use post-increment on the same expression you use to read or print. Finally, be cautious with strncpy: it does not guarantee a terminating null byte on truncation. If you must copy, snprintf(p->name, sizeof p->name, "%s", input) is often a safer alternative. See strcspn and strncpy for details.

Recommended Answers

All 2 Replies

Why its not working:
1. p->count is never initialized to anything.
But the main problem is this:
2. You are trying to copy a C-string to a character. p->name[any index] refers to a single character and not a C-string. So strcpy fails.
3. Again, you want to print a C-String (with that %s) but you are really trying to feed a character to that %s. Result: your code crashes.

The fix:

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

    typedef struct{
        int count;
        char name[20];
    }student;

    int main()
    {
        student *p;
        p = (student*) malloc(sizeof(student));
        char input_name[20];
        gets(input_name);
        strcpy(p->name, input_name);
        printf("%s", p->name);
        getch();
    }

Now, a few tips to improve:
1. Use fgets instead of gets.
2. Once you use fgets you may as well want to use strncpy to make the copy overflow safe.
3. You may want to include stdlib.h for malloc. In that case, the cast would be redundant (And this should be the case!)

thanx. now it is quite clear to me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.