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

  • SEARCH
  • Home
  • 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 8923495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:08:14+00:00 2026-06-15T07:08:14+00:00

Please consider the following code. typedef struct{ int field_1; int field_2; int field_3; int

  • 0

Please consider the following code.

typedef struct{
    int field_1;
    int field_2;
    int field_3;
    int field_4;

    uint8_t* data;
    uint32_t data_size;
} my_struct;

void ext_function(inalterable_my_struct* ims, ...);

I want to allow ext_function (written by a third party) to modify only field_3and field_4 in my_struct. So I do the following:

typedef struct{
    const int field_1;
    const int field_2;
    int field_3;
    int field_4;

    const uint8_t* data;
    const uint32_t data_size;
} inalterable_my_struct;

void ext_function(inalterable_my_struct* ims, ...);

Is it safe to cast pointers between my_struct and inalterable_my_struct before calling ext_function (as shown after)?

void call_ext_function(my_struct* ms){
    inalterable_my_struct* ims = (inalterable_my_struct*)ms;
    ext_function(ims, ...);
}
  • 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-15T07:08:15+00:00Added an answer on June 15, 2026 at 7:08 am

    I don’t think this is a good idea.

    The called function can always cast away any const:ness, and modify the data if it wants to.

    If you can control the callpoints, it would be better to create a copy and call the function with a pointer to the copy, then copy back the two fields you care about:

    void call_ext_function(my_struct* ms)
    {
        my_struct tmp = *ms;
        ext_function(&tmp, ...);
        ms->field_3 = tmp.field_3;
        ms->field_4 = tmp.field_4;
    }
    

    much cleaner, and unless you do this thousands of times a second the performance penalty should really be minor.

    You might have to fake the pointer-based data too, if the function touches it.

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

Sidebar

Related Questions

Please consider the following code: typedef struct { int type; } object_t; typedef struct
please consider the following code: typedef struct Person* PersonRef; struct Person { int age;
Consider the following code template<typename T, int N> struct A { typedef T value_type;
Please consider the following piece of code: int main() { typedef boost::ptr_vector<int> ptr_vector; ptr_vector
Please consider the following code. enum type {CONS, ATOM, FUNC, LAMBDA}; typedef struct{ enum
Please consider the following code, struct foo { foo() { std::cout << Constructing! <<
please consider the following code: template <typename T> struct foo { template <typename S>
Please consider the following code: #include <stdio.h> int main() { static int counter=5; printf
Please consider the following code: vector<int> myVector; myVector.push_back(10); myVector.erase(myVector.end()); This code compiles and runs
Please consider the following code. struct foo { }; template<typename T> class test {

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.