Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function format in a C program

Tags:

c

function

I'm writing some functions that manipulate strings in C and return extracts from the string.

What are your thoughts on good styles for returning values from the functions.

Referring to Steve McConnell's Code Complete (section 5.8 in 1993 edition) he suggests I use the following format:

void my_function ( char *p_in_string, char *p_out_string, int *status )

The alternatives I'm considering are:

Return the result of the function (option 2) using:

char* my_function ( char *p_in_string, int *status )

Return the status of the function (option 3) using:

int my_function ( char *p_in_string, char *p_out_string )

In option 2 above I would be returning the address of a local variable from my_function but my calling function would be using the value immediately so I consider this to be OK and assume the memory location has not been reused (correct me on this if I'm wrong).

Is this down to personal style and preference or should I be considering other issues ?

like image 897
David Avatar asked Aug 17 '09 11:08

David


People also ask

What is the format of C program?

A C program is divided into six sections: Documentation, Link, Definition, Global Declaration, Main() Function, and Subprograms. While the main section is compulsory, the rest are optional in the structure of the C program.

What is function in C with example?

There are two types of functions in C programming: Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc. User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times.


2 Answers

Option 3 is pretty much the unspoken(?) industry standard. If a IO-based C function that returns an integer, returns a non-zero integer value, it almost always means that the IO operation failed. You might want to refer to this Wikibook's section on return values in C/C++.

The reason that people use 0 for success is because there is only one condition of success. Then if it returns non-zero, you look up somehow what the non-zero value means in terms of errors. Perhaps a 1 means it couldn't allocate memory, 2 means the argument was invalid, 3 means there was some kind of IO error, for instance. Technically, typically you wouldn't return 1, but you'd return XXX_ERR_COULD_NOT_MALLOC or something like that.

Also, never return addresses of local variables. Unless you personally malloced it, there are no guarantees about that variable's address after you return from the function. Read the link for more info.

like image 124
Mark Rushakoff Avatar answered Sep 22 '22 02:09

Mark Rushakoff


In option 2 above I would be returning the address of a local variable from my_function but my calling function would be using the value immediately so I consider this to be OK and assume the memory location has not been reused (correct me on this if I'm wrong).

I'm sorry but you're wrong, go with Steve McConnell's method, or the last method (by the way on the first method, "int status" should be "int* status".

You're forgiven for thinking you'd be right, and it could work for the first 99,999 times you run the program, but the 100,000th time is the kicker. In a multi-threaded or even on multi process architecture you can't rely that someone or something hasn't taken that segment of memory and used it before you get to it.

Better to be safe than sorry.

like image 27
Binary Worrier Avatar answered Sep 23 '22 02:09

Binary Worrier