?
Solved

array question

Posted on 2003-12-01
1
Medium Priority
?
239 Views
Last Modified: 2010-04-15
hi all,

Is there any way to declare the size of an array during the execution of the program?i mean not using e.g define 10,but to be prompt to give the array's size(using cin maybe?)?

A piece of code would be great!

thanks in advance guys.
0
Comment
Question by:pmsatsoc
1 Comment
 
LVL 19

Accepted Solution

by:
Dexstar earned 500 total points
ID: 9853857
@pmsatsoc:

> Is there any way to declare the size of an array during the execution of the
> program?i mean not using e.g define 10,but to be prompt to give the array's
> size(using cout maybe?)?

Here's how you do it.
     int nSize;
     scanf( "%d", &nSize );
     int* pInts = (int*)malloc( nSize * sizeof(int) );

     pInts[0] = 1;
     pInts[1] = 2;

Then, when you are done, free up the memory:
     free( pInts );

Hope That Helps,
Dex*
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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

749 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