I am new to C and specifically: pointers.
I have the following C-code:
#include <string.h>
#include <stdio.h>
void changeFileName( char *fileName )
{
char new_name[] = "new_name";
char temp[] = "";
printf("... function: changeFileName, BEFORE: fileName = <%s>\n", fileName);
fileName = strcpy(temp, new_name);
printf("... function: changeFileName, AFTER : fileName = <%s>\n", fileName);
}
int main ()
{
char *sourceFileName = "sample";
printf("\nfunction: main, BEFORE: fileName = <%s>\n\n", sourceFileName);
changeFileName( sourceFileName );
printf("\nfunction: main, AFTER : fileName = <%s>\n\n", sourceFileName);
return;
}
and upon running I got the following output:
function: main, BEFORE: fileName = <sample>
... function: changeFileName, BEFORE: fileName = <sample>
... function: changeFileName, AFTER : fileName = <new_name>
function: main, AFTER : fileName = <sample>
I can't figure out why the value of sourceFileName
are not changing from the initial value sample
to what I'm expecting: new_name
.