Newbie: Array of pointers to strings questions.

K

Keith Thompson

Christian Kandeler said:
It is elusive _because_ you would never write it that way. The OP is
possibly the first person ever to have used the sizeof operator on a
function call.

I doubt it. In fact, I can imagine applying the sizeof operator to a
function call actually being useful. It gives you the size in bytes
of the function's result without calling the function (since the
operand of sizeof isn't evaluated unless it's a VLA).
 
C

CBFalconer

Lawrence said:
pete said:
... snip ...

void free_ptrs(char **s, size_t nmemb)
{
while (nmemb-- != 0) {
free(s[nmemb]);
}
}

A slight reorganization adds a world of safety, and makes it do
the right thing when nmemb is zero.

void free_ptrs(char **s, size_t nmemb)
{
while (nmemb) free(s[--nmemb]);
}

The only difference I see between the 2 versions is the value of
nmemb after the loop terminates. But since that value isn't used
and is well defined in both cases they are functionally equivalent.

I do think your version is cleaner however.

Apologies. The original does work correctly. I was thinking
(faultily) that it would do the unsigned wrap around thing and run
indefinitely for an input value of zero, while trying to access
non-existing array members.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
C

Christian Kandeler

Keith said:
In fact, I can imagine applying the sizeof operator to a
function call actually being useful. It gives you the size in bytes
of the function's result without calling the function (since the
operand of sizeof isn't evaluated unless it's a VLA).

While you are technically correct (I wasn't aware that the function doesn't
get called), I would find such as construct rather confusing, if not
obfuscating. IMHO, this outweighs the advantage of not having to hard code
the data type explicitly. I will therefore continue to write sizeof(int)
instead of sizeof printf("Hello World\n").


Christian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top