Re: How to get array size from a pointer?



"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.
.



Relevant Pages

  • Re: Can a ref parameter be saved for later use?
    ... but in this scenario the 'passing back' will have to happen later. ... Form(ref string[] strLst) { ... Even in C/C++ it's a dangerous thing to capture a pointer to some data and save it for reuse later. ...
    (microsoft.public.dotnet.languages.csharp)
  • =?Windows-1252?Q?Re:_VB.Net_2003:_Exception=2C_keine_Ahnung_warum_..._v?= =?Windows-1252?Q?=
    ... Die Übergabe eines Strings mit ByVal liefert einen Pointer. ... String arguments are a special case. ... Passing a string by value means you are ... "Passing Strings to a DLL Procedure" later in this chapter. ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • Re: String Functions in MF
    ... What I need is a pointer to the string passed to the procedure. ... want to trim. ... defined length - passing it with length of... ...
    (comp.lang.cobol)
  • Re: Replacing a word in a string
    ... [string arguments in standard library functions] ... >> Making a null pointer equal to an empty string - effectively, ... A null pointer is not a string, ...
    (comp.lang.c)
  • Re: Calling clarion DLL function from C++ prototype
    ... - your prototypes for the first param are right, but you're passing a ... string. ... So you're going to need to write code on the clarion side to ... manually derefernce the pointer (since CW doesn't know what pointers ...
    (comp.lang.clarion)

Loading