1

Programming Language: C


I'm dealing with a project that requires to recode certain already made functions, while looking at the man page of some of them to see the original prototype, I found out that some of them contain an asterisk before the name of the function like this one in the string.h library char *strchr(const char *s, int c) What I personally understand from the asterisk is that we add it to the declaration of a variable to make it a pointer, but how comes that a function is a pointer.

miken32
  • 42,008
  • 16
  • 111
  • 154

4 Answers4

8

Asterisk is a part of return type, so this function

char *strchr(const char *s, int c)

Returns char*

Also,

int *c;

is not a "pointer variable" to int, but a variable which stores int pointer (int*)

Why is the asterisk before the variable name, rather than after the type?

1

This means, that the function is returning a pointer, which points to a variable or any structure type or just a value of it, in this case one char variable or first element of an chararray, whose value(s) is/are determined by the function itself (depending on the processing of it). You can use that pointer to either check if the function was done successfully (which is common) or use it elsewhere back in the program according to the specific purpose of the function.

0

A pointer is like any other variable. int and long stores integers. float and double stores floating point numbers. Pointers stores memory addresses. And there's nothing wrong with a function returning an address.

klutt
  • 30,332
  • 17
  • 55
  • 95
0

A function with char* prototype implies that it is returning a pointer of type char. In other words the return value of this function will be an address pointing to a string (or character).

Aditya
  • 47
  • 8