473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Very simple: Find out if element of array exists

Hello guys,

I would like to do something seemingly simple: find out if an
element in an array that is passed to my function exists.

I used to think I could just do: if (arr[i]) ...
However, if this element's value happens to be 0, the conditional
will return false, even though the element actually exists.

Any ideas? Can you check if the element == null?

- Noah
Jul 19 '05 #1
13 34314
Noah Spitzer-Williams wrote:
Hello guys,

I would like to do something seemingly simple: find out if an
element in an array that is passed to my function exists.


You can't pass arrays to functions.

All elements in an array exist.

I don't understand the question.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2
On 23 Sep 2003 19:10:57 -0700, no****@cyberdude.com (Noah Spitzer-Williams) wrote:
I would like to do something seemingly simple: find out if an
element in an array that is passed to my function exists.


That is very simple indeed: all elements of an array exist.

Jul 19 '05 #3
On Wed, 24 Sep 2003 02:14:30 GMT, Kevin Goodsell <us*********************@neverbox.com> wrote:
Noah Spitzer-Williams wrote:
Hello guys,

I would like to do something seemingly simple: find out if an
element in an array that is passed to my function exists.
You can't pass arrays to functions.


Is that irony?

All elements in an array exist.

I don't understand the question.


AOL.

Jul 19 '05 #4
"Noah Spitzer-Williams" <no****@cyberdude.com> wrote in message
news:3d**************************@posting.google.c om...
Hello guys,

I would like to do something seemingly simple: find out if an
element in an array that is passed to my function exists.

I used to think I could just do: if (arr[i]) ...
However, if this element's value happens to be 0, the conditional
will return false, even though the element actually exists.

Any ideas? Can you check if the element == null?

If you set up an array of points to your elements (even if they were
elements of an array) then you can have the pointer as NULL when you have no
valid value for that element. But any array you declare will have elements
with some value in each of them.
--
Gary
Jul 19 '05 #5
On 23 Sep 2003 19:10:57 -0700, no****@cyberdude.com (Noah
Spitzer-Williams) wrote:
I would like to do something seemingly simple: find out if an
element in an array that is passed to my function exists.

I used to think I could just do: if (arr[i]) ...
However, if this element's value happens to be 0, the conditional
will return false, even though the element actually exists.


First of all, when array names are used as the argument to a function,
it is the address of the first element (ie. a pointer) that is
actually passed. This means you usually also have to pass in the size
of the array. ((A better choice would be a vector!))

Anyway, when you create an array, say of size 10, all ten elements
"exist". If you want to have 'gaps' in the array (that is, an array of
size 10 where not all of the elements have useful data), you probably
need to create an array (or vector) of *pointers to data* rather than
an array of data. That way, you can initialize all elements to 0
(null) and be able to check for real data at any location.

Of course, you are now getting into the realm of dynamic allocation
with new and of course having to clean up that memory with delete.

If this is what you're looking for and still need some help, reply to
this thread for more details (since I don't know how much of this you
know how to do).

S.

PS. Shame some of the other posters just give you a smart alec answer.
Not everyone here is like that.
Jul 19 '05 #6
What I mean is that, say the array that is passed has 20 elements...
how do I figure this out?

If I do array[50] on a 20 element array, obviously c++ will error. I
need to figure out the size of this array.

- Noah
al***@start.no (Alf P. Steinbach) wrote in message news:<3f****************@News.CIS.DFN.DE>...
On 23 Sep 2003 19:10:57 -0700, no****@cyberdude.com (Noah Spitzer-Williams) wrote:
I would like to do something seemingly simple: find out if an
element in an array that is passed to my function exists.


That is very simple indeed: all elements of an array exist.

Jul 19 '05 #7
Noah Spitzer-Williams wrote:
What I mean is that, say the array that is passed has 20 elements...
how do I figure this out?

If I do array[50] on a 20 element array, obviously c++ will error. I
need to figure out the size of this array.


You cannot. You have to pass it to the function as a separate argument.

--
Attila aka WW
Jul 19 '05 #8
"Noah Spitzer-Williams" <no****@cyberdude.com> wrote in message
news:3d**************************@posting.google.c om...
Hello guys,

I would like to do something seemingly simple: find out if an
element in an array that is passed to my function exists.

I used to think I could just do: if (arr[i]) ...
However, if this element's value happens to be 0, the conditional
will return false, even though the element actually exists.

Any ideas? Can you check if the element == null?
When your garbage can array holds garbage , it's still an array of garbage
cans.
What's more disconcerting is the fact that garbage is initialized
differently depending on the compiler used. And the pile of garbage in each
can is never NULL, it's just garbage and it should be treated as such..


- Noah

Jul 19 '05 #9
Noah Spitzer-Williams wrote:

Please don't top-post.

http://www.parashift.com/c++-faq-lit...t.html#faq-5.4
What I mean is that, say the array that is passed has 20 elements...
how do I figure this out?
Pass the size in separately. You can't create information that isn't
there - somehow you have to get the size information into the function.

If I do array[50] on a 20 element array, obviously c++ will error.
That's not obvious at all. Indexing beyond the end of an array gives
undefined behavior. Anything can happen - an error, no error, random
file deletion - anything.
I
need to figure out the size of this array.


The size of an array is easy to figure out:

size_t nelements = sizeof(array) / sizeof(*array);

HOWEVER this only works with *arrays*, NOT with pointers. In particular,
you cannot do this:

int *array = new int[somesize];
// ...
size_t nelements = sizeof(array) / sizeof(*array); // WRONG!!

