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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:22:27+00:00 2026-05-23T23:22:27+00:00

In the C language, why does the following expression fail? map = malloc(sizeof(Map) *

  • 0

In the C language, why does the following expression fail?

map = malloc(sizeof(Map) * tiles);
map = {
    0,2,0,0,0,0,0,0,2,0,
    0,1,0,0,0,0,0,0,1,0,
    0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,
    2,1,0,0,0,0,0,0,1,2,
    0,0,0,0,0,0,0,0,0,0
};

I just want to fill data in such a “row/column” format into a C array. However, the compiler fails with

error: expected expression before ‘{’ token

(in the map = { line). Filling the array in other ways works fine, and I am sure the brackets work for initializations…I can’t do that after I have allocated memory?

EDIT: I solved it by making a temporary char array and then feeding the data to the malloced map in a for loop.
Still. I’d like to know why the above code would leak memory as pointed below. And would my fix (parsing the temp array and setting the map data with it) leak memory as well?

  • 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-23T23:22:27+00:00Added an answer on May 23, 2026 at 11:22 pm

    Your Map * map; is a pointer, not an array. Curly braces are for aggregate initialization:

    int x[3] = { 1, 2, 3 };
    

    Pointers are not arrays, and you cannot fill memory with the aggregate initialization syntax.

    Here is the closest construct that would work:

    typedef struct Map_ { int a; int b; } Map; // some struct
    
    Map m[] = { {1,2}, {3,4}, {5,6} };  /* we initialized "Map m[3]",
                                           it has automatic storage! */
    

    Note that each element of the brace-list must itself initialize the base type of the aggregate.

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

Sidebar

Related Questions

In the Java programming language, are the following two loops equivalent for any expression
PHP, as we all know is very loosely typed. The language does not require
Does the Java language have delegate features, similar to how C# has support for
does anyone have any resources for learning assembly language on x86? I'm trying to
Does javascript coding work better with any particular server language?
Does anyone know of an accurate source for an (E)BNF for the Java language?
Does an auto-formatting tool exist for vi that'll allow me to define per language
When testing in any language, how does everybody phrase their assertion messages ? I
Why does the new operator exist in modern languages such as C# and Java?
Why does this lambda expression not compile? Action a = () => throw new

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.