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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:07:16+00:00 2026-05-11T16:07:16+00:00

This is such a dumb question it’s frustrating even asking it. Please, bear with

  • 0

This is such a dumb question it’s frustrating even asking it. Please, bear with me, I’m feeling particularly dumb over this one today….

I’ve got a library with a certain typedef struct. basically:

typedef struct {int x; int y;} coords;

What I really wanted to do was declare in my header a variable array of this:

coords MyCoords[];

(I recently switched it to just coords *MyCoords; and then ran a MyCoords = new coords[4];)

and then in my init I wanted to hardcode some values in it.

MyCoords[] = {{0, 0}, {2, 2} etc. 

I must just be brainfarting today because it’s not letting me do any manner of simply putting in even something simple like MyCoords[0] = {0, 0}

sorry about some of the syntax above. Thanks for any help. I don’t even care about hard-coding all the values at once. I’m now fine with just geting MyCoords[0] = {0, 0} working. I could do MyCoords[0].x = 0; MyCoords[0].y = 0 but then I’d never figure out what I’m doing wrong. Thanks a lot anyone..

  • 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-11T16:07:17+00:00Added an answer on May 11, 2026 at 4:07 pm

    Well, what you created is not a variable array. It’s an array whose size is not known. That array has an incomplete type, and can thus not be defined in C++. What you can do is to make it just a declaration by putting extern before it.

    extern coords MyCoords[];
    

    Then, in the .cpp file that initializes it, you can then create (define) that array – beware you should do it at namespace scope, outside any function, of course:

    coords MyCoords[] = {{1, 2}, {3, 4}, ... };
    

    If what you want is an array whose size is not known at compile time yet, but rather at runtime, you should use a std::vector:

    std::vector< coords > MyCoords;
    

    Then later if you fill it, do

    MyCoords.push_back( coords(1, 2) );
    MyCoords.push_back( coords(3, 4) );
    ....
    

    Or create a local array and then initialize the vector using a copy of the elements of that array:

    coords c[] = ....;
    MyCoords.insert(MyCoords.end(), c, c + sizeof c / sizeof *c);
    

    Well, in C, it is slightly different than in C++. You can define an array whose dimension isn’t known. What happens is that it’s taken to be a “tentative” definition. If at the end of the file (more precisely, of its translation unit, which is that file, and all the files it includes – meaning everything the compiler translates together) there is not a subsequent definition of it that includes the size, then the size is taken to be 1. However, in C++ there are no tentative definitions, and a definition that does not include the size and does not contain an initializer that can tell the size is an invalid C++ program.

    For the reason why your code goes grazy, you are doing

    MyCoords[0] = {0, 0}
    

    Which the compiler will read as:

    -> Set LHS to RHS
    -> -> LHS is a variable of type `coords`. Fine
    -> -> RHS is... hmm, `{0, 0}`. What the heck is it??
    

    You see, the compiler has no clue what the right hand side is supposed to mean. C99, for that reason (1999 version of C) introduced so-called compound literals. It allows you to write

    MyCoords[0] = (coords){0, 0};
    

    And it will actually do what you want it to do – creating a right hand side value of type coord and assigns it to the left hand side. But you should just, already from a compatibility reason (many C compilers are not reasonable C99 compliant – and neither C++ nor C89 supports compound literals), use one of the previous ways i showed you.

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

Sidebar

Ask A Question

Stats

  • Questions 85k
  • Answers 85k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Linq to SQL tries to translate your linq-query into SQL,… May 11, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer The most important bit is in "primarily intended for Windows".… May 11, 2026 at 5:11 pm
  • Editorial Team
    Editorial Team added an answer You have at least two possible options. You can either… May 11, 2026 at 5:10 pm

Related Questions

I have a repository data access pattern like so: IRepository<T> { .. query members
Maybe this is a dumb question, but is there any way to convert a
Ok I have a table with a few fields. One of the fields is
I am trying the FDT editor on a project that previously ran successfully in
I'm required to provide a handover to our content editors for the update corporate

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.