I keep getting EXC_BAD_ACCESS on the
line inside the while loop.
while (*filename != '.')
{
*(ext++) = *(--filename);
}
above you are treating 'ext' as a pointer, however 'ext' is declared as an array
if you want to use a pointer, then declare another pointer and point it to 'ext'
while (*filename) {
filename++;
}
in the above while loop you are moving the pointer 'filename' until it lands on '\0'
that's ok but instead you could start from the end of filename by position yourself on the last '.' like this:
char *p = filename + strlen( filename ) - 1; // last char
then move forward
while (*p != '.') --p;
now you p is where the '.' is
now copy from p + 1
strcpy( ext, p + 1 );
when you return you cannot return 'ext' because it doesn't exist outside the function body.
a way to do it is to either pass ext as an extra argument to the function where you allocate the 'ext' outside the function or use the heap allocate space for the extension
char *getFileExt(char *filename,char *ext)
or even better
char *getFileExt(char *filename,char *ext, size_t maxlen_ext)
or
char *getFileExt(char *filename)
{
char* ext = malloc( 10 );
...