Or this:

void somefunc(int array[])
{
size_t nelements = sizeof(array) / sizeof(*array); // WRONG!!
// ...
}

Since in these cases, the thing called 'array' is actually a *pointer*,
not an array.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #10
"Attila Feher" <at**********@lmf.ericsson.se> wrote:
Noah Spitzer-Williams wrote:
I need to figure out the size of this array.


You cannot. You have to pass it to the function as a separate argument.


If he spelled the array declaration correctly, he could use the 'size()'
member function, for example:

void f(std::vector<int>& array) {
std::cout << "size: " << array.size() << "\n";
// ...
}

Of course, this assumes that he is not fiddling around with things only
few people need to touch, ie. built-in array.

There are a few situations, where built-in array come in handy, though,
namely when defining a set of elements, eg.:

std::string weekdays[] = {
"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"
};

Of course, in this situation we know how many elements the array has but
in general we don't. In these cases, the size of the array can be
determined using a simple template function:

template <typename T, int sz> int size(T (&)[sz]) { return sz; }

int main() {
std::string weekdays[] = { ... };
std::cout << "no. weekdays: " << size(weekdays) << "\n";
}

This is about the only situation where I would suspect a user really
relying on use of a built-in array. In other situations, appropriate
classes like eg. 'std::vector<T>' are much preferable.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 19 '05 #11
no****@cyberdude.com (Noah Spitzer-Williams) wrote in message news:<3d**************************@posting.google. com>...
I would like to do something seemingly simple: find out if an
element in an array that is passed to my function exists.
The general rule is that, if you don't already know the size of the
array, you cannot determine the size of the array. The rule works as
follows:

std::vector (the standard C++ resizeable array) and boost::array (a
third-party fixed-size array class, available from boost.org) both
know their own size and can supply it on request:

#include <vector>

template <typename T>
std::size_t size (const std::vector<T> &v) { return v.size(); }

#include <boost/array.hpp>

template <typename T, std::size_t N>
std::size_t size (const boost::array<T, N> &v) { return v.size(); }

A named array also knows its own size, but a function can only
determine that size if the array is passed by reference:

template <typename T, std::size_t N>
std::size_t size (T (&v) [N]) { return N; }

template <typename T, std::size_t N>
std::size_t size (const T (&v) [N]) { return N; }

Besides that (or the rare pointer-to-array), arrays are passed by
pointer to the first element, never by value. That means that the
following prototypes are identical:

void f (int *array);
void g (int array[]);
void h (int array[20]);

(Note that all three functions accept any array of ints, be it int[50]
or int[5]. The "20" is misleading, allowed but ignored by C++. Don't
put it in your code.)

Since a pointer does not maintain the size of the array into which it
points (if it points into one at all), you must pass that in yourself:

template <typename T> void f (const T *v, std::size_t n);

int main () {
int array[20];
f(array, size(array)); // use the by-reference size() function
}

Also, since pointers do not maintain sizes, it is impossible to
calculate the size of an array allocated with new[] or similar. The
solution is to use std::vector instead, or to maintain the size
yourself. (I go with std::vector in almost all cases.)
I used to think I could just do: if (arr[i]) ...
However, if this element's value happens to be 0, the conditional
will return false, even though the element actually exists.
All elements of an array exist.
Any ideas? Can you check if the element == null?


Not unless that element is a pointer. But that's not what you meant.
:)

- Shane
Jul 19 '05 #12
WW
Dietmar Kuehl wrote:
If he spelled the array declaration correctly, he could use the
'size()' member function, for example:

void f(std::vector<int>& array) {


Hehe. Hehehehe. :-) Spelled. :-)

--
WW aka Attila

Jul 19 '05 #13

"Noah Spitzer-Williams" <no****@cyberdude.com> wrote in message news:3d**************************@posting.google.c om...
If I do array[50] on a 20 element array, obviously c++ will error. I
need to figure out the size of this array.


I don't understand what you mean by "C++ will error." You have
an error, but it's all yours, and C++ may not detect it (It's not required
to) either at compile time or runtime.

Jul 19 '05 #14

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

Similar topics

3
by: Christopher Benson-Manica | last post by:
I need a simple HTML element whose text I can change dynamically. What is it? (it doesn't seem to be <div>, since I can't seem to find what properties a <div> has...) -- Christopher...
0
by: Pete Beech | last post by:
Hi, I've looked all over for any information about this, and either this is a bug that I cannot find reported or I've misunderstood something. Lets say, in the XML Designer in VS.NET 2003, you...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
9
by: Steven C. | last post by:
Hello: I'm getting an error, "primary key not defined" when trying to use the FIND method on the DataTable Rows collection. I have a typed dataset called 'MortgagesDS' that I created with the...
6
by: Daz | last post by:
Hi Everyone. I know this is a stupid question, but I have been sifting through php.net and I am about ready to pull my hair out over it... I have seen a function similar to in_array() and...
7
by: =?utf-8?B?5YiY5piK?= | last post by:
Hi, folks, Is it possible to delete an element from a sorted array with O(1) time? Best regards
4
by: Gotch | last post by:
Hi, I'm getting a very strange behaviour while running a project I've done.... Let's expose it: I've two projects. Both of them use a Form to do some Gui stuff. Other threads pack up messages...
1
by: pitjpz | last post by:
We have moved our Database to another server. The server it was on used SQL 4 and the new one its on now uses SQL5 the only problem we can find is that when you attempt to delete a record from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
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,...

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.