473,473 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

can user declare the size of array?

1 New Member
how can, an array have the size specified by the user?
Aug 28 '13 #1
3 4024
Banfa
9,065 Recognized Expert Moderator Expert
If you are using C++ then use std::vector declared after the user has input the size.

If you are using C99 then just declare the array after you have got the input from the user using a input variable for its size.

If you are using C89 then use malloc to allocate the array after the user has input the required size.
Aug 28 '13 #2
hosting charges
10 New Member
Expand|Select|Wrap|Line Numbers
  1. int *arr;
  2. //get size from user
  3. arr = (int*)mallc(size*sizeof(int));
  4.  
Aug 28 '13 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
OK, but malloc just allocates memory and says nothing about the size of an array. The "arrayness" is in the pointer which may be typecast in varous ways.

Please read: http://bytes.com/topic/c/insights/77...rrays-revealed

and look at the last example.

You may also allocate arrays in C++ by using the new operator.
Aug 28 '13 #4

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

Similar topics

4
by: Majid Mohammadian | last post by:
Hi All, I want declare an array of int in SQL Server please help me to convert the following code from VB to TSQL ------------ Dim md_mon(12) As Integer md_mon(1) = 31 ------------- Thanks
2
by: nospam | last post by:
Hello! I can pass a "pointer to a double" to a function that accepts double*, like this: int func(double* var) { *var=1.0; ... }
5
by: Ingo Brueckl | last post by:
I need to declare a fixed array of (already defined and working) sort functions (sortfunc1, sortfunc2, sortfunc3) to pass one element of the array to qsort itself: int index; ??? func =...
3
by: Himmat Solanki via .NET 247 | last post by:
How can we declare an array that have not afix length? and can it possible to inserts new itmes to it? as much as we want to enter. -------------------------------- From: Himmat Solanki ...
3
by: lovecreatesbeauty | last post by:
It is strange. Happen to get following the fragment compiled successfully. Is there anything wrong with my compiler or my mind? Please guide me to correct my understanding on it. Sincerely, ...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
5
by: frankleggett | last post by:
Hi Please excuse my ignorance. I am trying to declare an array with a variable amount but I'm having no luck. this is what I've tried Dim Number as string X = Val(Number) dim Array(X) as...
6
by: pallavi27 | last post by:
HI, i want to declare an array that should accept any size given by the user.how to declare such array?please explain me with the help of a code..it should display on the screen "enter the no of...
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...
8
by: Gary | last post by:
When you declare an array of chars and store a string in it, where is the position of the null character \0? And what happens to the unused memory locations? #include <stdio.h> int main(void)...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.