How to send a multi-dimensional array to a function

Networking/Security Forums -> Programming and More

Author: HappyByte PostPosted: 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.

Author: GroovicusLocation: Centerville, South Dakota PostPosted: 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"? Laughing

Author: HappyByte PostPosted: 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.

Author: GroovicusLocation: Centerville, South Dakota PostPosted: 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].

Author: nailzLocation: Boston PostPosted: 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...

Author: GroovicusLocation: Centerville, South Dakota PostPosted: Fri Jul 31, 2009 3:12 pm    Post subject:
    ----
http://tigcc.ticalc.org/doc/keywords.html

Yes, it is. Smile

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.

Author: HappyByte PostPosted: 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?

Author: capiLocation: Portugal PostPosted: Mon Aug 03, 2009 12:29 am    Post subject:
    ----
A multidimensional array in C would be declared as int array[2][2].

Author: tbsdy1 PostPosted: 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".



Networking/Security Forums -> Programming and More


output generated using printer-friendly topic mod, All times are GMT + 2 Hours

Page 1 of 1

Powered by phpBB 2.0.x © 2001 phpBB Group