Is this valid code? 
Author Message
 Is this valid code?

The following code compiles without a hitch with cc on SunOS 4.1.2, but
wont compile on Coherent 4.2.  Who's right??

typedef struct person
{
        char    name[81];
        int     age;

Quote:
} PERSON;

PERSON me;

main()
{
        printf( "%s %d", me.name, me.age  );

Quote:
}

Coherent complains that 'me' is not declared...

--

Lane Spiers



Mon, 14 Oct 1996 01:59:43 GMT  
 Is this valid code?

Quote:

>The following code compiles without a hitch with cc on SunOS 4.1.2, but
>wont compile on Coherent 4.2.  Who's right??

The code is not perfect, but it should compile without errors.
Why not change your program to see whether the compiler is
broken or just confused:

Add :
  #include <stdio.h>

to get a prototype for printf. Your compiler should generate a
warning otherwise.

Quote:
>typedef struct person
>{
>    char    name[81];
>    int     age;
>} PERSON;
>PERSON me;
>main()

Change to :
  int main(void)

Implicit int should be accepted without a warning.

Quote:
>{
>    printf( "%s %d", me.name, me.age  );

Add :
    return 0;

Quote:
>}
>Coherent complains that 'me' is not declared...

Even if the compiler was confused by your legal code, this is
probably not the best way of telling you about it :-)

Kurt Watzka


-------------------------------------



Mon, 14 Oct 1996 18:00:20 GMT  
 Is this valid code?

Quote:
>The following code compiles without a hitch with cc on SunOS 4.1.2, but
>wont compile on Coherent 4.2.  Who's right??

>typedef struct person
>{
>    char    name[81];
>    int     age;
>} PERSON;

>PERSON me;

>main()
>{
>    printf( "%s %d", me.name, me.age  );
>}

>Coherent complains that 'me' is not declared...

Is that the correct message that it responds with? Of so, it is in
error, IMHO. However, one thing that needs to be pointed out is that
you have not initialized the items inside the structure 'me'.  You may
get a blank for me.name and 0 for me.age, but this is totally
accidental rather than good programming.

If Coherent is complaining about the structure values not being set or
initialized, then it is right. Although I do not expect C compilers to
point that out to me usually - lint, yes, C compilers, no (certainly
not without all the warning levels turned up "high").

In any case, make sure to initialize properly. Try the following:

typedef struct person
{
        char    name[81];
        int     age;

Quote:
} PERSON;

PERSON me;

main()
{
        strcpy ( me.name , "" );
        me.age = 0;

        printf( "%s %d", me.name, me.age  );

Quote:
}

as a hypothetical example.

                                                                Z

--
-------------------------------------------------------------------------
| Syed Zaeem Hosain          P. O. Box 610097            (408) 441-7021 |

-------------------------------------------------------------------------



Tue, 15 Oct 1996 04:22:21 GMT  
 Is this valid code?

Quote:

>>The following code compiles without a hitch with cc on SunOS 4.1.2, but
>>wont compile on Coherent 4.2.  Who's right??

>>typedef struct person
>>{
>>        char    name[81];
>>        int     age;
>>} PERSON;

>>PERSON me;

>>main()
>>{
>>        printf( "%s %d", me.name, me.age  );
>>}

>>Coherent complains that 'me' is not declared...
>             However, one thing that needs to be pointed out is that
>you have not initialized the items inside the structure 'me'.  You may
>get a blank for me.name and 0 for me.age, but this is totally
>accidental rather than good programming.

It is not accidental and the code above is an extract (hopefully)
of a real program.

Quote:
>If Coherent is complaining about the structure values not being set or
>initialized, then it is right. Although I do not expect C compilers to
>point that out to me usually - lint, yes, C compilers, no (certainly
>not without all the warning levels turned up "high").

Borland does complain (with levels high ;). But only for uninitialized
local vars. I think it is very difficulty for a compiler or lint
to detect an uninitialized global variable, because therefore it has
to run the program. Perhaps some other functions perhaps in an other
module set/change the value.
Btw. initializing of global vars in the definition can be dangerous.
our crosslinker puts them to the ROM until I tell it not to do.

But concerning the original question. The program has to be compiled
AND it has to work without errors, because I learned that global
vars are initialized to zero. (expect the are initalized to something
else)

Quote:
>In any case, make sure to initialize properly. Try the following:

[corrected example deleted]

Of course, the original posting is an example too. We always force
people to extract an as small as possible example of there problem.
Therefore we should not complain, if the example is in some way
senseless.

hans friedrich steffani
--



0721 / 608-6008



Tue, 15 Oct 1996 14:20:41 GMT  
 Is this valid code?


|> : >The following code compiles without a hitch with cc on SunOS 4.1.2, but
|> : >wont compile on Coherent 4.2.  Who's right??
|> : >
|> : >typedef struct person
|> : >{
|> : >    char    name[81];
|> : >    int     age;
|> : >} PERSON;
|> : >
|> : >PERSON me;
[munch munch]
|> Before I get lots of flame, yes, "me" is of storage class 'static' and linkage
|> 'external'.  Don't tell me there is no "static" declaration!

