Solved

array question

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

Industry Leaders: 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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

724 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