facing problem to copy string to a structure via function.can input the name but just cant copy it.can anyone help

char copy(char *);//declare prototpe;
typedef struct{
      char student_name[20];
      int count;

student_data *p;// declare structure variable;
student_data s;//declare structure variable;
int main()
    p=&s;//give p the address of s;
    p->count=0; //give p->count value 0;
    char name[20];

char copy(char *q)
        strcpy( p->student_name[(p->count)++],q); 

Edited by general2012: missing characters

4 Years
Discussion Span
Last Post by general2012

I'd suggest you use a safer method to read input - gets is an unsafe operation. fgets can be used instead:

char name[20] = {0};
fgets (name, 20, stdin);

Also, strcpy expects the destination to be a pointer to a char array. You are providing a char (by indexing the char array with []). That should probably look somthing like:

strncpy (p->student_name, q, 20);

Again, notice that I used a different method than you: strncpy. strcpy disregards the destination size while strncpy will only copy (at most) as many bytes as you specify.


thnx,but somehow can it be done by incrementing array index ??like this

strncpy( p->student_name[(p->count)++],q,20); 

You are failing to provide what the function is asking for. Your compiler should be warning you about this - loudly. You have two choices

  • call the function correctly considering your code: strncpy (p->student_name, q, 20)
  • Change your code to match how you are expecting to call the function

The second item in that list requires some level of memory management or bounds checking. It depends on how you want to implement it.

What is the ultimate goal of your program?


i want [(p->count)++] will reserve number of characters copied so that i can copy multiple strings in a single array one after another.i am writting small programmes to see the results so that i can further use it in bigger programmes like implementing a queue.

strncpy( p->student_name[(p->count)++],q,20);

but cant figure out how to make above code work.help needed.

Edited by general2012: missing characters

strncpy( p->student_name[(p->count)++],q,20);

above code isnt working with char student_name[20]; but if i convert char student_name[20]; to
char student_name[2][20]; (a 2D array) then it works. why??


Because at that point p->student_name[0] results in a pointer to an array of char. Here is what happens when you use the [] operator to index an array:

  • char a[20] = {0}; -> Declare a to be an array of char
  • a[0] -> Index into the array (equivalent to *(a + 0))
  • *(a + 0) -> can be reduced to *a
  • *a -> dereference the pointer a. The result is the type pointed to by a
  • The type pointed to by a is char (because you declared an array of char)

When you change the above to start with char a[2][20] you get somthing along the lines of the above but when you arrive at the last bullet the type being pointed to is now an array of 20 char instead of just a char.

Because of this, the call to strncpy gets the argument it is looking for and everything works as you want.

I would like to reiterate another point. The compiler should be warning you very loudly about your first version of the code. You should always try to heed those warnings as they can help you solve these types of bugs.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.