So my goal is to get the name of the directory by itself.
But when I print it at the end, I get the name + some garbage.
I don't understand why, since I am malloc'ing the right amount of characters...
Code:
int main () {
char address[] ="./testrun/forfun";
char *dirName;
char *pch;
int i = 0, lastslash = 0;
pch = strrchr(address, '/');
lastslash = pch - address + 1;
pch = NULL;
dirName = (char *)malloc(sizeof(char) * (strlen(address) - lastslash));
while (address[lastslash] != '\0'){
dirName[i] = address[lastslash];
i++;
lastslash++;
}
printf ("%s\n", dirName);
return 0;
}
Would somebody help me?
I have tried doing realloc on dirName inside the while and all, but it doesn't like that either...