strrchr, but...

Started by
14 comments, last by amemorex 22 years, 4 months ago
okay okay, "" was a common mistake when i was typing in the post, nonetheless, was my understanding of how the function works correct?

also, i have a question (i thought i fairly understood pointers, but this totally confuses me now)

why is that

*chrlast = '\0';

truncutates the string "fullpath"? i dont understand why or how that works

Edited by - amemorex on December 14, 2001 8:13:02 PM
Advertisement
The value strrchr returns is a pointer. Which points to the last character in the original string which is equal to the second argument. It does not create a new string, nor changes the original string. However, if you modify the character pointed at by the return value, you are modifying the original string. strcpy detects a ''\0'' as the end of the string to copy.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
ok, i understand what you mean now, but now i'm trying to implement it and im having problems..

basically i want to kill 2 birds with 1 function, like this:

    #include <iostream.h>#include <string.h>void returninfo(char *file, char **fname){        char *tmp = strrchr(file, '/')+1);	strcpy(*fname, tmp);	*tmp = '\0';	(*fname)++;}void main() {	char fullpath[256];	char *filename;	strcpy(fullpath, "c:/windows/image.bmp");	getfile(fullpath, &filename);	cout << fullpath << endl; //should be c:/windows/	cout << filename << endl; //should be image.bmp}    


i realize i am doing something wrong here and getting things mixed up, can you guys tell me what to do to this to achieve my goal? i basically want the returninfo function to set fullpath to the path, and filename to the filename

Edited by - amemorex on December 14, 2001 9:07:57 PM
hello?..
hi amemorex, how are you?
strcpy() can only copy strings onto properly allocated memory. You are confusing it with strdup(). And if you use strdup() remember to free() your memory at some point.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement