Solved

How to get the size of an array in a function

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

772 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