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

The Archive Base Latest Questions

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

static char buf[8]; void foo(){ const char* ptr = buf; /* … */ char*

  • 0
static char buf[8];
void foo(){
    const char* ptr = buf;
    /* ... */
    char* q = (char*)ptr;
}

The above snippet will generate "warning: cast discards ‘__attribute__((const))’ qualifier from pointer target type [-Wcast-qual]". I like -Wcast-qual since it can help me from accidentally writing to memory I shouldn’t write to.

But now I want to cast away const for only a single occurrence (not for the entire file or project). The memory it is pointing to is writable (just like buf above). I’d rather not drop const from ptr since it is used elsewhere and keeping to pointers (one const and one non-const) seems like a worse idea.

  • 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:13:41+00:00Added an answer on June 13, 2026 at 10:13 pm

    In GCC 4.2 and later, you can suppress the warning for a function by using #pragma. The disadvantage is you have to suppress the warning across the whole function; you cannot just use it only for some lines of code.

    #pragma GCC diagnostic push  // require GCC 4.6
    #pragma GCC diagnostic ignored "-Wcast-qual"
    void foo(){
        const char* ptr = buf;
        /* ... */
        char* q = (char*)ptr;
    }
    #pragma GCC diagnostic pop   // require GCC 4.6
    

    The advantage is your whole project can use the same warning/errors checking options. And you do know exactly what the code does, and just make GCC to ignore some explicit checking for a piece of code.
    Because the limitation of this pragma, you have to extract essential code from current function to new one, and make the new function alone with this #pragma.

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

Sidebar

Related Questions

static char st[][8192]; void foo ( int tab_size){ st = (char**) malloc ((tab_size+1)*sizeof(char)*8192); }
c++: static void doIp(byte data[]) { unsigned char j, k; byte val; byte buf[8];
I' ve got such a function static ssize_t read_mydevice(struct file *filp, char* buf, size_t
struct X { constexpr static char a1[] = hello; // Okay constexpr static const
I tried the following line: static const const char* values[]; But I get the
What is default calling convention for a static function say: static void PrintHelloWorld(char* s)
File 1: static char* const path; //GLOBAL int main() { path = FunctionReturningPath(); UsePath()
We need to define a const static char pointer in each and every header
I would like to have a static char array member initialized in terms of
I have a class with a static char array. The size of the array

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.