473,805 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to get array size from a pointer?

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

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.
Oct 8 '08
30 3618
gg******@gmail. com said:
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. 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.

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.

Your English description of the array contents is confusing. I suggest you
show us the actual initialisation code - i.e. replace /* bunch of stuff */
with the actual bunch of actual stuff.
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 8 '08 #11
gg******@gmail. com <gg******@gmail .comwrote:
>
However, it appears to me that 0 is the same binary value as
for the empty string (NUL, \0, whatever).
No. An empty string is like an empty box; a null pointer is no box at
all.
--
Larry Jones

Rats. I can't tell my gum from my Silly Putty. -- Calvin
Oct 8 '08 #12
"Bartc" <bc@freeuk.comw rites:
<vi******@gmail .comwrote in message
news:31******** *************** ***********@r37 g2000prr.google groups.com...
>On Oct 8, 4:22 pm, "Bartc" <b...@freeuk.co mwrote:
[...]
>>Any empty string is not necessarily NULL.

An empty string is _never_ NULL.

I missed where the OP mentioned the null string "", and assumed that
NULL was also being used to signify an empty string.

However NULL to represent an empty string can be a useful technique
(although not recognised by standard functions), hence my comment.
[...]

Sure, you can use a null pointer to represent an empty string. You
can also use the number 42, or the string "this is an empty string",
to represent an empty string. You can use anything you like to
represent anything you like, as long as you're consistent about it.

But a null pointer *is not* an empty string, and it doesn't point to
an empty string. And, in my opinion, using a null pointer to
represent an empty string is a bad idea; it's too easy to accidentally
pass it to a function that doesn't know about your convention, and it
loses you the ability to distinguish between an empty string and the
absence of a string.

Finally, strings are not pointers. A pointer can point to a string; a
pointer cannot be a string.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 8 '08 #13
On Oct 9, 12:48 am, Keith Thompson <ks...@mib.orgw rote:
>
But a null pointer *is not* an empty string, and it doesn't point to
an empty string. And, in my opinion, using a null pointer to
represent an empty string is a bad idea; it's too easy to accidentally
pass it to a function that doesn't know about your convention, and it
loses you the ability to distinguish between an empty string and the
absence of a string.

Finally, strings are not pointers. A pointer can point to a string; a
pointer cannot be a string.
Yes. In my opinion, the null/empty string is this string: ""; the null
pointer is this value: NULL.
Oct 8 '08 #14
"lo************ ***@gmail.c0m" <lo************ ***@gmail.comwr ites:
On Oct 9, 12:48 am, Keith Thompson <ks...@mib.orgw rote:
>But a null pointer *is not* an empty string, and it doesn't point to
an empty string. And, in my opinion, using a null pointer to
represent an empty string is a bad idea; it's too easy to accidentally
pass it to a function that doesn't know about your convention, and it
loses you the ability to distinguish between an empty string and the
absence of a string.

Finally, strings are not pointers. A pointer can point to a string; a
pointer cannot be a string.

Yes. In my opinion, the null/empty string is this string: ""; the null
pointer is this value: NULL.
Yes -- but it's not a matter of opinion.

[news.motzarella .org was having problems, which seem to have been
cleared up; sorry if this appears twice.]

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 8 '08 #15
"gg******@gmail .com" <gg******@gmail .comwrites:
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.
Oct 8 '08 #16

"Keith Thompson" <ks***@mib.orgw rote in message
news:ln******** ****@nuthaus.mi b.org...
"Bartc" <bc@freeuk.comw rites:
><vi******@gmai l.comwrote in message
news:31******* *************** ************@r3 7g2000prr.googl egroups.com...
>>On Oct 8, 4:22 pm, "Bartc" <b...@freeuk.co mwrote:
[...]
>>>Any empty string is not necessarily NULL.

An empty string is _never_ NULL.

I missed where the OP mentioned the null string "", and assumed that
NULL was also being used to signify an empty string.

However NULL to represent an empty string can be a useful technique
(although not recognised by standard functions), hence my comment.
[...]

Sure, you can use a null pointer to represent an empty string. You
can also use the number 42, or the string "this is an empty string",
to represent an empty string. You can use anything you like to
represent anything you like, as long as you're consistent about it.
I quite like this other quote in this thread:

<la************ @siemens.comwro te in message
news:5q******** ****@jones.home ip.net...
gg******@gmail. com <gg******@gmail .comwrote:
No. An empty string is like an empty box; a null pointer is no box at
all.
If you go to your drawer to get some socks, the absence of a drawer will
tell you you have no socks, just as well as an empty drawer. And you don't
have to open the drawer to find out.
>
But a null pointer *is not* an empty string, and it doesn't point to
an empty string. And, in my opinion, using a null pointer to
represent an empty string is a bad idea; it's too easy to accidentally
pass it to a function that doesn't know about your convention, and it
loses you the ability to distinguish between an empty string and the
absence of a string.
Well I used null pointers for empty strings before, outside of C. In C, that
would be problematic because standard functions tend to crash when you pass
null pointers. But I do still sometimes use null pointers this way, it just
needs some care.

