I am confused about functions that return pointers and was woundering if someone can help clerify..
I have not programed anything in C in about 4 months, recently I decided to start a project.
I have a book Teach yourself C in 21 days, which I have read long time ago, and am now useing for refernce.
There are 2 chapters that talk about pointers.
Here is my problem, I was looking for a function that will split a string, simular to split in perl.
After useing google I was able to find a function that someone wrote that will do what I want it to do. The function returns a pointer to a pointer of type char.
Forgeting about that function I will expain what I understand about pointers and what I dont quite understand:
I understand that initialy you must declare a pointer like so
int *ptr;
and then it must be initialized to point to something
int line = 25;
ptr = &line;
where &line is the address in memory.
And then to use the pointer you use the indirection operator * to access the pointed to value
fprintf(stderr,"There was an error on line %d",*ptr);
now as an example of what I dont understand about functions.
if I have a function such as:
char *strchr(char *str, int ch);
which returns a pointer to type char, and I declare some stuff:
char *loc, buf[25];
int ch;
and I want loc to contain the character pointed to;
which of the following is corect and why:
loc = strchr(buf, ch);
---OR---
*loc = strchr(buf, ch);
I know that loc is suposed to be the address in memory of the value pointed to, and *loc is refernceing the actual memory value.
So if strchr returns a pointer to the character it found why are we assigning that to loc whereas we should be assigning that to the valuse pointed to by *loc
strchr does not return the address therefor
loc = strchr(buf,ch);
would be incorect???
I hope I have made it clear what I dont understand and can someone please explain to me how I access the character pointed to by the return value of the function.
