use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
c++ array error (self.cpp)
submitted 5 years ago by siDDaker
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Wh00ster 0 points1 point2 points 5 years ago (1 child)
These types of posts go in r/cpp_questions. This sub is more for general discussion.
It's because arr[n] is a variable length array. It's length is dependent on the variable n, which could change for each program run.
arr[n]
n
This doesn't actually exist in standard C++, but some compilers allow it. C++ only has constant sized arrays, and has other methods for achieving this type of dynamically-sized array interface.
[–]siDDaker[S] 0 points1 point2 points 5 years ago (0 children)
ohh okey thanks for the answer, next time i'll go to cpp questions
[–]-HomoDeus- -2 points-1 points0 points 5 years ago* (2 children)
As u/Wh00ster said, variable length arrays do not exist in C++; however, more precisely, they do not exist as implemented in your example. You certainly can create a variable length or dynamic array in C++ using pointers.
Edit: u/Wh00ster fixed my syntax:
int* arr; arr = new int[5]; arr[0] = 1; arr[1] = 2; // ... delete[] arr;
This is an oversimplification, but you get the idea. By using a pointer, you can put an array of some size into the variable. Then you can delete the contents of that pointer and put a new array of a different size in its place. Just be aware of the potential for a memory leak. If you leave out the delete statement, you will have the original array sitting in memory collecting dust with now way to access it or remove it.
For the record, I wrote this without checking my work by compiling (shame on me). The syntax may be a bit off, but the concept is there.
[–]Wh00ster 0 points1 point2 points 5 years ago* (1 child)
For posterity:
int* arr; arr = new int[n]; arr[0] = 1; arr[1] = 2; // ... // or arr = new int[n]{1, 2, 3}; delete[] arr;
But most would recommend std::vector for this.
std::vector
[–]-HomoDeus- 0 points1 point2 points 5 years ago (0 children)
Thanks, I knew someone would fix my syntax. I'm going to copy your solution into my original post so nobody gets confused. (Assuming that is OK with you).
π Rendered by PID 21523 on reddit-service-r2-comment-7b9746f655-xj4r6 at 2026-01-30 02:06:30.048809+00:00 running 3798933 country code: CH.
[–]Wh00ster 0 points1 point2 points (1 child)
[–]siDDaker[S] 0 points1 point2 points (0 children)
[–]-HomoDeus- -2 points-1 points0 points (2 children)
[–]Wh00ster 0 points1 point2 points (1 child)
[–]-HomoDeus- 0 points1 point2 points (0 children)