--
Bartc

Oct 8 '08 #17
"gg******@gmail .com" wrote:
>
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. 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.
Maybe "bunch of stuff" is that, but y is not. y is an array of
pointers to char, and those pointers also point to char arrays that
are not writeable. The arrays are strings, with a '\0' terminal
char. The last string is an empty string, and is used to mark the
end of the array.

Now consider what foo is. It appears, from your definition, to be
a function that returns void (i.e. nothing). It also requires a
parameter, x, which is a pointer to a char pointer.

Combining those things, the call to foo(y) cannot possibly do
anything.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Oct 9 '08 #18
On 9 Oct, 01:27, CBFalconer <cbfalco...@yah oo.comwrote:
"ggngu...@gmail .com" wrote:
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. 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.

Maybe "bunch of stuff" is that, but y is not. *y is an array of
pointers to char, and those pointers also point to char arrays that
are not writeable. *The arrays are strings, with a '\0' terminal
char. *The last string is an empty string, and is used to mark the
end of the array.

Now consider what foo is. *It appears, from your definition, to be
a function that returns void (i.e. nothing). *It also requires a
parameter, x, which is a pointer to a char pointer.

Combining those things, the call to foo(y) cannot possibly do
anything.

--
*[mail]: Chuck F (cbfalconer at maineline dot net)
*[page]: <http://cbfalconer.home .att.net>
* * * * * * Try the download section.- Hide quoted text -

- Show quoted text -
On 9 Oct, 01:27, CBFalconer <cbfalco...@yah oo.comwrote:

<snip>
Now consider what foo is. It appears, from your definition, to be
a function that returns void (i.e. nothing). It also requires a
parameter, x, which is a pointer to a char pointer.

Combining those things, the call to foo(y) cannot possibly do
anything.
i/o?

--
Nick Keighley
Oct 9 '08 #19
CBFalconer <cb********@yah oo.comwrites:
"gg******@gmail .com" wrote:
>>
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. 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.

Maybe "bunch of stuff" is that, but y is not. y is an array of
pointers to char, and those pointers also point to char arrays that
are not writeable. The arrays are strings, with a '\0' terminal
char. The last string is an empty string, and is used to mark the
end of the array.

Now consider what foo is. It appears, from your definition, to be
a function that returns void (i.e. nothing). It also requires a
parameter, x, which is a pointer to a char pointer.

Combining those things, the call to foo(y) cannot possibly do
anything.
Um? Here are some possible definitions for foo that do things.

void foo(char **x) { x[3] = "Hi mom!"; }
void foo(char **x) { while (*x) puts(*x++); }
void foo(char **x) { errno = EDOOFUS; }
void foo(char **x) { longjmp(somewhe re); }
void foo(char **x) { abort(); }

Oct 9 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
10188
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
1
6438
by: Sam | last post by:
Hello all I have a two dimensional array (the dimensions are not known) that needs to be passed to fortran from c++, allocate the dimensions of the array in fortran code, do some filling up of the array in fortran and then accessing it in my c++ code. Say in my c++ code I have; extern "C" { void foo_(float **, int &, int &); }
8
29070
by: Tweaxor | last post by:
Hey, I was trying to figure out was it possible in C to pass the values in an array from one function to another function. Is the possible in C? ex. y is the array that holds seven values If possible how could one pass these seven values in the array to a function that would check the values. I tried return y but it didn't work
6
4154
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the end is found is less efficient than using sizeof operator , since size of the array is completely determined at compile time. i don't quite understand this. Could anyone explain to me in detail ?
7
7418
by: simkn | last post by:
Hello, I'm writing a function that updates an array. That is, given an array, change each element. The trick is this: I can't change any elements until I've processed the entire array. For example, the manner in which I update element 1 depends on several other (randomly numbered) elements in the array. So, I can't change an element until I've figured out how every element changes.
9
2200
by: shaun | last post by:
Dear all, I realized an error in a previous post, I reproduce it here because I'm still not sure how to solve it: I want to make a templated function which points to one-past-the-end of a simple array, to pass to a range constructor for a const vector. Here is some demonstration code: #include <iostream> using namespace std;
12
3892
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
23
7425
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
7
8138
by: bowlderster | last post by:
Hello,all. I want to get the array size in a function, and the array is an argument of the function. I try the following code. /*************************************** */ #include<stdio.h> #include<stdlib.h> #include<math.h>
33
7191
by: Adam Chapman | last post by:
Hi, Im trying to migrate from programming in Matlab over to C. Im trying to make a simple function to multiply one matrix by the other. I've realised that C can't determine the size of a 2d array, so im inputting the dimensions of those myself. The problem is that the output array (C=A*B) has as many rows as A and as many columns as B. I would think of initialising C with:
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10613
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7649
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.