Struct (with pointer) to byte array

This is a discussion on Struct (with pointer) to byte array within the C Programming forums, part of the General Programming Boards category; Hi, I have a struct that I need to convert to a byte(=char) array. Normally, I know how to do ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    4

    Struct (with pointer) to byte array

    Hi,

    I have a struct that I need to convert to a byte(=char) array. Normally, I know how to do that using memcpy() but in this case the struct contains a pointer that I use to allocate memory dynamically elsewhere is my program.

    Something like:

    Code:
    struct my_struct {
        int size;
        int num;
        int *list;
    } sb;
    and later on:
    Code:
    sb.list = (int*)malloc(some_var);
    Is this even possible? If it is, what approach should I follow?

    Thanks.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,450
    first convert struct contents without pointer, then convert memory pointed by the pointer
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 10:50 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21