Solved

Finding array length

Posted on 2004-08-04
4
33,432 Views
Last Modified: 2011-08-18
Hi!

I am currently getting frustrated about a problem I have been thinking about for ages.

If I have a function:

void in_array_cp(char *haystack[],char *needle)
{
...
}

which is supposed to find out if an array (haystack) contains a certain element(needle), I need to know the number of elements in the array to be able to scan through it. Is there a way to find the number of elements in the array or do I need to pass it as another parameter?

Thx
by the way: I am using GCC3.3.1 on SuSE Linux9
0
Comment
Question by:Professor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 11713153
1. No way
2. Yes, you need another parameter to pass the # of elements.
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 125 total points
ID: 11713162
Hi Professor,

> Is there a way to find the number of elements in the array or do I need to pass it as another
> parameter?

Number of valid elements -> pass another parameter
Array capacity -> if actually declared as array then sizeof(array)/sizeof(element)
               -> if declared as pointer and malloced then again pass another argument

Sunnycoder
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11713172
Yes, but only if you make some assumptions.

You could assume that the las entry in the haystack is NULL.

You could assume a fixed number of entries in haystack.

You could assume that haystack has been allocated on-heap.

I cant think of any other reliable way to tell without making assumptions.

The required code should be obvious for each of the above assumptions but if you need help, just ask.

Good luck.

Paul
0
 

Expert Comment

by:Vikram_B
ID: 14811679
Yes, as PaulCaswell  says, keeping a NULL at the end of your haystack[]  seems to be a better idea to me. this will allow you to determine the length of array by keep counting the number of elements until a NULL occurs.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question