Click to See Complete Forum and Search --> : a couple questions about c


3m00
01-21-2003, 11:19 PM
K, here they go.

1. Whats the difference between void'ing and int'ing subroutines? (or, can you call subroutines in another way?)
2. Can I pass argc and argv variables to subroutines? (if not, how can I pass strings or serv_addr's to them?)
3. If I have a common .h file, what should they have that my other files don't? (special header files? stdio.h?)

truls
01-22-2003, 10:12 AM
1. Not exactly sure what your question is here. Are you talking about the return value of subroutines ( or "functions" are we geeks like to call them :p )?

2. Yes.
Your function would have to have the same signature as main, but you can easily pass them on.

void myFunction( int number, char **array)
{
int i;
for( i=0 ; i<number ; i++ ) {
printf( "line %d = %s\n", number, array[i] );
}
}

int main( int argc, char **argv )
{
myFunction( argc, argv );
return 0;
}

3. I'd avoid this. It's informative to see the individual headers each file uses. Also, if you have divided your classes/sourcefiles into logical entities the number of common include headers won't be all that large.

3m00
01-22-2003, 10:33 PM
Okie then, but can I void subroutines with argc and argv? Sorta like:
#include <Stdio.h>
void cool_function(int argc, char * argv[]){
printf("yahey - you gave %d arguments!\n",argc);}
int main(){
cool_function(yahey sup cool function);
}
Will it function like the real argc and argv?

truls
01-23-2003, 10:06 AM
No, you need to take in the arguments in main in order to pass them on to cool_func. See my first example of how you have to do it. If you define main as main() there is no way you can get hold of argc and argv.

I'm still confused as to what "void a function" means. Do you mean to call a function which returns void?

3m00
01-23-2003, 08:33 PM
Originally posted by truls
No, you need to take in the arguments in main in order to pass them on to cool_func. See my first example of how you have to do it. If you define main as main() there is no way you can get hold of argc and argv.

I'm still confused as to what "void a function" means. Do you mean to call a function which returns void?
Yeah, thats what I mean. (Because you "int main" so I thought you could "void a function".... ) Anyways, thanks. I didn't really think I could... but if so... it would have been sooo cool... :) \me hopes I can pass structs to functions...

Spawn913
01-23-2003, 09:52 PM
you can always pass struct pointers to functions.

that way you can still use the struct.