Topic: Proposal for string initialization


Author: phimsch@vnet3.vub.ac.be (Peter Himschoot)
Date: 7 Dec 1994 12:30:02 GMT
Raw View
I'm sorry if this has already been proposed, but here it goes:

I'd like to be able to initialize character arrays I use like this :

char name[10] = "Peter". I know you can do it by using initialization
lists, but I think the above solution is far more readable.
It is also very conform with initializing other variables, like
int i = 0;

The compiler could always give an error if the initialising string is
too large to fit the array.

What do you think ?

Peter




Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 7 Dec 1994 18:09:57 GMT
Raw View
In article 2vp@rc1.vub.ac.be, phimsch@vnet3.vub.ac.be (Peter Himschoot) writes:
>I'd like to be able to initialize character arrays I use like this :
>
>char name[10] = "Peter". ...
>
>The compiler could always give an error if the initialising string is
>too large to fit the array.

It has always been the case in K&R C, ANSI/ISO C, and C++ that
 char name[10] = "Peter";
is equivalent to
 char name[10] = { 'P', 'e', 't', 'e', 'r', 0 };
and
 char name[10] = "Peter Himschoot";
will yield an error message.

So what is it you are proposing?

---
Steve Clamage, stephen.clamage@eng.sun.com






Author: phimsch@vnet3.vub.ac.be (Peter Himschoot)
Date: 8 Dec 1994 09:01:10 GMT
Raw View
In article <3c49sb$2vp@rc1.vub.ac.be>, phimsch@vnet3.vub.ac.be (Peter Himschoot) says:
>I'd like to be able to initialize character arrays I use like this :
>char name[10] = "Peter". I know you can do it by using initialization
>lists, but I think the above solution is far more readable.
>It is also very conform with initializing other variables, like int i = 0;
>The compiler could always give an error if the initialising string is
>too large to fit the array.
>What do you think ?
Ooooooooooh, I really goofy'd up here ! I always thought you couldn't
do this in C++, and now I took a good look at the ARM, and my suggestion
already exists ! It's part of the standard already.
I really feel like a Goofy !!!

Sorry !

Peter