# Thread: Structures, Pointers and arrays

1. ## Structures, Pointers and arrays

Hi everyone, i have the following problem:

I have a struct which is declared as:

Code:
```typedef struct
{
unsigned char src[6], dst[6];
unsigned short type;

} eth_hdr;```
and i have a function,in which i would like to do something like this:

Code:
```void ethernet_decap(unsigned char *eth_in, eth_hdr *hdr)
{
hdr -> src = eth_in;
}```
The 'eth_in' is not the same size as the other array, its a lot bigger, but i need to copy the first 6 elements of the array to the 'src' array.
I know its wrong, but you can see what i would like to point at.
And without using strcpy or giving values to that array by a 'for' cicle or something..I want to solve it by pointers and casting. Is that possible somehow?

Thanx

2. Use a loop that tests against the string length of src.

3. Code:
```    char p,*ptr = &p;

p=eth_in[0];
src[0]=*ptr;
p=eth_in[1];
src[1]=*ptr;
p=eth_in[2];
src[2]=*ptr;
p=eth_in[3];
src[3]=*ptr;
p=eth_in[4];
src[4]=*ptr;
p=eth_in[5];
src[5]=*ptr;
src[6]="\0";
/*with type casting*/
p=(char)eth_in[i];
src[i]=*ptr;```
A loop would do the same thing.
Code:
```for(i=0;i<strlen(src)-1;i++){
p=eth_in[i];
src[i]=*ptr;
}```

4. You can't "copy" things by casting. Casting changes the way the memory/address you're accessing is represented. It doesn't copy anything. You can copy structures by the assignment operator.

What you want to do is best accomplished using strncpy to copy the first 5 characters from the string (you need the 6th for the NULL terminator).