1

Im trying to write a simple function that get name and return it after adding an extension to it. for example, if i have the char pointer to "abcd" the function should return "abcd.as"

I tried to write this function that get char pointer and return a pointer to a new char after adding the extension. But is not working does someone know why?

char* AddFileExtension(char* FileName)
{
    char* FixFileName=NULL;
    char* Extension = ".as";
    strcpy(FixFileName, FileName);
    strcat(FixFileName, Extension);
    return FixFileName;
}
NPE
  • 486,780
  • 108
  • 951
  • 1,012
Yuval
  • 1,721
  • 4
  • 16
  • 15

3 Answers3

1

You have to allocate memory for FixFileName:

char* Extension = ".as";
char* FixFileName = malloc(strlen(FileName) + strlen(Extension) + 1);

Don't forget to free() the memory when you're done with it. For obvious reasons, this has to be done outside the function.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • char* FixFileName = malloc(strlen(FileName) + strlen(Extension) + 1); why you added "+1" in the end of malloc? – Yuval Mar 29 '13 at 09:21
  • 2
    @Yuval: For the NUL terminator. http://en.wikipedia.org/wiki/Null-terminated_string – NPE Mar 29 '13 at 09:22
1

you have to allocate memory for FixFileName and the size of allocate memory should be the lenght of FileName + sizeof(".as")

Note: the sizeof will count the null character of ".sa" string so no need to add 1 for the null charachter of string FixFileName

char* AddFileExtension(char* FileName)
{
#define EXTENSION_AS ".as"    
    char* FixFileName= malloc(strlen(FileName) + sizeof(EXTENSION_AS));

    sprintf(FixFileName, "%s%s", FileName, EXTENSION_AS);
    return FixFileName;
}

do not forget to free the allocated memory when it became useless in your program with free()

MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • You might consider using `strlen(EXTENSION_AS)` instead of `sizeof(EXTENSION_AS)`. They're both equally fast, since they're both evaluated (in this case) at compile time. However, `sizeof(EXTENSION_AS)` will count the NUL byte. In other words, you're allocating an extra byte. – Dietrich Epp Mar 29 '13 at 09:07
  • @DietrichEpp You are right concerning that sizeof count the null charachter. so I will remove the +1 from the expression. – MOHAMED Mar 29 '13 at 09:16
  • @DietrichEpp `strlen()` could not executed in the compile time. it's a run time function and it's not a macro – MOHAMED Mar 29 '13 at 09:21
  • @MohamedKALLEL, I have a question after seeing your code, sizeof(".as") give results 4, because 3+1(NULL).So I define const char * ws = "hello"; sizeof(ws) gives me 4 on x86 system, why it does not give me 4, not is 6 according you answer? – MYMNeo Mar 29 '13 at 09:29
  • @MYMNeo the string `".as"` is defined as a macro in my code and not as a variable. and for your case `ws` is a pointer and the sizeof pointer is 4 for 32-bits systems and 8 for 64-bits systems. you can ask that question and I will try to answer to your question with more details – MOHAMED Mar 29 '13 at 09:37
  • @MohamedKALLEL: Yes, `strlen()` is a function and not a macro, but it **is** evaluated at compile time in circumstances like this. If you can read assembly, you can verify this for yourself -- be sure to enable optimizations. – Dietrich Epp Mar 29 '13 at 09:39
  • @DietrichEpp It's interesting. I did not know that before. Thank you for the info. In fact I checked and I found that strlen of a literal string is evaluated in the compilation phase. but strlen of a pointer pointing to a literal string is not evaluated in the compilation phase. – MOHAMED Mar 29 '13 at 10:05
0
char* AddFileExtension(char* FileName)
{
    char* FixFileName=NULL;
    char* Extension = ".as";
    FixFileName=(char *) malloc(strlen(FileName)+strlen(Extension)+1);
    strcpy(FixFileName, FileName);
    strcat(FixFileName, Extension);
    return FixFileName;
}

Try the above code. You have to free the memory once you are done

dead programmer
  • 4,223
  • 9
  • 46
  • 77