![]() |
|
#1
|
|||
|
|||
How to give size to this array?CPP / C++ / C Code:
I want to create an array by passing the size of that array as an argument to send(). The array must be a static array NOT dynamic. How do i create a static array like this? Last edited by admin II : 29-Apr-2008 at 03:47.
Reason: Please surround your C code with [cpp] your code [/cpp]
|
|
#2
|
|||
|
|||
Re: How to give size to this array?Quote:
A little terminology: By definition, space for an object with static storage duration is provided at compile time (actually it's part of the binary executable file, and storage is allocated at load time). Therefore it is quite impossible to have the size specified by a variable whose value is not known until run time. The so-called C99 language standard provides for variable-length arrays. That is, arrays that can be declared at run time with a dimension that is a variable, so your example might work with some compilers. Two things to note: 1. Those arrays are not static arrays. They can't be, by definition of static. 2. Not all compilers support Variable Length Arrays; in fact many commonly used compilers do not. Recent GNU compilers do (so your example could be compiled with GNU compilers, although you would get a warning telling you that the return type of main() is supposed to be an int). Borland and Microsoft compilers to which I have access do not support Variable Length Arrays. So: What, exactly is the assignment? Regards, Dave |
|
#3
|
|||
|
|||
Re: How to give size to this array?it's a test program. I just need to pass an integer to a method as an argument and that integer will be the size of an array which is defined in that method.
CPP / C++ / C Code:
later i want to assign values to that array to every element in it. Last edited by LuciWiz : 29-Apr-2008 at 07:30.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#4
|
|||
|
|||
Re: How to give size to this array?Quote:
I tried to explain (in the item number '2') that if your compiler supports the C99 standard feature known as Variable Length Arrays, then you can do it. If your compiler does not support Variable Length Arrays, then you can not do it. CPP / C++ / C Code:
Code:
Result of trying to compile with Borland bcc32.exe version 5.82 from Turbo C++ Explorer: Code:
Result of trying to compile with cl.exe from Microsoft Visual C++ express 2008: Code:
Regards, Dave |
|
#5
|
|||
|
|||
Re: How to give size to this array?Thank you.
|
|
#6
|
|||
|
|||
Re: How to give size to this array?CPP / C++ / C Code:
|
|
#7
|
|||
|
|||
Re: How to give size to this array?Quote:
In your first post you specifically said that you are not allowed to use a dynamic array (with a capital NOT). So I guess you are SOL (Somewhat Out of Luck) with that compiler. Regards, Dave |
|
#8
|
|||
|
|||
Re: How to give size to this array?There is a rather sloppy way to do it if you MUST have a static array and you know the maximum value that the variable i will ever be.
For example, suppose we know that i will always be a number between 1 and 100: CPP / C++ / C Code:
|
|
#9
|
|||
|
|||
Re: How to give size to this array?Quote:
A matter of terminology: It's still not static. It goes out of scope when the program returns from the function. However, the Original Poster was later willing to abandon his "static" requirement (I think). If you want any variable (including an array) that is declared inside a function to be static, you have to declare it static. Period. And, as I mentioned previously, you simply can't declare a static array with a variable size (which was his original request) any time, anywhere, in any version of C. 2. The Original Poster specifically said that the size of the array is to be determined by the function parameter. I was really, really hoping that the Original Poster would restate the problem in a way that makes sense. In other words, I don't want to guess what the real requirements are. (Maybe a dynamically-allocated array with a static pointer, so that the array could be allocated upon the first call to the function but would be accessible with unchanged contents for subsequent calls. Something like that. But why should we have to guess?) Regards, Dave |
Recent GIDBlog
Developing GUIs with wxPython (Part 3) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| where is the problem and can you fix it (php) | oggie | MySQL / PHP Forum | 8 | 14-Apr-2008 15:08 |
| Getting a line error in register | oggie | MySQL / PHP Forum | 5 | 13-Apr-2008 16:16 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 19:31 |
| Having a problem | Chuckles | Computer Hardware Forum | 19 | 13-Sep-2004 12:17 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The