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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:52:46+00:00 2026-05-24T01:52:46+00:00

#include <stdio.h> #include <stdlib.h> const int * func() { int * i = malloc(sizeof(int));

  • 0
#include <stdio.h>
#include <stdlib.h>

const int * func()
{
    int * i = malloc(sizeof(int));
    (*i) = 5;    // initialize the value of the memory area
    return i;
}

int main()
{
    int * p = func();
    printf("%d\n", (*p));
    (*p) = 3;       // attempt to change the memory area - compiles fine
    printf("%d\n", (*p));
    free(p);
    return 0;
}

Why does the compiler allow me to change (*p) even if func() returns a const pointer?

I’m using gcc, it shows only a warning on the int * p = func(); line : “warning: initialization discards qualifiers from pointer target type”.

Thanks.

  • 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-24T01:52:47+00:00Added an answer on May 24, 2026 at 1:52 am

    Your program is not valid. C forbids implicitly removing a const like that, and in conformance to the spec GCC should give you at least a warning for that code. You would need a cast to remove the const.

    Having consumed a warning for that, you can however rely on the program to work (although not anymore from a Standards point of view), because the pointer is pointing to a malloc’ed memory area. And you are allowed to write to that area. A const T* pointing to some memory doesn’t mean that the memory is thereafter marked immutable.

    Note that the Standard doesn’t require a compiler to reject any program. The Standard merely requires compilers to sometimes emit a message to the user. Whether that’s an error message or warning and how the message is emitted and whatever happens after that emission, isn’t specified by the Standard at all.

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

Sidebar

Related Questions

Test the following code: #include <stdio.h> #include <stdlib.h> main() { const char *yytext=0; const
I just translated this program, #include <stdio.h> int dam[1000][1000]; int main (int argc, const
#include <stdio.h> int main() { unsigned long long int num = 285212672; //FYI: fits
sorry i modified the code now: #include <stdio.h> #include <stdlib.h> #include <string.h> void main()
It seems that when I run the following code: #include <stdio.h> #include <stdlib.h> int
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
I have a program here: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include
Can anyone explain to me why this isn't working? #include <stdio.h> #include <stdlib.h> char
This is the code #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h>
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T

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.