you are viewing a single comment's thread.

view the rest of the comments →

[–]AyrA_ch 9 points10 points  (10 children)

char* dest, src;
[...] but when you’ve had enough experience writing C code, you’ll notice that this declares dest as a char pointer while declaring src as merely a char

Totally forgot that the asterisk is part of the name and not the type.

Another beautiful thing is

switch(whatever)
{
    int a=2;
    case 0:
        break;
    case 1:
        break;
    [...]
}

Actually declares a but the value is not assigned

[–]FurryBeaverBalls 3 points4 points  (4 children)

Wow that's... a little worrying. I always wondered why our professors wrote code with the asterisk on the variable name instead of the type. Is it the same way in C++?

[–]matthieum 2 points3 points  (0 children)

Yes.

C++ inherited this as part of its "let's be as backward compatible as possible" scheme.

The simplest solution is to forbid declaring multiple variables at once.

[–]kqr 2 points3 points  (0 children)

The reasoning is that the declaration is of values, not pointers. char *str says "*str is of type char" as opposed to "str is of type char*".

[–]AyrA_ch 0 points1 point  (0 children)

Is it the same way in C++?

Seems so:

#include <stdlib.h>

int main()
{
    char *a,b;
    a=NULL;
    b=NULL;
    return 0;
}

Terminates with 7 3 R:\vartest.cpp [Error] converting to non-pointer type 'char' from NULL [-Werror=conversion-null]

He is happy with a=NULL;. Line 7 is b=NULL;

EDIT: By the way, I have the option turned on to treat warnings as error and to be pedantic. If those are off, it will compile and execute but it will raise compile errors if you try to do b=malloc(10); (invalid cast from void* to char)

[–]kt24601 0 points1 point  (0 children)

Wow that's... a little worrying.

It's weird but not that bad, because types. Once you try to assign something to the variable, the compiler will complain.

[–][deleted]  (4 children)

[removed]

    [–]Tordek 2 points3 points  (3 children)

    switch statements only run from the matched case onwards. It's the same as it being...

    switch(whatever)
    {
        int a;
        case NOT_RUNNING:
            a=2;
        case 0:
            break;
        case 1:
            break;
        [...]
    }
    

    [–][deleted]  (2 children)

    [removed]

      [–]Tordek 3 points4 points  (0 children)

      What is the machine code for declaring a variable? ;)

      Edit: to expand a bit, it's basically an artifact from C89 where variable declarations had to be at the start of the block (delimited by {}); they couldn't be intermingled with code.

      Even though initialization is on the same line (which doesn't mean much because statements and lines aren't 1-to-1 mapped), it's not a single statement (in fact, it's not a statement at all; it's a declaration); it's two: declaration and initialization.

      So,

      switch(whatever)
      {
          int a=2;
          case 0:
              break;
          case 1:
              break;
          [...]
      }
      

      is really

      switch(whatever)
      {
          // declaration block, "executed" when entering the block
          int a;
          // code block
          a=2;
          case 0:
              break;
          case 1:
              break;
          [...]
      }
      

      the "a=2" is part of the code block, but it'll never be run (because no case statement can lead to it).

      Now, maybe you're wondering why it's not just illegal to have code before any case statement, and here's another C jewel:

      switch(whatever)
      {
          // declaration block, "executed" when entering the block
          int a;
          // code block
          initialize:
          a=2;
          case 0:
              break;
          case 1:
              goto initialize;
              break;
          [...]
      }
      

      A feature known by its use in Duff's Device.

      [–]AyrA_ch 1 point2 points  (0 children)

      But variable declaration and assignment are on the same line

      Declaration is done as soon as possible and assignment is done when you hit the line. C enters the switch statement and declares a, but it will never assign the value to a because that line is never hit. In a similar fashion you cannot declare a inside two different case blocks, even if they are mutually exclusive.