Solved

How to get the size of an array in a function

Posted on 1998-06-19
4
214 Views
Last Modified: 2010-04-01
I have an array of a structure and this array is passed by reference to a function.  What do I have to do to know how many element does the array have ?  

I know usually, we pass the array size as parameter too but is it possible to figure it out ?

Here is how my code look like !

       struct TEST
       {
             int a;
             int b;
             int c;
             bool d;
       };

       void main()
       {
            void F1(TEST *);    

             TEST TstArray[10];
              F1(TstArray);
       }    

       void F1(TEST *TstArray)
       {
           // Here, what do I have to do to know how many element does the TstArray have ????
       }
0
Comment
Question by:tam031198
[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
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 30 total points
ID: 1166319
answer coming.
0
 
LVL 8

Expert Comment

by:trestan
ID: 1166320
You have to pass the array size to the function as well.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1166321
It is best to pass the array size as a parameter or to use some sort of semaphore value that indicates that it is the last item in the array.  In general, it is not possible to determine the size of an array given a pointer to it.

However, if you know that the array is dynamically allocated and if you know that the pointer you have points to the start of the array, then you can use _msize() do determine the amount of memory allocated for the array. You can divide this by the size of the array elements to get the number of items in the array.  However, I do not recommend this tecnhique  The two conditions that it relies upon tend to be too restrictive and eventually some code will violate those restrictions and you will have a bug.  

Pass a size or use a semaphore.
0
 

Author Comment

by:tam031198
ID: 1166322
Thank you very much !
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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

749 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