Having this:
#define _DEFAULT_SOURCE 1
#include <stdio.h>
#include <string.h>
int main(){
char *token, org[] = "Cats,Dogs,Mice,,,Dwarves,Elves:High,Elves:Wood";
while((token=strsep(&org,",")))
printf("Token: %s\n",token);
}
gives err (incompatible pointer type):
/usr/include/string.h:439:14: note: expected ‘char ** restrict’ but argument is of type ‘char (*)[47]’
extern char *strsep (char **__restrict __stringp,
I know it is different type (one has memory initialized ->
org[]
, but the function wants pointer without any memory initialized), but they have the same behaviour, so why it complain anyway?And can somone explain me, what is the meaning of this keyword
restrict
or__restrict
in case of*strsep (char **__restrict __stringp,
(on the other hand, I assume the__stringp
is not a internal datatype (because of double underscores) but only a fancy variable name).
Edit:
I think an array is stored in stack, but the strsep
wants a pointer that points to a heap, which could be done with having org
allocated with malloc
and then memcpy
, or even better, copy the string via strdup
(which does internally memcpy
). But anyway, way does strsep
wants pointer that points to heap and not to stack? Both are just pointers, point only to different addresses, but that should not mind.