How unfortunate! "me" does not have *storage class* "static", but is of static
*duration*. Its storage class defaults to "extern."

[munch more]

--
                                       ^^

University of Twente                 =x=  \        tel. +31 53 893747
Tele-Informatics & Open Systems        |   \       tfx. +31 53 333815
P.O. Box 217   7500 AE Enschede       /|__  \
The Netherlands                      (____)_/

``Look up the words in a dictionary. -- Ian Stewart - Does God Play Dice?
    Mega: big                          (Referring to the use of high-performance
    Flop: failure''                     computers for weather prediction.)



Tue, 22 Oct 1996 20:11:35 GMT  
 Is this valid code?


: >The following code compiles without a hitch with cc on SunOS 4.1.2, but
: >wont compile on Coherent 4.2.  Who's right??
: >
: >typedef struct person
: >{
: >  char    name[81];
: >  int     age;
: >} PERSON;
: >
: >PERSON me;
: >
: >main()
: >{
: >  printf( "%s %d", me.name, me.age  );
: >}
: >
: >Coherent complains that 'me' is not declared...

There is ABSOLUTELY NOTHING WRONG with this code.

: ..... However, one thing that needs to be pointed out is that
: you have not initialized the items inside the structure 'me'.  You may
: get a blank for me.name and 0 for me.age, but this is totally
: accidental rather than good programming.

: [...]

Nope.  The Standard says very clearly that static variables are initialised
to zero if not otherwise specified (or to NULL if a pointer, or to 0.0 if
of floating type, but that's another story).  Hence the char array and the
int will all contain zero bytes only. Hence me.name is "" if interpreted as
a string, and me.age is 0.

Before I get lots of flame, yes, "me" is of storage class 'static' and linkage
'external'.  Don't tell me there is no "static" declaration!

In times long past, people wrote compilers that didn't initialise properly.
This had two unfortunate consequences:

        Variables were unnecessarily initialised to zero by the programmer,
        which meant all these zeroes had to be stored in the executable,
        rather than created when the program was loaded,

        most text books on C keep spreading this nonsense to this day.

(And, judging from this newsgroup, even the people at the good old
Rechenzentrum in Karlsruhe, where 25 years ago I learned to program Algol 60.)

--
Martin Sohnius

Novell Labs Europe
Bracknell, England
+44-344-724031

(My opinions may not be those of Novell!)



Tue, 22 Oct 1996 05:59:18 GMT  
 Is this valid code?

Quote:

>: ..... However, one thing that needs to be pointed out is that
>: you have not initialized the items inside the structure 'me'.  You may
>: get a blank for me.name and 0 for me.age, but this is totally
>: accidental rather than good programming.

>Nope.  The Standard says very clearly that static variables are initialised
>to zero if not otherwise specified (or to NULL if a pointer, or to 0.0 if
>of floating type, but that's another story).  Hence the char array and the
>int will all contain zero bytes only. Hence me.name is "" if interpreted as
>a string, and me.age is 0.

Agreed. Yes, I screwed up here. I guess I have always been of the
"initialize everything correctly" school of thought and extended my
faulty "knowledge" of local variable being uninitialized to globals.
FWIW, I erred on the correct side.

The FAQ has it correctly stated - I went and checked, and Steve Summit
has also correctly identified this in another response to this post.

However, I still intend to continue to initialize globals as needed in
my code - in this scenario, I'd rather wear suspenders and a belt.
Since static initializations are constant and done at compile time,
there is no loss in efficiency in doing it this way.

                                                                Z

--
-------------------------------------------------------------------------
| Syed Zaeem Hosain          P. O. Box 610097            (408) 441-7021 |

-------------------------------------------------------------------------



Fri, 25 Oct 1996 11:13:32 GMT  
 Is this valid code?

Quote:

>However, I still intend to continue to initialize globals as needed in
>my code - in this scenario, I'd rather wear suspenders and a belt.

I'm a suspenders and bra man myself, just like my dear papa.

(Yes, I'm weak, but it was too good to pass up.)

--
Alan Watson                        | C is not a big language, and it is not

Department of Astronomy            |                  -- Kernighan & Ritchie
University of Wisconsin -- Madison |



Fri, 25 Oct 1996 19:13:25 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. VC71 final beta - Reject valid C++ code snippet

2. Showing Call Stack and Determine which object is not valid LARGE CHUNK OF CODE

3. valid c code?

4. is this valid code?

5. Who thinks this is valid C code?

6. Is this valid C++ code?

7. Please I am looking for sample code (C++ BUT NO MFC PLEASE)

8. Am I Assuming Too Much With This Code?

9. What am i doing wrong in this code?

10. Please look at this code n tell me where i am wrong

11. Code: What am I doing wrong?

12. Help-Assignment2// This time I am posting the code!!!Thanx

 

 
Powered by phpBB® Forum Software