c - A legal array assignment. Is it possible? -
after reading chapter structures in k&r book decided make tests understand them better, wrote piece of code:
#include <stdio.h> #include <string.h> struct test func(char *c); struct test { int ; int j ; char x[20]; }; main(void) { char c[20]; struct {int ; int j ; char x[20];} = {5 , 7 , "somestring"} , b; c = func("another string").x; printf("%s\n" , c); } struct test func(char *c) { struct test temp; strcpy(temp.x , c); return temp; }
my question is: why c = func("another string").x;
working (i know it's illegal, why working)? @ first wrote using strcpy()
(because seemed logical thing do) kept having error:
structest.c: in function ‘main’: structest.c:16:2: error: invalid use of non-lvalue array
char c[20]; ... c = func("another string").x;
this not valid c code. not in c89, not in c99, not in c11.
apparently compiles latest gcc
versions 4.8
in -std=c89
mode without diagnostic assignment (clang
issues diagnostic). bug in gcc
when used in c89 mode.
relevant quotes c90 standard:
6.2.2.1 "a modifiable lvalue lvalue not have array type, not have incomplete type, not have const-qualified type. , if structure or union. not have member (including. recursively, member of contained structures or unions) const-qualified type."
and
6.3.16 "an assignment operator shall have modifiable lvalue left operand."
6.3.16 constraint , imposes @ least gcc
issue diagnostic gcc
not, bug.
Comments
Post a Comment