Solved

array question

Posted on 2003-12-01
1
232 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 You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

747 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

13 Experts available now in Live!

Get 1:1 Help Now