Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #147707
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: What are the odds? (a continuation of One works ...) |
| Date | 2019-11-07 11:57 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <87o8xnj3vs.fsf@bsb.me.uk> (permalink) |
| References | (3 earlier) <vvv5sehh7oil3eb5dft8dun7s4t1rbt4h7@4ax.com> <voGwF.93833$vD7.57507@fx06.iad> <g4g6se5u1ggsmecioggck2k7ve59pfdbbp@4ax.com> <joIwF.86203$FF4.79851@fx40.iad> <lny2ws4ba3.fsf@kst-u.example.com> |
Keith Thompson <kst-u@mib.org> writes:
> DFS <nospam@dfs.com> writes:
> [...]
>> char charset[62]
>> ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
>> int charsetLen = strlen(charset);
>> printf("%d \n",charsetLen);
>> 62
>
> That still has undefined behavior. It appears to "work" only because,
> most likely, there just happens to be a null byte in memory immediately
> after charset.
>
> I just compiled and ran your on my system. I got 62 with gcc and clang,
> and 65 with tcc. There is not necessarily any particular reason for
> this, and it could just as easily produce different results tomorrow.
>
>> char charset[63] =
>> "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
>> int charsetLen = strlen(charset);
>> printf("%d \n",charsetLen);
>> 62
>
> That's well defined, but as I mentioned elsethread your (incorrect)
> strlen call is the only thing that relies on charset containing a valid
> string.
>
> If you define charset using [] you can get its size using sizeof.
(This is to the OP...) You will get 63 using sizeof because the size
includes the null that will be put there if you [] in the array
declaration. You have to write sizeof charset - 1. Keith's previous
remark about using sizeof refereed to the case what you gave an explicit
size in the array declaration.
Just in case things have got confused, here are your options:
char s[] = "abc";
s will be a null terminated string and you can get the number of
non-null characters using either strlen(s) or sizeof s - 1.
char s[3] = "abc";
s will not be a string. There is no guarantee that there will be a null
at the end (though there might be one by accident). You can't use
strlen but sizeof s will give you 3.
char *s = "abc";
s points to the start of a null-terminated string. You can get the
number of non-null characters in it using strlen(s) but you can't size
sizeof s because that will report only the size of the pointer s.
(And you can put const in front of all of these to tell the reader the
that this is read-only data. Indeed, in the last case you can write
const char *const s = "abc";
to show that the pointer s itself is read-only as well as the string it
points to.)
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread
Re: What are the odds? (a continuation of One works ...) Barry Schwarz <schwarzb@delq.com> - 2019-11-06 14:23 -0800
Re: What are the odds? (a continuation of One works ...) Bart <bc@freeuk.com> - 2019-11-06 22:35 +0000
Re: What are the odds? (a continuation of One works ...) luser droog <luser.droog@gmail.com> - 2019-11-06 17:09 -0800
Re: What are the odds? (a continuation of One works ...) Keith Thompson <kst-u@mib.org> - 2019-11-06 19:19 -0800
Re: What are the odds? (a continuation of One works ...) Ian Collins <ian-news@hotmail.com> - 2019-11-07 16:30 +1300
Re: What are the odds? (a continuation of One works ...) Bart <bc@freeuk.com> - 2019-11-07 10:59 +0000
Re: What are the odds? (a continuation of One works ...) David Brown <david.brown@hesbynett.no> - 2019-11-07 14:27 +0100
Re: What are the odds? (a continuation of One works ...) DFS <nospam@dfs.com> - 2019-11-06 18:09 -0500
Re: What are the odds? (a continuation of One works ...) Keith Thompson <kst-u@mib.org> - 2019-11-06 19:26 -0800
Re: What are the odds? (a continuation of One works ...) Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-11-07 11:57 +0000
csiph-web