r/cprogramming • u/SubstantialCase3062 • Jan 17 '26
What is char *somefunc(){}
A func can be a pointer
0
Upvotes
r/cprogramming • u/SubstantialCase3062 • Jan 17 '26
A func can be a pointer
25
u/omgmajk Jan 17 '26 edited Jan 17 '26
It's the return type, in this case.
The function takes no arguments and returns a
char *.8
u/darkNergy Jan 17 '26
It's a function that returns a pointer to a char
2
u/Madivga Jan 17 '26
It means the function returns a pointer to some address
2
u/tangerinelion Jan 17 '26
char *somefunc(){}is the same aschar* somefunc(){}2
u/HashDefTrueFalse Jan 17 '26 edited Jan 17 '26
somefunc is the name of a function that takes an unspecified number of parameters of unspecified types and returns a pointer to a char.
Function names evaluate to pointers to the function in most use cases. So yes, a function can be a pointer in that sense. This is just a definition though.
1
u/HugoNikanor Jan 17 '26
In C, declarations are written to mimic usage. In your example,
char *somefunc();declaressomething, which if evaluated as*somefunc()would yield a char.1
u/EatingSolidBricks Jan 17 '26
Function that returns garbage form ax registere or dosent compile depending on the standart
1
u/DawnOnTheEdge Jan 17 '26
In C23, a function declared as taking no arguments and returning
char*, which doesn’t compile because it doesn’t return any value.In older dialects, a function that accepts any number of arguments with no type-checking, compiles, but causes unpredictable bugs if it gets called.
1
u/konacurrents Jan 18 '26
I have a question about code example: obviously there is no valid reply since no return statement. - but there will be a reply. The compiler allocated space for the return - which isn’t initialized. So the result is garbage.
But if the caller didn’t care about result - and the function made some good side effects — who cares😊
You gotta love C.
1
u/sol_hsa Jan 18 '26
cdecl.org can be helpful in deciphering c declarations.
1
u/Paul_Pedant Jan 18 '26 edited Jan 18 '26
It does nothing, so IRL it represents a function that has not yet been written, but allows the code to link so you can test other stuff.
To fix the compile problem, add
{ return (NULL); }To remind the developer to write the code, have it log any call to itself via stderr, and a suitable comment about TBD.
A function can just be a forward declaration, but in that case it cannot have a body (even an empty one). And it really ought to have its args declared, so that any dormant callers get checked.
1
u/Mobile-Major-1837 Jan 19 '26
What you have written is a pointer to somefunc() that returns a char, a single char.
Or, do you wish for char* somefunc(), a function somefunc() that returns a pointer to a char, which might be an array of char?
1
u/Spare-Plum Jan 20 '26
char *somefunc(){} returns is a function that returns a pointer, same as a function returning char*
However there is a way to use a function as a pointer itself. This can be done with char (*somefunc)()
It's helpful to think of C as a classic turing machine. A function pointer is quite literally pointing to a position in memory that contains binary, and this binary can be used just like a normal tape. If you wanted to, you can use an int array as a function pointer, insert code into it, and execute it. You could even modify it in real time like a tape if you wanted to
0
u/[deleted] Jan 17 '26
[deleted] — view deleted comment
1
u/[deleted] Jan 17 '26
[removed] — view removed comment