Solved

How to get the size of an array in a function

Posted on 1998-06-19
4
191 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now