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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:05:48+00:00 2026-05-13T18:05:48+00:00

char sXSongBuffer[20][30]; sXSongBuffer = {Thriller, Don’t Stop Till You Get Enough, Billy Jean}; Why

  • 0
char sXSongBuffer[20][30];
sXSongBuffer = {"Thriller", "Don't Stop Till You Get Enough", "Billy Jean"};

Why does this return the error expected expression before ‘{’ token? The reason I want to initialize my array like this is so that I can change its contents like this later:

sXSongBuffer = {"New Song", "More Music From Me"};
  • 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-13T18:05:48+00:00Added an answer on May 13, 2026 at 6:05 pm

    You can’t assign to arrays in C. C allows initializing arrays with values that are compile-time constants. If you want to change the values later, or set values that are not compile-time constants, you must assign to a particular index of the array manually.

    So, your assignment to sXSongBuffer is disallowed in C. Moreover, since sXSongBuffer[0] to sXSongBuffer[19] are arrays too, you can’t even say: sXSongBuffer[0] = "New Song";

    Depending upon what you want, this may work for you:

    /* declare sXSongBuffer as an array of pointers */
    char *sXSongBuffer[30] = {
        "Thriller",
        "Don't Stop Till You Get Enough",
        "Billy Jean",
        NULL /* set the rest of the elements to NULL */
    };
    size_t i;
    /* and then later in your code */
    sXSongBuffer[0] = "New Song";
    sXSongBuffer[1] = "More Music From Me";
    for (i=2; i < sizeof sXSongBuffer; ++i)
        sXSongBuffer[i] = NULL;
    

    But the above only works if you know all your strings at compile time. If not, you will have to decide if you want “big-enough” arrays, or if you need dynamic memory for the strings and/or the number of strings. In both cases, you will want to use an equivalent of strcpy() to copy your strings.

    Edit: To respond to the comment:

    You’re declaring an array of 30 char pointers with the first three elements pointing to buffers the size of the strings, ie the buff pointed to by sXSongBuffer[0] won’t hold any string larger than "Thriller" and if he does sXSongBuffer[0] = malloc(32); He’ll get a minor memory leek. Also, he’ll have to malloc memory for each of the rest of the slots in the array. He should either use 2d char arrays like in the OP + a designated init, or malloc each buffer at run time and copy in the values. He’ll also need to remember to free any memory he mallocs.

    sXSongBuffer in char *sXSongBuffer[30]; is an array of size 30, with each element being a char *, a pointer to char. When I do:

    char *sXSongBuffer[30];
    

    each of those 30 pointers is uninitialized. When I do:

    char *sXSongBuffer[30] = { "Thriller", ... };
    

    I set the pointers to different read-only locations. There is nothing preventing me to then “re-point” the pointers somewhere else. It is as if I had:

    char *data = "Hello";
    printf("%s\n", data);
    data = "Hello, world";
    printf("%s\n", data);
    

    In the above snippet, I assign data to "Hello" first, and then change it to point to a longer string later. The code I had above in my answer did nothing more than reassign sXSongBuffer[i] to something else later, and since sXSongBuffer[i] is a pointer, the assignment is OK. In particular, sXSongBuffer[0] is a char *, and can point to any valid location that has a char in it.

    As I said later in my answer, if the strings aren’t known at compile-time, this scheme doesn’t work, and one has to either use arrays with “big enough” sizes, or dynamically allocate memory that’s big enough.

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

Sidebar

Related Questions

char *myfunc() { char *temp = string; return temp; } In this piece of
char data_[4096]; ... socket_.async_read_some(boost::asio::buffer(data_, 4096), boost::bind(&client::handle_read_header, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); when function handle_read_header is fired,
char bytes[0x7FFFFFFF]; /* this? */ int main() { return 0; } Over 2Gb? (if
char arr[100]; cin.get(arr,100); Is this safe? Will the null-character be appended at the end
char imei_temp[14] = {0, }; strcpy(imei_temp, 00000000000000); According to my understanding this is valid
char b; operator<<(cout,(operator>>(cin,b))); this is not compiling in vc++ because all 8 overloads cant
char character = 'c'; string str = null; str = character.ToString();//this is ok char[]
char byte_to_ascii(char value_to_convert, volatile char *converted_value) { if (value_to_convert < 10) { return (value_to_convert
char c=7; The above statement will execute in java without error even though we
char ParseCmd(char *buf,int len) { char *p; p = strtok(buf, ); return *p; }

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.