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 8312745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:15:35+00:00 2026-06-08T20:15:35+00:00

i have a packet struct which have a variable len for a string example:

  • 0

i have a packet struct which have a variable len for a string example:

BYTE StringLen;
String MyString; //String is not a real type, just trying to represent an string of unknown size

My question is how i can make the implementation of this packet inside an struct without knowing the size of members (in this case strings). Here is an example of how i want it to “look like”

void ProcessPacket (PacketStruct* packet)
{
     pointer = &packet.MyString;
}

I think its not possible to make since the compiler doesn’t know the size of the string until run time. So how can make it look high level and comprehensible?.

The reason i need structs its for document every packet without the user actually have to look any of the functions that analyze the packet.

So i can resume the question to: is there a way to declare an struct of undefined size members or something close as a struct?

  • 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-08T20:15:36+00:00Added an answer on June 8, 2026 at 8:15 pm

    I would recommend a shell class that just interprets the packet data.

    struct StringPacket {
        char *data_;
        StringPacket (char *data) : data_(data) {}
        unsigned char len () const { return *data_; }
        std::string str () const { return std::string(data_+1, len());
    };
    

    As mentioned in comments, you wanted a way to treat a variable-sized packet like a struct. The old C way to do that was to create a struct that looked like this:

    struct StringPacketC {
        unsigned char len_;
        char str_[1]; /* Modern C allows char str_[]; but C++ doesn't */
    };
    

    And then, cast the data (remember, this is C code):

    struct StringPacketC *strpack = (struct StringPacketC *)packet;
    

    But, you are entering undefined behavior, since to access the full range of data in strpack, you would have to read beyond the 1 byte array boundary defined in the struct. But, this is a commonly used technique in C.

    But, in C++, you don’t have to resort to such a hack, because you can define accessor methods to treat the variable length data appropriately.

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

Sidebar

Related Questions

I have the following packet layout: struct PacketLayout { int GateCode; BYTE StringLen; char
I have the following code int ParseData(unsigned char *packet, int len) { struct ethhdr
I have a struct PACKET which contains two int fields. How would I declare
I have some struct containig a bitfield, which may vary in size. Example: struct
I have a struct struct Packet { int senderId; int sequenceNumber; char data[MaxDataSize]; char*
I have a java program which sends a udp packet to a server when
i have made a module which aims to send the ping packet using ioctl
I have a struct that's used in my internet application, which looks like: struct
I have a packet from a server which is parsed in an embedded system.
I have a TCP Client,which puts a packet in a structure using System.Runtime.InteropServices; [StructLayoutAttribute(LayoutKind.Sequential)]

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.