473,587 Members | 2,509 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

allocate an array

Hello everybody,

I've got a problem. I'm trying to allocate an array inside a function with
Parameter int n:
main(){
int i;
....
i = strlen(ch);
array(i);
....
}
int array ( int n ){
int a[n];
....
}
but when i compile:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'a' : unknown size
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
how can i do now?
Thank u in advance for your afforts.
Nov 13 '05 #1
3 18753
Simon wrote:
Hello everybody,
int array ( int n ){
int a[n];
...
}
but when i compile:

error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0
error C2133: 'a' : unknown size
error C2057: expected constant expression
error C2466: cannot allocate an array of constant size 0


Perhaps because your compiler does not accept C99 ?
To my knowledge, this construction is in C99, but not
in C89.
Change your compiler, or use "malloc".

Marc Boyer
--
Lying for having sex or lying for making war? Trust US presidents :-(
Nov 13 '05 #2
Simon wrote:
I've got a problem. I'm trying to allocate an array inside a function with
Parameter int n:
main(){
int i;
...
i = strlen(ch);
array(i);
...
}
int array ( int n ){
int a[n];
...
}


You can't do that in (pre-99) C. Instead of allocating an array,
allocate some heap space (and remember to free it), eg:

int *a = malloc( n * sizeof (*a) );

Remember to #include <stdlib.h>.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #3
thanks a lot.

i got it

"Chris Dollin" <ke**@hpl.hp.co m> schrieb im Newsbeitrag
news:be******** **@murdoch.hpl. hp.com...
Simon wrote:
I've got a problem. I'm trying to allocate an array inside a function with Parameter int n:
main(){
int i;
...
i = strlen(ch);
array(i);
...
}
int array ( int n ){
int a[n];
...
}


You can't do that in (pre-99) C. Instead of allocating an array,
allocate some heap space (and remember to free it), eg:

int *a = malloc( n * sizeof (*a) );

Remember to #include <stdlib.h>.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3261
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before opening to the file. I fail to write a function that allocates memory for a 2d array and returns it to main. I was trying to pass a pointer to the array back to main, but main cannot access the data. The simplified code (no opening of file, but...
7
7009
by: bijax | last post by:
hi, i am new to multidimensional array of c programming .pls help me to solve out this how to allocate memory for mutidimensional array? i.e a ;
4
4992
by: marora | last post by:
I have created class definition which contains a charater pointer as one of it's data memeber. The objective is to read some data from a file, and assign it to a data member; Size of data is not known in the begining. We can assume that it will not exceed 256; class definition:
2
3436
by: xhunga | last post by:
I have try a new version of my work. I have put the sizes of the matrix into the matrix. A = number of rows A = number of columns The first element of the matrix is A instead of A. You can not use the row 0, and the column 0.
3
2674
by: valerio | last post by:
Hello all I would like to dinamically allocate an array of array of structures. To explain this: struct file{ char* fileName,int inode) myfiles; struct file{ char* fileName,int inode) mydirs; but I would like to do it dinamically with linked list. I am able to do it for myfiles, but not with mydirs. Pseudo-code is ok.
1
3399
by: trevor.farchild | last post by:
Hi, long time reader, first time poster I have an application that will be doing this 5 times a second: Get a bunch of data from a NetworkStream and convert it into a Bitmap Therefore, the process looks like: Read the NetworkStream to find out size of the data Allocate that amount as a byte array Read the NetworkStream into the byte array Put the byte array into a MemoryStream
13
27408
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
3
6895
by: Samant.Trupti | last post by:
HI, I want to dynamically allocate array variable of type LPWSTR. Code looks like this... main() { LPWSTR *wstr; int count = Foo (wstr); for (int i = 0; i < count; i++) //print each element;
28
7120
by: Trups | last post by:
HI, I want to dynamically allocate array variable of type LPWSTR. Code looks like this... main() { LPWSTR *wstr; int count = Foo (wstr); for (int i = 0; i < count; i++)
0
7923
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7974
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8221
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3845
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.