-2

what this line will do?

char var[] = {0};

Is this equivalent to the following?

char var[1];
memset(var, 0, NULL); 
Arun
  • 2,247
  • 3
  • 28
  • 51
  • 5
    no, they aren't. the second one doesn't compile. – The Paramagnetic Croissant May 27 '15 at 12:48
  • @TheParamagneticCroissant , The `memset` is useless isn't it? – Spikatrix May 27 '15 at 12:49
  • 2
    @CoolGuy memset's 3rd parameter is the number of bytes to write, and not a pointer. if the `NULL` pointer constant happens to be 0 on OP's platform, **and** the compiler doesn't choke on the implicit pointer-to-integer conversion, then the `memset()` is ineffective and the variable is left uninitialized. – The Paramagnetic Croissant May 27 '15 at 12:51
  • Related: [what is the difference between struct {0} and memset 0](http://stackoverflow.com/questions/14031727/what-is-the-difference-between-struct-0-and-memset-0/14031737#14031737) – myaut May 27 '15 at 12:54
  • 5
    Rolled back to original question. Let's not put our own words into OP's mouth. – The Paramagnetic Croissant May 27 '15 at 12:54
  • 4
    @TheParamagneticCroissant the null pointer constant *must* be `0`, or an ICE that evaluates to `0`, optionally cast to `(void *)`. So this code is either ill-formed, or sets zero bytes. – M.M May 27 '15 at 13:01
  • @MattMcNabb The null pointer constant, when converted to an integral type, may not have the numeric value zero. (in fact, since all pointer-to-integer conversions are implementation defined, it may have any value which, when converted back to a pointer, yields a null pointer.) – The Paramagnetic Croissant May 27 '15 at 13:28
  • 1
    @TheParamagneticCroissant Rephrasing as it was apparently unclear. There are two cases of null pointer constant: (a) an ICE evaluating to `0`, (b) an ICE evaluating to `0` which is then cast to `(void *)`. (6.3.2.3/3). In case (a) it *must* have value zero. Since it is of integral type already, "converted to integral type" makes no change. In case (b), the `memset` is ill-formed because a pointer is not implicitly convertable to `int`. – M.M May 27 '15 at 13:46
  • @MattMcNabb I think we are talking about different issues. Obtaining a null pointer from a null pointer constant does need to be done using an integral constant of value 0. The representation of pointers and pointer-to-integer conversions, however, are implementation-defined. Thus, the value obtained by converting *any* pointer (including the null pointer) to an integer is implementation-defined. Put differently, you muse use `0` (and co.) in a pointer context in order to acquire a null pointer, but `(int)NULL` may not have the numeric value 0. – The Paramagnetic Croissant May 27 '15 at 18:27
  • @MattMcNabb [(see this answer in particular.)](https://stackoverflow.com/questions/15429372/typecasting-null-pointer-in-c) (and yes, I do realize that this is not true when `NULL` is simply an integral constant of value 0 *without* an explicit cast to `void *`. I'm addressing the case when its definition includes a cast.) – The Paramagnetic Croissant May 27 '15 at 18:37

1 Answers1

3

char var[] will define var as a char array where size is decided by the initializer list size.

So char var[] = {0} is equivalent to char var[1] = {0}.

In your later code snippet, var may be uninitialized first (if a local variable, which I believe it is as it is followed by memset) and is then set to all zeros.(assuming you meant memset(var, 0, 1)) Although the observable effect of the both snippets are same, compiler may generate different instructions or compiler may choose to call the memset which may add function call overhead in later snippet.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • 4
    in the second example, `var` is not initialized to zeros. it **could** be **set** to all zeros, **if** OP wrote the correct code for that. – The Paramagnetic Croissant May 27 '15 at 12:52
  • @TheParamagneticCroissant You are right, corrected. – Mohit Jain May 27 '15 at 12:52
  • 2
    Why did you change OP's code for your own purpose? It's strictly No-No here on SO. You should have explained clearly that point in your answer. – Am_I_Helpful May 27 '15 at 12:54
  • What will happen if I assign some value to it. i.e. more than 1 byte? eg: strcpy(var, "some value"); – Arun May 27 '15 at 12:57
  • int main() { char value[] = {0}; double dVal = 234.576; sprintf(value, "%f", dVal); strcat(value, "appending some test here"); ..... ..... } – Arun May 27 '15 at 13:00
  • 1
    @impulse `strcpy(var, "some value")` leads to undefined behaviour as you are overrunning the array size (`1` in this case) – Mohit Jain May 27 '15 at 13:03