Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

char array initialized by string literal

29 views
Skip to first unread message

wij

unread,
Jul 27, 2023, 9:51:57 AM7/27/23
to
I found char array can be initialized by string literal ! Is such coding correct?

--------------------------------------
int main() {
char tnam[]="tmpnamXXXXXX";

int fd=::mkstemp(tnam);
return 0;
}

g++ t.cpp -std=gnu++11

Malcolm McLean

unread,
Jul 27, 2023, 10:01:50 AM7/27/23
to
Yes, that's fine.
char modifable[] = "This string can be modiifed";
const char *constant = "This string shouldn't be modified";
Message has been deleted

Alf P. Steinbach

unread,
Jul 27, 2023, 1:36:58 PM7/27/23
to
On 2023-07-27 4:14 PM, Tony Oliver wrote:
> On Thursday, 27 July 2023 at 14:51:57 UTC+1, wij wrote:
> N3096: 6.7.10 Initialization
>
> 15 - An array of character type may be initialized by a character string
> literalor UTF-8 string literal, optionally enclosed in braces.
> Successive bytes of the string literal (including the terminating null
> character if there is room or if the array is of unknown size) initialize
> the elements of the array.

That appears to be the C standard, while this is a C++ group.

The languages are not the same.

C++ requires that there is room for a terminating zero.


- Alf

Tony Oliver

unread,
Jul 28, 2023, 7:19:27 AM7/28/23
to
Yes - I realised this wasn't in clc as soon as I posted it.
My immediate retraction request was, of course, ignored.
Apologies for any confusion caused.
0 new messages