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();
}

Recommended Answers

All 10 Replies

if you want to use strcpy() just do this: strcpy(p->name,input_name);

gets(input_name);

Never call gets() because it will allow you to enter more characters then the buffer can hold. All excess characters are just scribbled all over your program's memory. Instead, call fgets() like this: fgets(input_name, sizeof(input_name), stdin); <<< stdin is the handle to keyhboard input. You can put any open file handle there but if you want input from keyboard then use stdin.

@general2012: why did you start 2 threads for this?

I have replied in similar lines as AncientDragon in the other thread. Check that out for the printf problem as well...

Along with not using gets, you should also not cast the output of malloc() and you should make a habit of including stdlib.h for malloc so that you will atleast get some friendly warnings.

ya that worked.Now my question is when im permitted to do this.is this a valid c command ??????

p->name[(p->count)++];

@NP-complete this is the post that i posted first.after posting this i faced problem in "Tag articles with keywords to increase their visibility" option.so wanted to delete it.I didn't find any "delete button" to deleate this one.so i reposted it.
And yes,I checked your reply in the other post.that was helpful.thnx

Now my question is when im permitted to do this.is this a valid c command ???

Yes it is a nearly valid statement. You do that when you want to assign a single character to a specific location within the string
p->name[(p->count)++] = 'A';

or this
p->name[(p->count)++] = input_name[0];

what is p->count supposed to represent? The number of characters in p->name? If yes, then you don't need it, to get the length of name just call strlen().

that question rises when tried to do this. i considered a situation where a students name has two parts ,i input two parts in two different arrays and i copy first part to p->name[(p->count)++] using strcpy and than add the 2nd part using strcat here i used "count"so that it can work with strcat from the following bytes after executing strcpy
my code was something like this.but this didn't work

#include<stdio.h>
#include<stdlib.h>
typedef struct{
        int count;
        char name[20];
        }student_data;

int main()
{

        student_data *p;
        char name_part1[10];
        char name_part2[10];
        p=(student_data *)malloc(sizeof(student_data));
        fgets(name_part1,10,stdin);
        fgets(name_part2,10,stdin);
        strcpy(p->name,name_part1);
        strcat(p->name,name_part2);
        printf("%s",p->name);
        getch();
        }

you probably need to put a space between the two parts so that they don't both run together. An easy way to do that is like this
sprintf(p->name,"%s %s", name_part1,name_part2);

You just need to make sure that p->name is long enough to hold both names plus a space.

in my code if i input two string as-
1.james
2.carter

 printf("%s",p->name);

gives me output
james
carter
in seperate lines.
as both the names are copied in the same array p->name it should be printed in the same line.can u please explain why they are not in the same line?
replacing it with

sprintf(p->name,"%s %s", name_part1,name_part2);

prints nothing in the screen.i want a output as james carter .how can i print them in one line?

The problem is fgets(). That function adds '\n' to the end of the string. So after calling fgets() you need to remove it.

char* ptr;
fgets(name_part1,10,stdin);
if( (ptr = strrchr(name_part1,'\n')) != NULL)
   *ptr = 0;

thnx.i changed my code.and now it works fine

#include<stdio.h>
#include<stdlib.h>
typedef struct{
        int count;
        char name[29];
        }student_data;

int main()
{
    char *m;

        student_data *p;
        char name_part1[10];
        char name_part2[10];
        p=(student_data *)malloc(sizeof(student_data));
        fgets(name_part1,10,stdin);
        if( (m = strrchr(name_part1,'\n')) != NULL)
         *m = ' ';
        fgets(name_part2,10,stdin);
        strcpy(p->name,name_part1);
        strcat(p->name,name_part2);
        printf("%s ",p->name );
        getch();
        }
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.