-1

I want to copy all the element of small structure array to larger structure array without copying individual element from the structure my code is below

This question is asked here copy smaller array into larger array before but i couldn't find appropriate reply.please help me

  struct st 
 {
  int i;
  char ch[10];
 };

 int main()
  {
   struct st var[2]={1,"hello",2"bye"};
   struct st largevar[3];
   strcpy(largevar,var);// this is bad i guss but is there any way to copy without individual element access?
  }
1
  • 1
    memcpy? Commented Nov 5, 2014 at 16:44

2 Answers 2

1

You were not very far, but the correct function in memcpy : strcpy copies null terminated strings, memcpy copies arbitrary memory blocks :

You could use :

memcpy(largevar, var, sizeof(struc st) * 2);
Sign up to request clarification or add additional context in comments.

Comments

0

You should be using memcpy() as shown below and not strcpy().

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

    struct st 
     {
      int i;
      char ch[10];
     };

     int main()
      {
          int i =0;
       struct st var[2]={{1,"hello"},{2,"bye"}};
       struct st largevar[3];
       memcpy(largevar,var,sizeof(struct st) * 2);
       for(i=0;i<2;i++)
       printf("%d %s\n",largevar[i].i,largevar[i].ch);
       return 0;
      }

Comments

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.