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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:36:52+00:00 2026-06-08T12:36:52+00:00

I am trying to use a pointer to an array inside of a for

  • 0

I am trying to use a pointer to an array inside of a for each loop in C++. The code below won’t work because the “for each statement cannot operate on variables of type ‘int *'”. I’d prefer to use the new operator so that the array is on the heap and not the stack, but I just can’t seem to figure out the syntax here. Any suggestions?

#include <iostream>
using namespace std;

int main() {
    int total = 0;

    int* array = new int[6];
    array[0] = 10; array[1] = 20; array[2] = 30;
    array[3] = 40; array[4] = 50; array[5] = 60;

    for each(int i in array) {
        total += i;
    }

    cout << total << endl;
}
  • 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-06-08T12:36:53+00:00Added an answer on June 8, 2026 at 12:36 pm

    That for each thing you are using is a Visual C++ extension that’s not even recommended by some microsoft employees (I know I’ve heard STL say bad things about it, I can’t remember where).

    There are other options, like std::for_each, and range-based for from C++11 (though I don’t think Visual C++ supports that yet). However, that’s not what you should be using here. You should be using std::accumulate, because this is the job that it was made for:

    total = std::accumulate(array, array + 6, 0);
    

    If you’re really just interested in how to use this Microsoft for each construct, well, I’m pretty sure you can’t if you just have a pointer. You should use a std::vector instead. You should be doing that anyway.

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

Sidebar

Related Questions

I'm learning to use pointer to copy char array. I have the following code
i am trying to create a 2d array using double pointer... my code is...
I was just trying to use a void pointer to an integer array ,I
I am trying to use a function pointer and I am getting this error:
I have a couple UIViewControllers that I am trying to access an array inside
I am trying to use a double void pointer but I am a little
I am trying to use qsort from STL to sort array of edge: struct
I'm trying to use D's equivalent of function pointer as a way of specifying
I have been trying to use 2D array as a parameter to a constructor
I am trying to use an array of objects but when I use the

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.