View previous topic :: View next topic |
Author |
Message |
HappyByte Just Arrived
Joined: 24 Jul 2009 Posts: 0
|
Posted: Wed Jul 29, 2009 10:10 pm Post subject: How to send a multi-dimensional array to a function |
|
|
Hello I am working on a C program. I am writing a C program and one of the functions that I have in the program, I am submitting an array to it. How do you do that? The compiler that I am using is not accepting it.
|
|
Back to top |
|
|
Groovicus Trusted SF Member
Joined: 19 May 2004 Posts: 9 Location: Centerville, South Dakota
|
Posted: Wed Jul 29, 2009 10:48 pm Post subject: |
|
|
Quote: |
The compiler that I am using is not accepting it. |
What does that mean? Is the compiler saying "No way man, I'm not doing that"?
|
|
Back to top |
|
|
HappyByte Just Arrived
Joined: 24 Jul 2009 Posts: 0
|
Posted: Fri Jul 31, 2009 5:20 am Post subject: |
|
|
I am taking an online course at O'Reillys. When I compile my application, this error is what I get:
Compiling Your Program
Here are the results of compiling your Program Cfiles2/final2.c:
# /users/pbarnard/Cfiles2/final2.c: In function `int main()':
# /users/pbarnard/Cfiles2/final2.c:49: parse error before `,'
# /users/pbarnard/Cfiles2/final2.c: In function `int victory_condition(int, int)':
# /users/pbarnard/Cfiles2/final2.c:103: invalid types `int[int]' for array subscript
# /users/pbarnard/Cfiles2/final2.c:109: invalid types `int[int]' for array subscript
# /users/pbarnard/Cfiles2/final2.c:109: parse error before `{'
# /users/pbarnard/Cfiles2/final2.c:119: nondigits in number and not hexadecimal
# /users/pbarnard/Cfiles2/final2.c:119: nondigits in number and not hexadecimal
# /users/pbarnard/Cfiles2/final2.c:119: non-lvalue in assignment
# /users/pbarnard/Cfiles2/final2.c:120: nondigits in number and not hexadecimal
# /users/pbarnard/Cfiles2/final2.c:120: nondigits in number and not hexadecimal
# /users/pbarnard/Cfiles2/final2.c:120: non-lvalue in assignment
# /users/pbarnard/Cfiles2/final2.c:126: invalid types `int[int]' for array subscript
# /users/pbarnard/Cfiles2/final2.c:133: invalid types `int[int]' for array subscript
# /users/pbarnard/Cfiles2/final2.c:144: nondigits in number and not hexadecimal
# /users/pbarnard/Cfiles2/final2.c:144: nondigits in number and not hexadecimal
# /users/pbarnard/Cfiles2/final2.c:144: non-lvalue in assignment
# /users/pbarnard/Cfiles2/final2.c:145: nondigits in number and not hexadecimal
# /users/pbarnard/Cfiles2/final2.c:145: nondigits in number and not hexadecimal
# /users/pbarnard/Cfiles2/final2.c:145: non-lvalue in assignment
# /users/pbarnard/Cfiles2/final2.c:150: invalid types `int[int]' for array subscript
# /users/pbarnard/Cfiles2/final2.c:157: invalid types `int[int]' for array subscript
# /users/pbarnard/Cfiles2/final2.c:166: nondigits in number and not hexadecimal
# /users/pbarnard/Cfiles2/final2.c:166: nondigits in number and not hexadecimal
# /users/pbarnard/Cfiles2/final2.c:166: non-lvalue in assignment
# /users/pbarnard/Cfiles2/final2.c:167: nondigits in number and not hexadecimal
# /users/pbarnard/Cfiles2/final2.c:167: nondigits in number and not hexadecimal
# /users/pbarnard/Cfiles2/final2.c:167: non-lvalue in assignment
# /users/pbarnard/Cfiles2/final2.c:172: invalid types `int[int]' for array subscript
# /users/pbarnard/Cfiles2/final2.c:182: nondigits in number and not hexadecimal
# /users/pbarnard/Cfiles2/final2.c:182: nondigits in number and not hexadecimal
# /users/pbarnard/Cfiles2/final2.c:182: non-lvalue in assignment
# /users/pbarnard/Cfiles2/final2.c:183: nondigits in number and not hexadecimal
# /users/pbarnard/Cfiles2/final2.c:183: nondigits in number and not hexadecimal
# /users/pbarnard/Cfiles2/final2.c:183: non-lvalue in assignment
# /users/pbarnard/Cfiles2/final2.c:185: confused by earlier errors, bailing out
At line 103 is when the transferred array is used for a comparison test. That is when the errors start.
|
|
Back to top |
|
|
Groovicus Trusted SF Member
Joined: 19 May 2004 Posts: 9 Location: Centerville, South Dakota
|
Posted: Fri Jul 31, 2009 5:50 am Post subject: |
|
|
Um, are you passing two parameters to your function called int and int? I am pretty sure that int is a protected word, and thus can not be used for a parameter name. Therefore int[int] makes no sense. Your function should be something like int victory_condition(my_array, index).
Then when indexing the element of the array, you would do something like this: my_array[index].
|
|
Back to top |
|
|
nailz Just Arrived
Joined: 17 Jul 2009 Posts: 0 Location: Boston
|
Posted: Fri Jul 31, 2009 7:35 am Post subject: |
|
|
it's been a while since I have worked with C but I don't believe int is a protected word(it's possible it is so please consider groovicus. Have you properly defined all of your arrays???
maybe when defining an array you forgot [] which could be screwing up your code...
|
|
Back to top |
|
|
Groovicus Trusted SF Member
Joined: 19 May 2004 Posts: 9 Location: Centerville, South Dakota
|
Posted: Fri Jul 31, 2009 3:12 pm Post subject: |
|
|
http://tigcc.ticalc.org/doc/keywords.html
Yes, it is.
EDIT: Crap, I forgot to mention that there is apparently some sort of syntax error before that. A syntax error will throw everything off, and cause all kinds of inexplicable errors.
|
|
Back to top |
|
|
HappyByte Just Arrived
Joined: 24 Jul 2009 Posts: 0
|
Posted: Sun Aug 02, 2009 9:30 pm Post subject: |
|
|
So an array can be transferred to a function, you have to properly declare it? When I declare the array, I declare it with the data type that it is. Then I put in the array name as "my_array[2,2]". Does the inclusion of the dimensions of the array cause an error or does a multidimensional array need to be declared with a separate pair of brackets for each dimension it has?
|
|
Back to top |
|
|
capi SF Senior Mod
Joined: 21 Sep 2003 Posts: 16777097 Location: Portugal
|
Posted: Mon Aug 03, 2009 12:29 am Post subject: |
|
|
A multidimensional array in C would be declared as int array[2][2].
|
|
Back to top |
|
|
tbsdy1 Just Arrived
Joined: 17 Aug 2009 Posts: 0
|
Posted: Mon Aug 17, 2009 2:06 pm Post subject: |
|
|
Can you post the code you are using?
If you want to pass an array to a function, you need to use the address of operator. In other words, let's say you have a multidimension integer variable, then you would use:
foo(&intVariable);
Just so you know, int is most definitely a reserved word in C.
I'd strongly advise getting a good primer on pointers. The best I've ever read was "C Pointers and Dynamic Memory Management".
|
|
Back to top |
|
|
|