[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

array question

Posted on 2003-12-01
1
Medium Priority
?
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

649 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