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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:20:25+00:00 2026-05-25T23:20:25+00:00

I have a unsigned char pointer which contains a structure.Now I want to do

  • 0

I have a unsigned char pointer which contains a structure.Now I want to do the following

unsigned char buffer[24];

//code to fill the buffer with the relevant information.

int len = ntohs((record_t*)buffer->len);

where record_t structure contains a field called len. I am not able to do so and am getting the error.

error: request for member ‘len’ in something not a structure or union.

Then I tried:

int len = ntohs(((record_t*)buffer)->len);

so as to get the operator precedence right. That gave me:

warning: dereferencing type-punned pointer will break strict-aliasing rules.

then I declared

record_t *rec = null;

rec = (record_t*)

what am I doing wrong here?

  • 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-05-25T23:20:25+00:00Added an answer on May 25, 2026 at 11:20 pm

    According to the C and C++ standards, it is undefined behaviour to access a variable of a given type through a pointer to another type. Example:

    int a;
    float * p = (float*)&a;  // #1
    float b = *p;            // #2
    

    Here #2 causes undefined behaviour. The assignment at #1 is called "type punning". The term "aliasing" refers to the idea that several different pointer variables may be pointing at the same data — in this case, p aliases the data a. Legal aliasing is a problem for optimization (which is one of the main reasons for Fortran’s superior performance in certain situations), but what we have here is flat-out illegal aliasing.

    Your situation is no different; you’re accessing data at buffer through a pointer to a different type (i.e. a pointer that isn’t unsigned char *). This is simply not allowed.

    The upshot is: You should never have had data at buffer in the first place.

    But how to solve it? Make sure you have a valid pointer! There is one exception to type punning, namely accessing data through a pointer to char, which is allowed. So we can write this:

    record_t data;
    record_t * p = &data;          // good pointer
    char * buffer = (char*)&data;  // this is allowed!
    
    return p->len;                 // access through correct pointer!
    

    The crucial difference is that we store the real data in a variable of the correct type, and only after having allocated that variable do we treat the variable as an array of chars (which is allowed). The moral here is that the character array always comes second, and the real data type comes first.

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

Sidebar

Related Questions

I have a unsigned char pointer which contains a structure.Now I want to do
I have the following code int ParseData(unsigned char *packet, int len) { struct ethhdr
I have the following code: tmp_data = simulated_data[index_data]; unsigned char *dem_content_buff; dem_content_buff = new
I have unsigned char* , want to convert it to std::string . Can you
I have a incoming stream of bytes (unsigned char) from either a file or
I have a small sample function: #define VALUE 0 int test(unsigned char x) {
So, I have two types at the moment: const unsigned char* unencrypted_data_char; string unencrypted_data;
I have the code: unsigned int length = strlen(somestring); I'm compiling with the warning
I have an native C++ library which makes use of a large static buffer
I've got a very basic application that boils down to the following code: char*

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.