Thread: Structures, Pointers and arrays

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    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
    Last edited by jimbo2; 04-21-2014 at 04:26 PM.

  2. #2
    Registered User
    Join Date
    Feb 2014
    Location
    NY
    Posts
    56
    Use a loop that tests against the string length of src.

  3. #3
    Registered User
    Join Date
    Feb 2014
    Location
    NY
    Posts
    56
    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;
    }
    Last edited by 3DT; 04-21-2014 at 09:52 PM.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers/arrays/structures
    By johngoodman in forum C Programming
    Replies: 14
    Last Post: 02-07-2013, 01:24 PM
  2. Arrays of pointers inside structures.
    By Posto2012 in forum C Programming
    Replies: 7
    Last Post: 11-18-2009, 12:58 AM
  3. Structures, arrays, pointers, malloc.
    By omnificient in forum C Programming
    Replies: 9
    Last Post: 02-29-2008, 12:05 PM
  4. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:44 AM
  5. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM