Re: How to get array size from a pointer?
- From: Nate Eldredge <nate@xxxxxxxxxx>
- Date: Wed, 08 Oct 2008 11:13:13 -0700
"ggnguser@xxxxxxxxx" <ggnguser@xxxxxxxxx> writes:
It's part of a test and I'm stumped. There is a function
void foo(char **x)
The function signature is given and cannot be changed, so no passing
of other values. The test case involves defining this variable:
char *y[] = { /* bunch of stuff */ }
and calling
foo(y)
In the above, "bunch of stuff" is a series of triplets, two strings
followed by a null string (""). However, the last triplet ends with an
integer 0. This seems that it's supposed to signify the end of the
array.
If I understand right, then an appropriate initializer for y would be
something like
char *y[] = { "foo", "bar", "", "baz", "qux", "", "xyzzy", "plugh", 0 };
However, it appears to me that 0 is the same binary value as
for the empty string (NUL, \0, whatever). So in effect, one cannot
test for it as a sentry value because it's actually the same as the
preceding triplets.
Well, this may or may not be the case. But it's irrelevant to the
problem at hand. I think what you may be overlooking is that an empty
string is not represented by a NULL pointer (which can be got by
assigning the integer 0 to a pointer), but by a (non-NULL) pointer to
a null character ('\0'). (Or, if you prefer, an array of chars of
which the first is '\0'.) So if I write
char *s = "";
then the following expressions are all true:
s != NULL;
*s == '\0';
s[0] = '\0';
Based on this, you could also understand why
char t[30] = "hello";
strcat(t, s);
would leave t unchanged, while strcat(t, NULL) would crash. (If
you're confused, try writing your own version of strcat and see what
it would do in each case.)
In a sense, the point isn't whether the NULL pointer and the null
character '\0' are represented by the same bits (the C standard makes
no guarantee one way or the other). The issue is whether you're
looking at the pointer itself or the thing it points to.
This is complicated somewhat by the fact that some people write their
code in such a way that passing NULL where a string is expected has
the same effect as passing an empty string. But this is something
that they have to do explicitly. (You could modify your version of
strcat to do this if you want.) In the situation at hand, this is
exactly the opposite of what you want; the distinction between the two
is the information you need.
How then, can the function foo() determine the bounds of the array?
Knowing the bounds of the particular test case is not sufficient since
the actual test suite may have arrays of varying size.
Hopefully this discussion is enough of a hint. If not, post again.
By the way, if by "test" you mean this is part of an exam for a
course, don't forget to properly credit this post as a source of
information.
.
- References:
- How to get array size from a pointer?
- From: ggnguser@xxxxxxxxx
- How to get array size from a pointer?
- Prev by Date: Re: How to get array size from a pointer?
- Next by Date: Re: Casting the return value of malloc() ?
- Previous by thread: Re: How to get array size from a pointer?
- Next by thread: Re: How to get array size from a pointer?
- Index(es):
Relevant Pages
|
Loading