[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to create a user defined varible length array in ANSI C++?

Posted on 2006-06-28
3
Medium Priority
?
372 Views
Last Modified: 2010-04-01
So,  this should be a real easy answer for a programming guru... worth 500 points.

How do you code a user defined varible length array in C++?

Please provide me with a code example...

(I'm not a programmer.)

Feel free to ask questions if I'm being unclear!

Thanks all!

Gerhard
0
Comment
Question by:gerhardub
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
wayside earned 2000 total points
ID: 17002128
1)

int length = 25; // get length from user somehow
int *pArray = new int[length];
for (int i=0; i<length; i++) {
   pArray[i] = i; // do something here
}

2) use the STL vector class, it handles all the size issues.

0
 
LVL 14

Expert Comment

by:wayside
ID: 17002524
Don't forget to

delete [] pArray;

when you are done with it or you create a memory leak.
0
 
LVL 30

Expert Comment

by:Axter
ID: 17002700
Hi gerhardub,
FYI:
If you create the array with an empty bracket (), the array gets initialized to zero.
Example:
int *pArray = new int[length]();


David Maisonave (Axter)
Cheers!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question