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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:12:40+00:00 2026-05-13T21:12:40+00:00

This code has an interesting bug: some_struct struct_array1[10] = {0}; some_struct struct_array2[10] = {0}

  • 0

This code has an interesting bug:

some_struct struct_array1[10] = {0};
some_struct struct_array2[10] = {0}
int i;

for (i = 0; 
     i < sizeof(struct_array1) / sizeof(struct_array1[0]); 
     struct_array1[i].value = struct_array2[i++].value = 1)
    ;

For most compilers, the above code results in setting the “value” field of all structs in the respective arrays to 1.
However, for one specific compiler (let’s call it xcc), the structs in struct_array1 are NOT initialized correctly. The “value” field is set to 0 for all structs, which kind of surprised me.

The following code snippet works as expected on all compilers:

for (i = 0; 
     i < sizeof(struct_array1) / sizeof(struct_array1[0]); 
     i++)
{
    struct_array1[i].value = struct_array2[i].value = 1;
}

Now, am I completely off here, or does the offending compiler “xcc” simply display a bug?

I can’t find anything that displays implementation-specific behavior in the first code snippet; from what I understand the postfix incrementation should have precedence over the assignments, and the assignments should be evaluated right-to-left. There should be nothing wierd with the first code snippet, except that it’s a tad unreadable.

  • 1 1 Answer
  • 2 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-13T21:12:40+00:00Added an answer on May 13, 2026 at 9:12 pm

    You have invoked undefined behaviour, because it modifies i and also fetches its value for a purpose other than calculating the new value, without an intervening sequence point.

    The relevant part of the C99 standard is this clause in section 6.5:

    Between the previous and next sequence
    point an object shall have its stored
    value modified at most once by the
    evaluation of an expression.
    Furthermore, the prior value shall be
    read only to determine the value to be
    stored.

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

Sidebar

Related Questions

I have to maintain code from a contractor. It has this interesting snippet: String
This code has generated a 1 result for r on the 1st toss 200
I translated this code(it has bad side effect that it just capture the outer
I have this code that has one button that let's me choose an entry
All my controller has this code on the top of class. public class TestController
I was checking out this question which has this code - (NSArray *) percentagesRGBArray:(float[])
I'm using a tutorial plugin - http://particlebits.com/code/jquery-tutorial/ which has this code attached to the
In my application I use the ShinyBlue.xaml resource dictionary which has this code for
This code I am looking at has a lot of places where I see
This stackoverflow question has an interesting discussion on how to avoid giving enums and

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.