The only problem I am having now is that when no extension is found, I want to return a null pointer, but for some stupid reason I am not seeing, I don't know how to make it a null pointer can anyone help me out. The line I am trying to set it to null is highlighted in red.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void getext(const char *path, char *ext);
int main (void)
{
char *path = "foo/bar.baz/blah";
char *ext = malloc(FILENAME_MAX * sizeof(char));
getext(path, ext);
if (ext == NULL)
printf("No extension found\n");
else
printf("Extension: %s\n", ext);
return 0;
}
void getext(const char *path, char *ext)
{
char *pch;
pch = strrchr(path, '/');
if (pch != NULL)
pch = strrchr(pch, '.');
else
pch = strrchr(path, '.');
if (pch == NULL)
*ext = NULL;
else
memcpy(ext, pch + 1, strlen(pch));
}