This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
char a[] = "abc123"; | |
char *b = strrchr(a, '1'); | |
printf("%s\n", b); // 123 | |
printf("%d\n", b); // pointer to '1', in my machine is 229315 | |
printf("%d\n", a);// pointer to 'a', in my machine is 229312 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <errno.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <unistd.h> | |
int main(int argc, char* argv[]) | |
{ | |
int rval; | |
char link_target[1024]; | |
char* last_slash; | |
size_t result_length; | |
char* result; | |
/* Read the target of the symbolic link /proc/self/exe. */ | |
rval = readlink ("/proc/self/exe", link_target, sizeof (link_target)); | |
printf("%s\n", link_target); | |
if (rval == -1) | |
/* The call to readlink failed, so bail. */ | |
abort (); | |
else | |
link_target[rval] = '\0';/* NUL-terminate the target. */ | |
/* We want to trim the name of the executable file, to obtain the | |
directory that contains it. Find the rightmost slash. */ | |
last_slash = strrchr (link_target, '/'); | |
if (last_slash == NULL || last_slash == link_target) | |
/* Something strange is going on. */ | |
abort (); | |
/* Allocate a buffer to hold the resulting path. */ | |
result_length = last_slash - link_target ; | |
result = (char*) malloc (result_length + 1); | |
/* Copy the result. */ | |
strncpy (result, link_target, result_length); | |
result[result_length] = '\0'; | |
printf("%s\n", result); | |
return 0; | |
} |
Ref:
1. http://www.cplusplus.com/reference/cstring/strrchr/
2. Advanced Linux Programming by Mark Mitchell, Jeffrey Oldham, and Alex Samuel www.newriders.com