2

I fell over this little thing while coding:

char* strinit(char* str) {
    str = (char*) malloc(100);
    strcpy(str, "hello SO");
    return str;
}
int main()
{
    char* str = strinit(str);
    return 0;
}

As you can see, I am using the same variable that I am declaring to initialize it. This is no problem. I tried the same thing in Java. That causes errors.

So my question is: Is there any problems doing this? Can I use it in my code in good conscience?

MyrionSC2
  • 1,248
  • 1
  • 14
  • 24
  • Can you post the errors? Also, am I reading your question correctly: is this a question about Java? – rost0031 May 12 '15 at 08:44
  • @rost0031 This is the error in java: error: variable str might not have been initialized . But the question is not about java. It's about the case in C. – MyrionSC2 May 12 '15 at 08:51
  • @user3121023 You are right, but it doesn't make any difference actually. It still compiles fine, and if i print str after the declaration, I get "hello SO". – MyrionSC2 May 12 '15 at 08:52
  • 2
    you can write like that since str is declared first, then initialized, that said why would want to write like that, the code is harder to read and thus more error prone just to save on a temp variable. – AndersK May 12 '15 at 08:53
  • [Don't cast the return value of `malloc()`!](http://stackoverflow.com/a/605858/3488231) – user12205 May 12 '15 at 08:57
  • You should never do this. Programming like this may work, but is not good programming and may be dependend on the Compiler you are using. –  May 12 '15 at 09:00
  • @CyberSpock It is for a generated C file. strinit is substitute for another function that i use a few places. It basically saves me a few lines of code, so no biggie really. Just wanted to know why it worked. And you told me. Thanks :) – MyrionSC2 May 12 '15 at 09:06

2 Answers2

1

Not everything you can do should be done. The code

char* strinit(char* str) {
    str = (char*) malloc(100);
    strcpy(str, "hello SO");
    return str;
}

uses a parameter only as a local variable. You should change it to

char* strinit(void) {
    char* str = malloc(100);
    strcpy(str, "hello SO");
    return str;
}

and call the function without parameters.

Edit: There is only a little problem with the actual call of your function. The value of the variable str in the main function is passed to the strinit function. This is cheap in your case. But this can be expensive if the parameter type is a more complex type. The compiler will create a copy of the parameter what can call the objects constructor. Of course, the copy of a pointer is cheap.

harper
  • 13,345
  • 8
  • 56
  • 105
  • Strinit is substitute for another function that i use a few places in my code, so I couldn't change the parameter without writing a new function. I just wanted to know if this would cause any problems down the line. I know it looks terrible, but it is for a generated C file, that gets deleted right after anyway, so I'm not too concerned about that. It doesn't seem like it will cause any problems, so thanks :) – MyrionSC2 May 12 '15 at 09:27
  • Function taking no arguments should be `void` in C. Also no need for cast on malloc as mentioned in question comment. And finally, functions starting with `str` should be avoided if possible to prevent possible conflicts with standard C library. – user694733 May 12 '15 at 09:31
1

C & C++ consider that char* str = strinit(str); is legal; because it is evaluated to:

 char* str;
 str = strinit(str);

see Why is 'int i = i;' legal?

Community
  • 1
  • 1
houssam
  • 1,823
  • 15
  • 27