1

Recently was reading man page of getcwd(3). It writes current working directory to passed buf pointer and also returns it as pointer to char. Could you please explain why it does so(giving result using two ways)?

Bulat M.
  • 638
  • 6
  • 21

4 Answers4

3

Many functions write to a specified argument buffer rather than allocating their own. (This allows the caller to decide how to allocate and manage the buffer.)

However, it is often convenient for a caller to use the result directly, such as when chaining function calls, so these functions sometimes additionally return a pointer to the same buffer that was passed in. For example:

char buf[256] = "foo";
puts(strncat(buf, "bar", sizeof buf - strlen(buf) - 1));
jamesdlin
  • 48,496
  • 10
  • 105
  • 134
  • Why do you use `strncat()`? passing `sizeof buf - 1` is incorrect, it should be `sizeof buf - 1 - strlen(buf)`. `strncat()` semantics are error prone and of course `strncpy()` semantics even worse so. – chqrlie Oct 28 '16 at 13:07
  • @chqrlie Oops, fixed. (I used `strncat` because I consider `strcat`/`strcpy`/`stncpy` to be worse.) – jamesdlin Oct 28 '16 at 17:09
1

One use of this is in function chaining. e.g. consider a function like strcat

char * strcat ( char * destination, const char * source );

This can be invoked as

strcat(dest, source);

However, if you want to concatenate 3 strings into 1, you can use function chaining

dest = strcat(strcat(dest, src1), src2);
Rishikesh Raje
  • 8,410
  • 2
  • 14
  • 30
1

I'm not sure this is the exact answer to this complicated question, but back at the academy my lecturer told us that it exist in order to avoid unexpected program behavior , or unexpected runtime errors.

What exactly do I mean?

For example, let's say you have to copy one string to another using 'strcpy'.
Without going into the deeper details of this function's implementation, as a general case, you should protect your data against null pointers.
Say some error has occurred during the function call, and since C doesn't quite use exceptions, it just returns Null instead.
What will happen if you just ignore the returned null value?
In this case, it will probably result in a runtime error or some unexpected behavior.

Take a look at this code:

char *str1 = ”Hello World”;
char *str2 = calloc(11, sizeof(char));
strcpy(str2, str1); // Error occurs, just part of the string is copied
if(!strcmp(str1,str2))
    printf(”Success”); // Never gets printed - In a more complex example you could't even figure out why

You should always store the result of such functions in a temporary pointer, so that your'e sure no internal error has occured.

Timor Gruber
  • 300
  • 1
  • 4
  • 14
  • Ok, but why some functions also write to passed pointer? In your case you only cover returned value. – Bulat M. Oct 28 '16 at 07:56
  • 1
    @Bulat M. For exactly the same reason - To guard your data. Those functions manage the passed buffers internally, for implementation specific purposes, yet give you indication of their success through the returned value. I think this value could also be an int or a bool, but otherwise you wouldn't be able to chain function calls. – Timor Gruber Oct 28 '16 at 08:12
  • So returned value contains status of the function operation and passed pointer points to the result of function execution? – Bulat M. Oct 28 '16 at 08:21
  • 1
    @Bulat M. Both contain the same value upon successful execution, yet the return value is the only one that can indicate failure upon unsuccessful execution. – Timor Gruber Oct 28 '16 at 08:24
0

when there is a need to return multiple results from a particular function we use pointers. when only one value is to be returned there is no need for pointers. suppose you write a function to get the time of the day. time has hours,minutes and seconds. your function needs to return 3 values, hence you pass the address of some location(or a structure) and the function will fill the address and return it to you.

same works with srtcat and strcmp etc...

Darshan b
  • 157
  • 6
  • 1
    It seems strange that this was the accepted answer when the original question seemed to be about why some functions return *the same thing* multiple ways, something this answer doesn't address at all. – jamesdlin Oct 28 '16 at 07:32
  • same for `strcat` but not for `strcmp`. – chqrlie Oct 28 '16 at 13:10