Hi string copy not working with following code

VidZone[Loop].pui8Source = (UINT8 **)malloc(FileCount * sizeof(UINT8 *));

for(FileIndex = 0 ; FileIndex < VidZone[Loop].ui32NoOfSourceFiles ; FileIndex++)
         {
             VidZone[Loop].pui8Source[FileIndex] = (UINT8 *)malloc(255 * sizeof(UINT8));
             strncpy(VidZone[Loop].pui8Source[FileIndex] , pui8SourceString[FileIndex] , 255);
         }

"pui8Source" is a double poiner and pui8SourceString[FileIndex] array of character pointers.

Please let me know if any problem with my code

It is copying for first instance when FileIndex=0 but not for second instance.

Regards,
Anand Shah

Hi There, I think line 5 has a missing "*" between 255 and sizeof(UINT8) , it should read:

VidZone[Loop].pui8Source[FileIndex] = (UINT8 *)malloc(255 * sizeof(UINT8));

Also, please use [ code ] tags, as it helps other people understand your code.

hi,
In my code '*' is there.I forgot to add it here.Sorry for posting wrong code.But still its not working

Does VidZone[Loop].ui32NoOfSourceFiles have the value you expect?

Yes, it has.

What about FileCount ? You reserve the memory for VidZone[Loop].pui8Source to be a size of FileCount*sizeof(UINT8) but then you loop over VidZone[Loop].ui32NoOfSourceFiles elements. If these are not guaranteed to be the same number (for example if FileCount is one for some reason) then you'd run into problems.

Other than that, the actual code that you've put here seems OK to me. Perhaps you could provide a little more, or a short, complete program that demonstrates the problem, or an example of exactly what output you get?

This is the line just before the code I shared.From this is clear that both the value are same.
VidZone[Loop].ui32NoOfSourceFiles = FileCount;

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.