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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:35:24+00:00 2026-05-26T00:35:24+00:00

I have following piece of code: typedef uint8_t array_t[8]; static array_t _my_array; static const

  • 0

I have following piece of code:

typedef uint8_t array_t[8];
static array_t _my_array;
static const array_t * foo(void) {
    return &_my_array; // <-- return from incompatible pointer type
}   

how do I fix this error? what am I doing wrong?
do I have to cast _my_array as (const array_t *)? Shouldn’t cast from pointer to const pointer be implicit?

Note:

return _my_array;

works as well, i.e. compiles with the same warning.

  • 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-26T00:35:24+00:00Added an answer on May 26, 2026 at 12:35 am

    The problem is the const; the function returns const array_t * (pointer to const array_t), but the returned expression, &_my_array, is of type array_t *, and the two types are not compatible.

    The simplest fix is to drop the const from the return type:

    typedef uint8_t array_t[8];
    static array_t _my_array;
    static array_t * foo(void) {
        return &_my_array;
    }
    

    EDIT:

    I’m hesitant to suggest a compiler bug, but I’ve come up with a test program that I think indicates either a bug in gcc or a very obscure aspect of the C standard.

    typedef int this_type;
    typedef int that_type[8];
    
    static this_type this;
    static that_type that;
    
    static const this_type *this_func(void) {
        return &this;
    }
    
    static const that_type *that_func(void) {
        return &that;
    }
    

    When I compile this with gcc -c -std=c99 -pedantic-errors c.c (gcc 4.5.2), I get:

    c.c: In function ‘that_func’:
    c.c:12:5: error: return from incompatible pointer type
    

    Why does it complain about an implicit conversion from that_type* to const that_type*, but not about a conversion from this_type* to const this_type*.

    Since that_type is a typedef, it’s an alias for an array type, and that_type* is a pointer to an array (not a pointer to an element of an array); there is no array-to-pointer conversion as far as I can tell. I don’t think the fact that this_type is an integer type and that_type is an array type should make any difference.

    Another data point: on Solaris 9, cc -c -Xc c.c doesn’t complain.

    Logically, converting a pointer to foo to a pointer to const foo should be safe; it doesn’t produce any opportunity to violate const-correctness.

    If I’m right, then the code in the question is valid, gcc’s warning is incorrect, and you can work around it either by dropping the const on the function definition (make it return array_t* rather than const array_t*, or by adding a cast on the return statement:

    return (const array_t*)&_my_array;
    

    If I’m wrong, I expect someone will point it out soon.

    (My use of this, a C++ keyword, as an identifier is somewhat deliberate. This is a C question. I understand that C++ has slightly different rules in this area.)

    EDIT2:
    I’ve just submitted a gcc bug report.

    EDIT3:
    Joseph S. Myers responded to my bug report:

    This is not a bug. You can implicitly convert “pointer to int” to
    “pointer to const int”, but not “pointer to array of int” to “pointer
    to array of const int” (see 6.5.16.1), and “const that_type *” is
    “pointer to array of const int” (there is no such type as “pointer to
    const array of int”, which would be a permitted target of such a
    conversion; see 6.7.3#8).

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

Sidebar

Related Questions

I have the following piece of code pattern: void M1(string s, string v) {
I have following piece of code: public void ProcessRequest (HttpContext context) { context.Response.ContentType =
I have the following piece of code which replaces template markers such as %POST_TITLE%
I have the following piece of code in Visual C++ 2005 : : class
I have trouble with the following piece of code. When I go through with
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel
I have the following piece of code which doesn't compile when I try to
I have following piece of code NSMutableArray *mutArray = [[NSMutableArray alloc] init]; [mutArray addObject:
I've the following piece of code in my program which dynamically links wtsapi32.dll file

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.