How do i pass arrays into functions?

Started by
13 comments, last by Draco5869 20 years, 9 months ago
The name of the array is a pointer to the first element such as bUsed[0][0].

#define LEN 20

so

int array[LEN];

is the same as

int * pt = array[0];

So therefore just pass the array name.
Advertisement
oh..that means theres a problem in my mini prog....i forgot their 0 based...o well..it probably wouldnt work anyways
I''m American and damn proud of it.
i decided to re-write my "mini prog" as a function..since its 1130 at night and i got nothin better to do (gf out of town )

once again....includes are a waste of my time

void arrayprint(int x1, int x2, int y1, int y2 int array)

{

x1=0;
//are those two neccessary?? can i put them in the parameters??
y1=0;

while (x1
{

cout<
x1++;

if (x1==x2)

{

x1=0;

y1++;

}

}

}




that one should be better...can someone tell me if i screwed up??? cause the noob here is lazy...and u guys debugging makes a lot more sense than msvs...yup..thanx
I''m American and damn proud of it.
Valderman - Your code only works because the size of an int is usually of the same size as a pointer. Multidimensional arrays really are ''flat'' arrays, not really arrays of arrays.

Assuming sizeof(char*) == 4, you would get the following results.

With char array[6][7];, array[2][3] is at offset 2*7+3 = 17.
With char** array;, array[2][3] is at offset 2*4+3 = 11.

The proper way to pass an array is either type*, which forces you to do the offset computation yourself, or type[size][size] which lets the compiler do it by itself. You can omit the leftmost ''size'' parameter (type[][size]) as it doesn''t influence the offset computation and C doesn''t do bounds-checking.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Sorry then, I didn''t know that.

"For crying out loud, she has fishes coming out of her head on either side. How can you find this hot?!"

"If anyone sees a suspicious, camouflaged factory being carried across the desert, they should report it immediately."

This topic is closed to new replies.

Advertisement