View Full Version : C syntax
Dogcow
09-30-2002, 05:43 PM
I need some more help with c... Syntax is killing me.
Problems:
1) Making a method return a 2-d array. The compiler complains when I try float[][] myMethod(....
2) Defining an entire array in a single line of code. I thought you just use myArray ={value 1, value 2, .... value n}; but that doesn't work...
3) Making an element of a 2-d array equal to an element of a 1-d array. The compiler doesn't like myArray[j][i] = otherArray[i];
My code (with the errors) is here (http://theunderground.eon.com.au/temp/errors.gif). These are simple problems that I can't figure out because I haven't used C in ages. Any help would be appreciated.
-Dogcow "moof!"
Visit The Underground (http://theunderground.eon.com.au)
mervTormel
09-30-2002, 07:07 PM
hmm, IIRC, you need to pass and return arrays by reference, i.e., just pass the array name (which is a pointer to the array) and access it's elements in a routine.
and to init an array, i think you just say:
type identifier[][] = { e1, e2, e3, ... eN };
your 4x4 is a 25 element array, right? {0,1,2,3,4} x {0,1,2,3,4}
don't you need to dereference the value of an array element, i.e., *foo[n][m] ?
where's your K&R? mine's over there but i don't wanna go look at it. if i do, i know i'll "waste" five days in C. :D
it'd be good code to paste your code and code tag your code here, directly. code?
N.B. i may be completely out in the weeds here.
nkuvu
10-01-2002, 01:02 PM
You can pass 1D arrays by value (IIRC), but if you go over 1D you need to pass by reference. This will also fix the "cannot convert float* to float(*)[4]" error.
Your result array in the transform function is a 2D array (declared as float result[4][4]). So you can't magically transform that back to a 1D array by just calling result[i].
Your initialization of the array in a single line of code would work fine if the array was a 1D array, but it's not, so you'll have to iterate.
hayne
10-01-2002, 03:35 PM
The C FAQ at http://www.eskimo.com/~scs/C-faq/top.html covers many of these questions about arrays.
scaryfish
10-01-2002, 05:10 PM
You know that in C, arrays and pointers are exactly the same thing. You can access pointers using my_pointer[i] syntax (or my_pointer[i][j] for a 2d array), and in fact arrays are converted to pointers when passed as arguments.
So for 1) you could try float **my_method(...
2) I'm not sure about - we did that a while back, but I can't remember.. :p
3) Don't know about that one.. Can you assign a regular float to it? like my_array[j][i] = float ? Is other_array[i] really a float?
EDIT: Ahh, wait, you're using c++. I thought you were using c. Yeah, so probably my stuff won't help you.
nkuvu
10-02-2002, 12:44 PM
a[] is not the same as a*.
See http://www.eskimo.com/~scs/C-faq/q6.2.html
for details.
I thought they were the same also, before I read the FAQ...
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.