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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:29:25+00:00 2026-06-14T09:29:25+00:00

So I need to remove constness from some variables in C (I know what

  • 0

So I need to remove constness from some variables in C (I know what I’m doing). So I wrote a little macro (UNCONST) which lets me assign an new value to a const value. This works just fine for normal variables of types like int. But this doesn’t work for pointers. So I can’t let a pointer point to a different position using my macro UNCONST without getting an compiler warning.

Here a little test program unconst.c:

#include <stdio.h>

#define UNCONST(type, var, assign) do { \
  type* ptr = (type*)&(var); \
  *ptr = (assign); \
} while(0)

struct mystruct {
  int value;
  char *buffer;
};

int main() {
  // this works just fine when we have an int
  const struct mystruct structure;
  UNCONST(int, structure.value, 6);
  printf("structure.value = %i\n", structure.value);

  // but it doesn't when we have an char *
  char *string = "string";
  UNCONST(char *, structure.buffer, string);
  printf("structure.buffer = %s\n", structure.buffer);

  // this doesn't work either, because whole struct is const, not the pointer.
  structure.buffer = string;
  printf("structure.buffer = %s\n", structure.buffer);
}

compiling & executing

$ LANG=en gcc -o unconst unconst.c
unconst.c: In function ‘main’:
unconst.c:21:3: warning: assignment discards ‘const’ qualifier from pointer target type [enabled by default]
unconst.c:25:3: error: assignment of member ‘buffer’ in read-only object

Is there a way to optimize my macro so this warning doesn’t show up?

  • 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-14T09:29:27+00:00Added an answer on June 14, 2026 at 9:29 am

    The problem in:

    const char *string1 = "string1";
    UNCONST(char *, string1, string2);
    

    is that string1 is not really const, it can be assigned to, but points to an array of const characters.

    Actually, doing:

    string1 = string2;
    

    compiles just fine.

    Other thing is if you want to copy the array. Then I’d write your macro as this:

    #define UNCONST(type, var) (*(type*)&(var))
    

    And so:

    UNCONST(int, i) = 42;
    

    To copy the array of chars you could do:

    strcpy(UNCONST(char**,string1), string2);
    

    FOOTNOTE: actually I find this macro rather useless: I find:

    *(int*)&i = 42;
    

    as cumbersome as this kind of operation should be.

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

Sidebar

Related Questions

I need to remove break from a specific div , i know how to
I need to remove an element from a list which contain inner lists inside.
I need to remove some items from right-click context menu, but the toolbar does
I need to remove from an array some keys. $array = array('a' => 'a',
I need to remove all characters from a string which aren't in a-z A-Z
I need to remove one directory (the leftmost) from variables in Bash. I found
I need to remove the border-top: 1px solid #202020; from .contact on my contact
I need to remove element from com.google.gwt.core.client.JsArray . Also I need to empty the
I need to remove the following format from the end of a string in
I need to remove the following script from my page(ASP.NET 3.5), leaving all other

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.