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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:11:28+00:00 2026-05-20T10:11:28+00:00

I am trying to assign values within an array within the condition of a

  • 0

I am trying to assign values within an array within the condition of a for loop:

#include <iostream>
using namespace std;
int* c;
int n;
int main()
{
    scanf("%d", &n);
    c = new int[n];
    for (int i = 0; i < n; c[i] = i++  )
    {
        printf("%d \n", c[i]);
    }
    return 0;
}

However, I am not obtaining the desired output, for n = 5, 0 1 2 3 4. Instead, if I am using the instruction, c[i] = ++i, I am obtaining the output -842150451 1 2 3 4. Could you please explain me we does my code behave like this and how can I correct it?

  • 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-20T10:11:28+00:00Added an answer on May 20, 2026 at 10:11 am

    The value of the expression ++i is the value after i has been incremented. So if it started at 0, you assign value 1 the first time and so on. You can see where the value got assigned, but asking why it got assigned there opens a can of worms.

    Using i in an expression where i is modified via i++ or ++i is undefined behavior unless there is a so-called “sequence point” in between the two. In this case, there isn’t. See Undefined behavior and sequence points for this rather complicated part of the language.

    Although the behaviour is undefined by the standard, and may not be consistent from one run to another, clearly your program has done something. Apparently it didn’t assign to index 0 at all (at least, not before the first print, which is understandable considering that the loop body happens before the last part of the “for”), so you got whatever just so happened to be in that raw memory when it was allocated to you. It assigned 1 to index 1 and so on.

    This means that it may also have attempted to assign the value 5 to c[5], which is a class of bug known as a “buffer overrun”, and more undefined behavior on top of what you’ve already got. Attempting to assign to it probably overwrites other memory, which on any given day may or may not contain something important.

    The fix is to assign some value to c[0], and don’t try to assign to c[5], which doesn’t exist anyway, and don’t try to invalidly use i “at the same time as” incrementing it. Normally you’d write this:

    for (int i = 0; i < n; ++i) {
        c[i] = i;
        printf("%d \n", c[i];
    }
    

    If you’re desperate for some reason to assign in the third clause of a for loop, you could use the comma operator to introduce a sequence point:

    for (int i = 0; i < n; c[i] = i, ++i) {
    }
    

    But of course if you do that then you can’t print the value of c[i] in the loop body. It hasn’t been assigned yet, because the third clause isn’t evaluated until the end of each loop.

    You could also try c[i] = i+1, ++i, but not ++i, c[i] = i because then we’re back to trying to assign to c[5], on the last iteration.

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

Sidebar

Related Questions

I have an asp.net mvc application and i am trying to assign value to
I am trying to assign the onclick event to an anchor tag that already
I am trying to assign absence dates to an academic year, the academic year
I'm using jQuery to parse an XML file, and I'm trying to push each
I am trying to autoreload my page after every 20 seconds. I am using
Trying to understand why you can't use sql_variant for the value type when using
I am struggling to know where to start when trying to assign ranks to
I'm trying to retrieve odd values in a List of strings and convert them
Trying to setup an SSH server on Windows Server 2003. What are some good
Trying to get my css / C# functions to look like this: body {

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.