Deutsch   English   Français   Italiano  
<8734ph7qe5.fsf@nosuchdomain.example.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news.nobody.at!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: "undefined behavior"?
Date: Wed, 12 Jun 2024 15:22:26 -0700
Organization: None to speak of
Lines: 34
Message-ID: <8734ph7qe5.fsf@nosuchdomain.example.com>
References: <666a095a$0$952$882e4bbb@reader.netnews.com>
	<v4d4hm$1rjc5$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Thu, 13 Jun 2024 00:22:27 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="32c0f168ad8ff54df06981ba253a92e6";
	logging-data="1935800"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19wf/FA63fpKmJHxvRsnn90"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:5XVpwIyYc/NnxLsHUj472ptNPuo=
	sha1:hhhqr4h/IeXf5xTh7OK36DlMsSY=
Bytes: 2375

Janis Papanagnou <janis_papanagnou+ng@hotmail.com> writes:
> On 12.06.2024 22:47, DFS wrote:
[...]
>> before: char outliers[100];
>> after : char outliers[100] = "";
[...]
> Seriously; why do you expect [in C] a declaration to initialize that
> stack object? (There are other languages that do initializations as
> the language defines it, but C doesn't; it may help to learn before
> programming in any language?) And why do you think that "" would be
> an appropriate initialization (i.e. a single '\0' character) and not
> all 100 elements set to '\0'? (Someone else might want to access the
> element 'answer[99]'.) And should we pay for initializing 1000000000
> characters in case one declares an appropriate huge array?

This:
    char outliers[100] = "";
initializes all 100 elements to zero.  So does this:
    char outliers[100] = { '\0' };
Any elements or members not specified in an initializer are set to zero.

If you want to set an array's 0th element to 0 and not waste time
initializing the rest, you can assign it separately:
    char outliers[100];
    outliers[0] = '\0';
or
    char outliers[100];
    strcpy(outliers, "");
though the overhead of the function call is likely to outweigh the
cost of initializing the array.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */