Ex25.c Pointer syntax

10 int read_string(char **out_string, int max_buffer){
11 *out_string = calloc(1,max_buffer + 1);
12 check_mem(*out_string);
13
14 char *result = fgets(*out_string, max_buffer, stdin);
15 check(result != NULL, “Input error.”);
16
17 return 0;
18
19 error:
20 if (*out_string){free(*out_string);}
21 *out_string = NULL;
22 return -1;
23 }
24
25 int read_int(long *out_int){
26 char *input = NULL;
27 char *end = NULL;
28 int rc = read_string(&input, MAX_DATA);
29 check(rc == 0, “Failed to read number.”);

Hello everyone, I am a little confused about the pointer syntax in ex25.c . I googled quite a bit but cannot find an answer to it (propably because I don’t know how to google this properly).
It shows up in the use of the read_string function.
The signature of the function is:
int read_string(char **out_string, int max_buffer)
all well and good.
But when it is used in line 28 we do not give the function a pointer to a pointer of type char (char **) but the address of a simple pointer. I can’t find any example of this in the earlier exercises. It works perfectly fine but I don’t understand it one bit. I hope someone can clarify this for my. Thank you very much in advance and happy programming!

Yes you do give a pointer to a pointer to char. input is a char*, then &input is a char**. The address of something is equivalent to a pointer to the same thing.

Thank you so much! :slight_smile:

A free service run by Zed A. Shaw for learncodethehardway.org.