Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8788479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:09:15+00:00 2026-06-13T22:09:15+00:00

I want to define a structure in C for network transferring, for example I

  • 0

I want to define a structure in C for network transferring, for example I want to transfer an Animal structure, which contains a variable length of animal name.

AFAIK, one way is using a predefined length of char array, or using a buffer in the struct, and we can parse the buffer (e.g., the first 4 bytes are the animal name length, followed by the animal name, and other fields’ length and other fields’ values), the advantage of the latter method is that it allows variable name length, as following code indicates:

struct Animal
{
    char   name[128];
    int    age;
}

or:

struct Animal
{
    int    bufferLen;
    char*  pBuffer;
}

My question is: are my approaches correct? i.e., there are the standard ways to transfer struct, and are there better ways?

My second question is: do I need to pay attention to paddding, i.e., use #pragma pack(push/pop, n)?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:09:16+00:00Added an answer on June 13, 2026 at 10:09 pm

    Both work fine, however, if you use a fixed length packed sturct it makes it slightly easier to deal with, but you may send more data than you need, for example, the following code, Assuming a 4 byte integer, will send 132 bytes:

    //packed struct
    struct Animal {
        char   name[128];
        int    age;
    };
    
    Animal a = {"name", 2};
    send(fd, &a, sizeof(a), 0);
    //and you're done
    

    On the other hand variable length fields will need more work to allocate memory and pack in a single packet, but you will be able to send the exact number of bytes you want, 9 bytes in this case:

    //not necessarily packed   
    struct Animal {
        char   *name;
        int    age;
    };
    
    //some arbitrary length
    int name_length = 50;
    //you should check the result of malloc
    Animal a = {malloc(name_length), 2}; 
    
    //copy the name
    strcpy(a.name, "name");
    
    //need to pack the fields in one buff    
    char *buf = malloc(strlen(a.name)+ 1 + sizeof(a.age));
    memcpy(buf, a.name, strlen(a.name)+1);
    memcpy(buf, &a.age, sizeof(a.age));
    
    send(fd, buf, strlen(a.name)+ 1 + sizeof(a.age));
    //now you have to do some cleanup
    free(buf);
    free(a.name);
    

    Edit: This is of course if you want to implement that yourself, you could use a library to serialize the data for you. Also, check the example serialization code in the Beej’s Guide to Network Programming

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I want to pass a structure defined in windows.h to one of method
I want to define an structure, where some math constants would be stored. Here
CGRect type is a structure type. If I want to define a property as
I want to define a global structure in C# and use it in three
I've created a structure called panel which render in OpenGL,works fine,but I want to
I'm currently working on a big JavaScript project for which we want to define
I want to define a variable that will store two parameters — username and
I want to define some data in a row, which is master/detail in nature.
At first sorry my bad English. I want define routing for this url: For
I use emacs in ubuntu 12.04. i want define some background or foreground in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.