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

The Archive Base Latest Questions

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

I have tried this but it won’t work: #include <stdio.h> int * retArr() {

  • 0

I have tried this but it won’t work:

#include <stdio.h>

    int * retArr()
    {
    int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    return a;
    }

    int main()
    {
    int a[3][3] = retArr();
    return 0;
    }

I get these errors:

Error 3 error C2075: ‘a’ : array initialization needs curly braces
4 IntelliSense: return value type does not match the function type

What am I doing wrong?

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

    A struct is one approach:

    struct t_thing { int a[3][3]; };
    

    then just return the struct by value.

    Full example:

    struct t_thing {
        int a[3][3];
    };
    
    struct t_thing retArr() {
        struct t_thing thing = {
            {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9}
            }
        };
    
        return thing;
    }
    
    int main(int argc, const char* argv[]) {
        struct t_thing thing = retArr();
        ...
        return 0;
    }
    

    The typical problem you face is that int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; in your example refers to memory which is reclaimed after the function returns. That means it is not safe for your caller to read (Undefined Behaviour).

    Other approaches involve passing the array (which the caller owns) as a parameter to the function, or creating a new allocation (e.g. using malloc). The struct is nice because it can eliminate many pitfalls, but it’s not ideal for every scenario. You would avoid using a struct by value when the size of the struct is not constant or very large.

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

Sidebar

Related Questions

I have tried this but it does not work (even if I specify .wav
I have tried this but it doesn't work: tell application Preview set myfile to
How can I join 2 Point3f arrays together? I have tried this but it
i have tried this code to redirect a php page.but it s not working
So I have tried to do this extensively but to no avail.. I'm writting
I need to have a TreeView with only vertical scrollbar. I tried this but
Does file_get_contents maintain line breaks? I thought it did but I have tried this:
I have tried searching over the internet about this problem but not able to
I have tried create this table, but nothing I have tried works from FKs.
I must have looked at 10-15 tutorials on this already but just cannot get

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.