• RSS
  • Twitter
  • FaceBook

Security Forums

Log in

FAQ | Search | Usergroups | Profile | Register | RSS | Posting Guidelines | Recent Posts

How to send a multi-dimensional array to a function

Users browsing this topic:0 Security Fans, 0 Stealth Security Fans
Registered Security Fans: None
Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> Programming and More

View previous topic :: View next topic  
Author Message
HappyByte
Just Arrived
Just Arrived


Joined: 24 Jul 2009
Posts: 0


Offline

PostPosted: Wed Jul 29, 2009 10:10 pm    Post subject: How to send a multi-dimensional array to a function Reply with quote

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
View user's profile Send private message
Groovicus
Trusted SF Member
Trusted SF Member


Joined: 19 May 2004
Posts: 9
Location: Centerville, South Dakota

Offline

PostPosted: Wed Jul 29, 2009 10:48 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Visit poster's website
HappyByte
Just Arrived
Just Arrived


Joined: 24 Jul 2009
Posts: 0


Offline

PostPosted: Fri Jul 31, 2009 5:20 am    Post subject: Reply with quote

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
View user's profile Send private message
Groovicus
Trusted SF Member
Trusted SF Member


Joined: 19 May 2004
Posts: 9
Location: Centerville, South Dakota

Offline

PostPosted: Fri Jul 31, 2009 5:50 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
nailz
Just Arrived
Just Arrived


Joined: 17 Jul 2009
Posts: 0
Location: Boston

Offline

PostPosted: Fri Jul 31, 2009 7:35 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail AIM Address
Groovicus
Trusted SF Member
Trusted SF Member


Joined: 19 May 2004
Posts: 9
Location: Centerville, South Dakota

Offline

PostPosted: Fri Jul 31, 2009 3:12 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website
HappyByte
Just Arrived
Just Arrived


Joined: 24 Jul 2009
Posts: 0


Offline

PostPosted: Sun Aug 02, 2009 9:30 pm    Post subject: Reply with quote

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
View user's profile Send private message
capi
SF Senior Mod
SF Senior Mod


Joined: 21 Sep 2003
Posts: 16777097
Location: Portugal

Offline

PostPosted: Mon Aug 03, 2009 12:29 am    Post subject: Reply with quote

A multidimensional array in C would be declared as int array[2][2].
Back to top
View user's profile Send private message
tbsdy1
Just Arrived
Just Arrived


Joined: 17 Aug 2009
Posts: 0


Offline

PostPosted: Mon Aug 17, 2009 2:06 pm    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Display posts from previous:   

Post new topic   Reply to topic   Printer-friendly version    Networking/Security Forums Index -> Programming and More All times are GMT + 2 Hours
Page 1 of 1


 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Community Area

Log in | Register