Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: sizeof(x) / sizeof(x[0]) Date: Mon, 16 Jan 2017 08:58:02 -0800 Organization: None to speak of Lines: 28 Message-ID: References: <1420cd8f-e5cb-4a8f-93e6-d80a8b050650@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="0a2d73b4260c7d84c3fe58a86676df00"; logging-data="32497"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19E7lf1JcOp8GD0kXhcXTnx" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:XUCXXZAyXqDG8obTlgQO+cXmtoA= sha1:lLgTgmksJqNEwY/NjOsvLHpf2A8= Xref: csiph.com comp.lang.c:101981 Alla _ writes: [...] >> Alla _ wrote: >> > For example: >> > char *array[] = {"abc", "defg", "hijklmop", NULL}; [...] > Thank you very much for this explanation. It is very > clear. Unfortunately, as I have not used this macro > for quite a while, I have completely forgotten (as I have > mentioned in my reply to Ben) that the denominator > has a pointer as its arguments. Thank you. In the macro #define ARRAY_LEN(x) (sizeof (x) / sizeof (x)[0]) the denominator is a pointer *if and only if* x happens to be an array of pointers. The numerator is the size of the array as a whole; the denominator is the size of a single element of the array. It's a general-purpose macro for determining the length in elements of an array *of any type*. (And of course it works only if the declaration of the array object itself is visible and specifies the length.) -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"