About using arrays as function parameters ( C )

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Display Modes
  05 May 2006
About using arrays as function parameters ( C )

Hi there! I'm having a little problem here understanding one thing. I'll waste no more time explaining it to you.

I need to build a simple program that reads arrays complied with two diferente functions.

The code in C ( not ++ ):

#include <stdio.h>
 #define N 5
 
 void Ler_Vector( int s[N] )
 {
 	int i;
 	for (i=0 ; i<N ; i++)
 	{
 		printf("%da pos: ", i+1);
 		scanf("%d", &s[i]);
 	}
 }
 
 void Visualizar_Vector( int s[N] )
 {
 	 int i;
 	 for (i=0 ; i<N ; i++)
 	 {
 	   printf("V[%d] = %d\n", i, s[i]);
 	 }
 }
 
 int main( )
 {
 	int v[N];
 	Ler_Vector(v);
 	putch('\n');
 	Visualizar_Vector(v);
 	getch();
 }


Now, it works great. I'm happy but not as I could be if I wasn't confused about one thing. When I call for

Ler_Vector(v);


I send the 'v' as the memory adress of the first position. Equivalent to it being '&v[0]'. But what confuses me is that when the 'Ler_Vector' function RECIEVES the adress, shouldn't it recieve in a pointer ( int *s[] in this case )? So that when I point to the value stored in 's[i]' it would get me to the value of 'v[i]' and allow me to manipulate it from a distance with the 's' array.

Well, I'm confused because the program works fine and, well, should it really?

Thanks for the hassle.
Cheers,
Roz
 
  05 May 2006
Array variables are pointers, meaning int s[] is the same as int *s. int *s[] means an array of int pointers.

these expressions both return element number index.
s[index];
*(s+index);
 
  05 May 2006
To be more exact, the name of an array variable is a pointer to its first element. An array is just a block of memory allocated on the stack, as opposed to being allocated on the heap with malloc().

Your function declaration is equivalent to

 void Ler_Vector( int* s )
 {
 	// ...
 }
 


Be careful to understand that declaring the parameter as int s[N] does not place any compile time restrictions on the size of the array. i.e. you could pass an array with less than N elements, the compiler would accept it but your program would likely segfault at compile time (if you're lucky: if you're unlucky you'll corrupt memory somewhere else in your program and end up with a random crash that makes no sense at all).
__________________

You can have your characters photoreal, fast or cheap. Pick two.
 
  05 May 2006
Originally Posted by playmesumch00ns: To be more exact, the name of an array variable is a pointer to its first element. An array is just a block of memory allocated on the stack, as opposed to being allocated on the heap with malloc().

Your function declaration is equivalent to

   void Ler_Vector( int* s )
   {
   	// ...
   }
   


Be careful to understand that declaring the parameter as int s[N] does not place any compile time restrictions on the size of the array. i.e. you could pass an array with less than N elements, the compiler would accept it but your program would likely segfault at compile time (if you're lucky: if you're unlucky you'll corrupt memory somewhere else in your program and end up with a random crash that makes no sense at all).


To continue with that thought, here's what I would do (if possible) to be safe.

Make a second parameter that is the size of the array. You end up with:

   void Ler_Vector( int* _array, unsigned int _size )
   {
   	// ...
   }
   


Oh yeah, "#defines" should be avoided when possible. Code that contains them is tougher to maintain as the program size becomes larger, especially when you start working with multiple .C and .H files.
__________________
Life is too short to work a job you hate.
Opinions presented DO NOT represent those of my employer.
"This place smells like boy!" ~RebeccaK

Last edited by mummey : 05 May 2006 at 01:25 PM.
 
  05 May 2006
Arrays and pointers aren't always the same.

When you are in the same scope or a child scope where the array has been declared, the sizeof operator will tell you the size of the array. However, if it's passed to a function it decays into a pointer and then the sizeof operator will tell you the sizeof the pointer, which is why you can't figure out the length of an array when it's passed to your function.

e.g.

#include <stdio.h>

void func(int[]);

static int s[20];

int main(int argc, char** argv)
{
int m[10];
printf("In main()\n");
printf(" sizeof(m)=%u\n", sizeof(m));
printf(" sizeof(s)=%u\n\n", sizeof(s));
func(m);
}

void func(int m[])
{
int f[5];
printf("In func()\n");
printf(" sizeof(m)=%u\n", sizeof(m));
printf(" sizeof(s)=%u\n", sizeof(s));
printf(" sizeof(f)=%u\n", sizeof(f));
}

On my system it prints this...

C:\Documents and Settings\Bill>gcc -o foo foo.c

C:\Documents and Settings\Bill>foo
In main()
sizeof(m)=40
sizeof(s)=80

In func()
sizeof(m)=4
sizeof(s)=80
sizeof(f)=20

s is a file level variable, so it's size is known to the compiler everywhere in the file (or more appropriately the compilation unit). m is declared inside of main, so the sizeof(m) will tell you the size of an array only inside that scope (the function main in this case). Once you pass it to func() it decays into a pointer and sizeof tells you a different answer. f is really the same as m, just in a different function.

Generally it's not that big of a deal when you're dealing with arrays of primitive types or structs, because you can pass the number of elements into the function so as not to run over the end of the array. It becomes a real pain in the neck however, when you need to deal with arrays of arrays.
 
  05 May 2006
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 05:38 AM.


Powered by vBulletin
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.