473,805 Members | 2,099 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 3620
"Bartc" <bc@freeuk.comw rote:
"Keith Thompson" <ks***@mib.orgw rote in message
"Bartc" <bc@freeuk.comw rites:
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
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.
I take it, then, that you've never worked with databases much. I assure
you, there is a world of difference between "We have no record of this
person" and "We have a record of this person, and his phone number field
is blank".
Basically, it's like asking how many legs a fish has, and how many legs
a null string has. In the first case, the answer is "none"; in the
second case, it's "What have you been smoking?".
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.
No, it needs a situation where there is no difference between "doesn't
exist" and "is blank". Those situations are rarer than most people, even
most programmers with no database experience, believe.

Richard
Oct 9 '08 #21
Nate Eldredge wrote:
CBFalconer <cb********@yah oo.comwrites:
>"gg******@gmai l.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(); }
In other words you want to use globals and side-effects. I should
have specified further.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Oct 9 '08 #22
On Oct 9, 12:31 pm, Nate Eldredge <n...@vulcan.la nwrote:

<snip>
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); }
Well, longjmp takes two arguments...
Oct 9 '08 #23
On Oct 9, 6:25 pm, vipps...@gmail. com wrote:
On Oct 9, 12:31 pm, Nate Eldredge <n...@vulcan.la nwrote:

<snip>
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); }

Well, longjmp takes two arguments...

Also, foo belongs in the programmers namespace, but EDOOFUS in the
implementations namespace, so the penultimate function can't work.
Oct 9 '08 #24
CBFalconer <cb********@yah oo.comwrites:
Nate Eldredge wrote:
>CBFalconer <cb********@yah oo.comwrites:
<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.

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(); }

In other words you want to use globals and side-effects. I should
have specified further.
Programming in C without side effects is almost impossible. Even if
we add the extra condition that all the x[i] point to unmodifiable
arrays (a restriction that Nate Eldredge's examples comply with) then
foo can still be useful. A clearer example might be

void sort_strings(ch ar **x)
{
qsort(x, count_strings(x ), sizeof *x, sptr_cmp);
}

where count_strings is the function the OP was originally asking
about and sptr_cmp compares strings when passed void *s that point at
pointers to these strings.

There are lots of useful functions with the same prototype as foo.

--
Ben.
Oct 9 '08 #25
On Oct 9, 11:26*am, vipps...@gmail. com wrote:
On Oct 9, 6:25 pm, vipps...@gmail. com wrote:
On Oct 9, 12:31 pm, Nate Eldredge <n...@vulcan.la nwrote:
<snip>
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); }
Well, longjmp takes two arguments...

Also, foo belongs in the programmers namespace, but EDOOFUS in the
implementations namespace, so the penultimate function can't work.
I think this is wrong. It can work. Either the implementation can
define EDOOFUS, which is just fine, or user code can define it if it
is not defined, also fine. It is generally a good idea NOT to start
your macros E[digit|uppercase] as it conflicts with a "future
direction" of a header. But the standard doesn't have language like
"reserved for any use", like leading __. But it is as far as I can
see legal and portable to put this in your code...

#include <errno.h>
#ifndef EDOOFUS
#define EDOOFUS 42
#endif

-David
Oct 9 '08 #26
Richard Bos <rl*@hoekstra-uitgeverij.nlwr ote:
>
No, it needs a situation where there is no difference between "doesn't
exist" and "is blank". Those situations are rarer than most people, even
most programmers with no database experience, believe.
Even programmers *with* database experience. Oracle, for example,
continues to treat empty string values and nulls identically, although
they have warned (forever) that that could change some time in the
future. I've run into way too much code that depends on that
equivalence and doesn't work when ported to a different db that does
distinguish.
--
Larry Jones

They can make me do it, but they can't make me do it with dignity. -- Calvin
Oct 9 '08 #27
On Thu, 9 Oct 2008 08:50:24 -0700 (PDT),
David Resnick <ln********@gma il.comwrote:
On Oct 9, 11:26Â*am, vipps...@gmail. com wrote:
>On Oct 9, 6:25 pm, vipps...@gmail. com wrote:
On Oct 9, 12:31 pm, Nate Eldredge <n...@vulcan.la nwrote:
[snip]
void foo(char **x) { errno = EDOOFUS; }
void foo(char **x) { longjmp(somewhe re); }
Well, longjmp takes two arguments...

Also, foo belongs in the programmers namespace, but EDOOFUS in the
implementation s namespace, so the penultimate function can't work.

I think this is wrong. It can work. Either the implementation can
define EDOOFUS, which is just fine,
And, in fact, there are implementations that do exactly that.

Martien
--
|
Martien Verbruggen | Quick! Hire a teenager while they still know
| everything.
|
Oct 9 '08 #28
la************@ siemens.com wrote:
Richard Bos <rl*@hoekstra-uitgeverij.nlwr ote:

No, it needs a situation where there is no difference between "doesn't
exist" and "is blank". Those situations are rarer than most people, even
most programmers with no database experience, believe.

Even programmers *with* database experience. Oracle, for example,
continues to treat empty string values and nulls identically, although
they have warned (forever) that that could change some time in the
future.
Now, I'm not much of an SQL expert, certainly not compared to the people
at Oracle, but... isn't that illegal?

Richard
Oct 10 '08 #29
Richard Bos wrote:
la************@ siemens.com wrote:
.... snip ...
>
>Even programmers *with* database experience. Oracle, for
example, continues to treat empty string values and nulls
identically, although they have warned (forever) that that
could change some time in the future.

Now, I'm not much of an SQL expert, certainly not compared to
the people at Oracle, but... isn't that illegal?
No, it isn't. First, they can certainly design their own functions
in any fashion they wish. Second, even if implementing C standard
functions, this is simply imposing a definition for 'undefined
behavior' on THEIR system.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Oct 10 '08 #30

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
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10109
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.