Thread: append data from one pointer fo another

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    77

    append data from one pointer fo another

    g'day,
    Could anybody help me with joinning data?
    Below what I do....

    Code:
    struct data {
        union {
            struct {
                u_int16_t match_size;
                char name[512];
                u_int8_t revision;
            } user;
            u_int16_t match_size;
        } u;
        unsigned char data[0];
    };
    
    int main{
    struct data *p=NULL, *m=NULL;
    
    p = foo();// here i add something to struct struct data
    m = oof();// and here too
    
    memcpy(p+sizeof(struct data),m,sizeof(struct data)); // it's not appending data from *m pointer to *p pointer, why? how that make?
    
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you actually allocate enough data for both structures?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    I guess yes, for just one it works well, but I can't add second.
    and I add here size for second struct...
    p+sizeof(struct data)

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. That's not allocating memory. That's assuming memory is there, and trying to access it.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    ok, but if I did that, I've got in *t only data from *p and lose data from *m.
    Code:
    int main{
    struct data *p=NULL, *m=NULL,*t=NULL;
    
    p = foo();// here i add something to struct struct data
    m = oof();// and here too
    
    t=calloc(1,sizeof(*p)+sizeof(*m));
    memcpy(t,p,sizeof(*p));
    memcpy(t+sizeof(*m),l,sizeof(*m));
    }
    how that make?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    struct data *p, *q, *r;
    
    p = foo(); /* allocates 1 instance of data, does whatever... */
    q = bar(); /* ...same... */
    r = realloc( p, 2 * sizeof *p );
    if( r )
    {
        memcpy( r + sizeof *r, q, sizeof *q );
        free( q );
        p = r;
    }
    Something like that perhaps.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program terminates abruptly
    By roaan in forum C Programming
    Replies: 3
    Last Post: 08-28-2009, 03:53 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM