Solved

array question

Posted on 2003-12-01
1
233 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 125 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

Why won’t your email signature format correctly?

Struggling to get your corporate email signatures to format correctly? Does the logo keep resizing? Is the text appearing too big? What can you do to prevent this? Find out how you can save your signatures today.

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now