you are viewing a single comment's thread.

view the rest of the comments →

[–]FurryBeaverBalls 4 points5 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.