Reputation: 3423
//if the following code works
char *ptr=a+12;
//why doesnt this work
char *(ptr=a+12);
Upvotes: 2
Views: 145
Reputation: 133112
because char *ptr=a+12;
is a declaration with an initializer and char *(ptr=a+12);
is ... well, nothing.
But this will work.
char* ptr;
ptr = a+12;
char x = *(ptr = a+12);
Upvotes: 2
Reputation: 385385
char* ptr = a + 12;
declares and defines a pointer-to-char to point 12 characters after a
does.
char* (ptr = a + 12);
tries to assign the value (a + 12
) to the pointer ptr
, and then dereference it to produce a value. However Type value
is not valid syntax (like int 0;
or char 'x';
are not valid), and ptr
is never declared/defined.
In short, it's completely senseless.
Upvotes: 0
Reputation: 34655
()
has a higher priority. So, the expression in it evaluates first and is not a valid lvalue
to assign to.
Upvotes: 0
Reputation: 6593
Because you are declaring as a pointer a whole expression, which makes no sense. A pointer must be a variable.
Upvotes: 0
Reputation: 91320
char * ptr;
declares a variable, =a+12
gives it a value. What you're doing makes no sense, the variable must exist in order for a value to be assigned. What are you trying to achieve?
This would be valid.
char * foo;
char * ptr = (foo = a + 12);
Upvotes: 0
Reputation: 437804
Because (ptr=a+12)
is not a valid name for a variable. What are you trying to achieve exactly?
Upvotes: 2