473,325 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

define size of array.

36
if i want to let the user to define the array size, how can i do it ?

because if im define arr[n] theres compilation error .
cause n!=const .
(the user enter n )

TNX .....
Aug 14 '09 #1
4 6109
Banfa
9,065 Expert Mod 8TB
You can't let the user define the size of an array (in C89 anyway) but what you can do is let the user enter a size and then allocate (using malloc) data large enough to be the array in question and set a pointer to it and then you can use you pointer pretty much like you'd use the array.

Read the insight Arrays Revealed.
Aug 14 '09 #2
weaknessforcats
9,208 Expert Mod 8TB
In C++ you can set the initial array size managed by a vector at the time the vector is created:

Expand|Select|Wrap|Line Numbers
  1. vector<int>  arr(100);   //100 ints
Of course, the vector code is calling malloc() just like Banfa said to do in C.
Aug 14 '09 #3
donbock
2,426 Expert 2GB
C99 introduced Variable-Length Arrays (VLAs). This feature might help.
Aug 14 '09 #4
sedaw
36
TNX ... , didn`t learn pointers yet but soon i will .
Aug 15 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Birt | last post by:
My understanding about defining your own copy and assignment constructors is whenever there is a member of pointer type. Is this true or is there any exception to this"rule"? How about when you...
20
by: Geoff Cox | last post by:
Hello, This may be the answer to a problem I have - or it may not be possible?! Can I have initial values of variable situation and count defined as var situation = 0; var count = 0;
17
by: Davor | last post by:
How to define global variable in main()? I'm asking because I have an array in main, whose size is determined by input, so the definition has to be in main ( or in some other funcion ). And I need...
7
by: Peng Yu | last post by:
I'm not very clear about when to use #define and when to use const? #define number 4 const int number = 4; If I just want to define a global constant, which way of the above is better? ...
31
by: bilbothebagginsbab5 AT freenet DOT de | last post by:
Hello, hello. So. I've read what I could find on google(groups) for this, also the faq of comp.lang.c. But still I do not understand why there is not standard method to "(...) query the...
7
by: Roman Mashak | last post by:
Hello, All! I wonder is it possible to define an array containing strings, not single characters? What I want is array 'table' that will have N elements, and every element is a strings tailoring...
10
by: Zhou Yan | last post by:
I want to define a pointer to a multiple-subscripted array. For instance, I have an array defined as below( I have reduced the size of the array as well as the dimension, but I think it OK to ask...
1
by: shofu_au | last post by:
Hi Group, I am trying to define a class that has a fixed size array of a structure containing a fixed size array of a structure. I am using System.Runtime.InteropServices and trying to define...
5
by: suresh | last post by:
Hi, How to define a two dimensional array where each row is of type vector<map<string,int>>? My idea is, if "x" is such a variable, x is a vector where each cell of the vector is a...
5
by: maker.rain1 | last post by:
Hello All, I have come across a problem as explained below in a sample. Please help me if anyone has any ideas to solve this. I have a #define as defined below. #define MAX 200 int...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.