spatara
spatara

Reputation: 901

memcpy-ing to a feild of a struct pointer in c

I am having trouble with struct pointers....Here are two examples in my code that are essentially doing the same thing except dsp is not a pointer and InMemory[Idx] is a pointer, how to I use memcpy in the pointer case?

my_struct* InMemory[SIZE]

//works prints: tmp3:local_file (file name)

memcpy(dsp.result.list[i].owner_name,req.file_name,256);
char tmp3[256];
memcpy(tmp3,dsp.result.list[i].owner_name,256);
printf("tmp3:%s\n",tmp3);

//doesn't work, prints: tmp:_____<---nothing! ??
//I am trying to copy the result from above into a field of the struct pointer array 
char tmp2[256];
memcpy(InMemory[Idx]->filename,dsp.result.list[i].owner_name,256);
memcpy(tmp2,InMemory[Idx]->filename,256);
printf("tmp:%s\n",tmp2);

Upvotes: 0

Views: 180

Answers (1)

Anon
Anon

Reputation: 2656

From your code, you have not allocated member elementes of InMemory

for (i=0;i<SIZE;i++)
{ 
  // allocate elements here
  InMemory[i]->filename = malloc(....)
  // other allocations
}

// now use memcpy

Upvotes: 1

Related Questions