How to copy struct with memcpy() in C

1 Answer

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

struct {
  char name[32];
  int age;
} worker, worker2;

int main(void)
{
	char name[] = "Ashley";

	memcpy(worker.name, "Ashley", strlen(name) + 1);
	worker.age = 42;
	printf ("worker.name: %s worker.age: %d \n", worker.name, worker.age);
	
	memcpy(&worker2, &worker, sizeof(worker));
	printf ("worker2.name: %s worker2.age: %d \n", worker.name, worker.age);
	
	return 0;
}

 
/*
   
run:
   
worker.name: Ashley worker.age: 42
worker2.name: Ashley worker2.age: 42

*/

 



answered Jan 26, 2016 by avibootz

Related questions

...