Solved

How to get the size of an array in a function

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

708 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

16 Experts available now in Live!

Get 1:1 Help Now