Author |
Message |
unoho
|
Posted: Tue Oct 05, 2010 7:45 pm Post subject: array inside a function |
|
|
hey guys, im having problem in the following code
code: |
int function(int array[], int n){
int b[n];
}
|
everytime i try that, the IDE gives me an error sometime about c89 ISO etc. etc. anyone knows what that means? but when i change that n to some number, it works fine. any ideas/comments is much appreciated. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
TheGuardian001
|
Posted: Tue Oct 05, 2010 8:05 pm Post subject: Re: array inside a function |
|
|
What IDE/Compiler are you using?
I have no problems with similar code using gcc.
Oh, and what specific error is it giving you? C89 is a rule set (and a fairly old one at that,) not an error. |
|
|
|
|
|
unoho
|
Posted: Tue Oct 05, 2010 8:59 pm Post subject: Re: array inside a function |
|
|
it says
- ISO C89 forbids variable-size array 'a' "
- ISO C89 forbids mixed declarations and code
- unsuccessful build
i tried it on Quicny 2005 and DevC++ and both r same.
so from what i can understand, u can't put a variable as an array size?
EDIT: OMG this is soo stupid, it works fine when i try to do it in command prompt using gcc 3.4.5 but it wont work with the IDEs |
|
|
|
|
|
TheGuardian001
|
Posted: Tue Oct 05, 2010 9:18 pm Post subject: Re: array inside a function |
|
|
From what I understand, it depends on what version of the C standard your compiler uses.
C89 doesn't allow it, C90 doesn't allow it, however C99 does. Since your compiler uses C89, you won't be able to do this without switching/updating your compiler.
Edit: If your IDE uses GCC (MinGW on windows,) you can force it to use C99 by adding the -std=c99 (case sensitive, c99 not C99,) so long as your version of it supports the c99 standard. |
|
|
|
|
|
unoho
|
Posted: Tue Oct 05, 2010 9:56 pm Post subject: RE:array inside a function |
|
|
thank you soo much. by default, the IDE had the C99 option unchecked for c programs. but ya..had to spent 4 hours trying to fix this problem :S
but at least i know how to use command promt to get to running gcc:D |
|
|
|
|
|
coolgod
|
Posted: Sat Oct 09, 2010 12:22 am Post subject: Re: array inside a function |
|
|
how is the compiler suppose to allocate memory for the program if it doesn't know how much space it need?
this isn't a dynamic array..... |
|
|
|
|
|
TheGuardian001
|
Posted: Sat Oct 09, 2010 12:42 am Post subject: Re: array inside a function |
|
|
coolgod @ Sat Oct 09, 2010 12:22 am wrote: how is the compiler suppose to allocate memory for the program if it doesn't know how much space it need?
this isn't a dynamic array.....
This isn't an attempt to make a dynamic array. The purpose of this is to create an array of size x (useful in a function, if it needs a temporary array of size that will be determined when the function is called.) So long as x has a value when the array is created, the compiler knows exactly how much space to allocate (x*sizeof(type).)
IE:
code: |
int x = 4;
int y[x]; //same as int y[4];
|
x resolves to 4, thus this array has 4 elements, and always will.
This is not a problem for most modern compilers. Any compiler that conforms to the C99 specifications (however not one that conforms to C89/ANSI C) can do this. |
|
|
|
|
|
DtY
|
Posted: Sat Oct 09, 2010 3:08 pm Post subject: Re: array inside a function |
|
|
coolgod @ Sat Oct 09, 2010 12:22 am wrote: how is the compiler suppose to allocate memory for the program if it doesn't know how much space it need?
this isn't a dynamic array..... Which is why there was a problem. Nondymanic arrays with a size computed at runtime wasn't a standard feature of C until C99 (and many people still think it's nonstandard). |
|
|
|
|
|
Sponsor Sponsor
|
|
|
ncvitak
|
Posted: Sun Jun 09, 2013 6:16 pm Post subject: Re: array inside a function |
|
|
In C to use an array as a function parameter you must pass it as a pointer.
Example:
c: | #include <stdio.h>
int a [5] = {0, 1, 2, 3, 4};
int foo (int* array, int n ){
return array [n ];
}
int main () {
printf("%d", foo (a, 2)); //prints "2"
return 0;
}
|
|
|
|
|
|
|
Nathan4102
|
Posted: Sun Jun 09, 2013 6:25 pm Post subject: RE:array inside a function |
|
|
Please don't gravedig old posts. This thread has been dead for almost 3 years now. |
|
|
|
|
|
ncvitak
|
Posted: Sun Jun 09, 2013 6:27 pm Post subject: RE:array inside a function |
|
|
Maybe he still wants help |
|
|
|
|
|
Raknarg
|
Posted: Sun Jun 09, 2013 6:43 pm Post subject: RE:array inside a function |
|
|
Necroposting is ok as long as the poster has some new content, I believe. Posts such as "wow thats cool" or "thanks" are frowned upon |
|
|
|
|
|
|