#include <stdio.h>
#define MAX 50
int main(void)
{
char str[MAX];
char *s;
char c;
printf("Enter a string < %d characters:", MAX);
fgets(str, MAX, stdin);
s = str;
c = *(s);
printf("s = %c\n", c);
}
If I change the line:
c = *(s);
to:
c = *s;
The program compiles without warning and runs OK with the following
compiler flags (maybe OT):
gcc -std=c99 -pedantic -Wall -Wextra deref.c
Are the parentheses necessary? Section 6.5.3.2 of ISO/IEC 9899:TC2
seems to indicate that the result of the dereference without parentheses
would be an lvalue, perhaps this is related?
--
Ted DeLoggio
> c = *(s);
No meaning at all. They can be omitted without changing the meaning of
the code:
c = *s;
<snip>
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
You didn't mention what happens if you *don't* change the line.
Presumably it behaves exactly the same way.
> Are the parentheses necessary? Section 6.5.3.2 of ISO/IEC 9899:TC2
> seems to indicate that the result of the dereference without
> parentheses would be an lvalue, perhaps this is related?
The parentheses are useless. C99 6.5.3.2 doesn't imply what you
think it does; the expression is an lvalue with or without the
parentheses (see C99 6.5.1p5), but it's not being used in a context
that requires an lvalue so that doesn't matter.
Incidentally, there's a newer post-C99 draft that includes all three
Technical Corrigenda:
<http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf>.
--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
A parenthesized expression is a primary expression, having
the highest precedence.
An identifier expression like s is already primary expression.
Parentheses around a primary expression do nothing; they derive a
primary expression from what is already a primary expression,
without changing any aspect of its meaning (such as its type,
whether it is an lvalue, whether it is a constant expression, ...).
>I do not understand the meaning of the parentheses used when
>dereferencing the char pointer named "s" in the following program:
In this case they are superfluous.
The parentheses serve only to make the expression between them the
operand of the dereference operator. Since the expression is a simple
one without any operators, the parentheses make no difference. *(s)
is exactly the same as *s.
If the expression were *(s+1) then you should have no difficulty
recognizing the difference with *s+1.
>
>#include <stdio.h>
>
>#define MAX 50
>
>int main(void)
>{
> char str[MAX];
> char *s;
> char c;
>
> printf("Enter a string < %d characters:", MAX);
> fgets(str, MAX, stdin);
>
> s = str;
> c = *(s);
> printf("s = %c\n", c);
>}
>
>
>If I change the line:
>c = *(s);
>
>to:
>
>c = *s;
>
>The program compiles without warning and runs OK with the following
>compiler flags (maybe OT):
>
>gcc -std=c99 -pedantic -Wall -Wextra deref.c
>
>Are the parentheses necessary? Section 6.5.3.2 of ISO/IEC 9899:TC2
>seems to indicate that the result of the dereference without parentheses
>would be an lvalue, perhaps this is related?
Not at all.
--
Remove del for email