-
help on copying arrays
Code:
typedef struct
{
char Jump[ 3 ];
char Manufacturer[ 8 ];
char BytesPerSector[ 2 ];
} Disk;
in a funtion, there is this
Disk *theDisk
char array[40]={'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', .....};
the problem is how do i copy the 3rd element to 8th element of array to the the member ( thDisk->Manufacturer ) of structure Disk?
and then display it on the screen.
thx for the help
-
Code:
#include <stdio.h>
#include <string.h>
typedef struct
{
char Jump[ 3 ];
char Manufacturer[ 8 ];
char BytesPerSector[ 2 ];
} Disk;
// if you use a pointer it must point to something before its used.
Disk* theDisk;
char array[40]={'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', ...};
// make theDisk point to a valid instance of Disk
memcpy(theDisk->Manufacturer, &array[3], 5);
-
thx man
that is helped a lot.
-
Don't forget, if you're going to use manufacturer as a string, it'll need to be null terminated. I'm guessing the struct is initialised to 0, as per your other thread (if I remember right)? ... which means it'll work OK.
Also, this is close:
>memcpy(theDisk->Manufacturer, &array[3], 5);
but the request was to start at the third element, whereas this starts at the 4th (the index count starts at 0, not 1).
:)