Sign in to follow this  

The table elements number

This topic is 4251 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering if there is a way to know the number of elements of a table...

Share this post


Link to post
Share on other sites
If you have a array

char array[32];

You can get the size like this

sizeof(array)

However, if you make a pointer to the array

char* parray = array;

and do this

sizeof(parray)

you will get the size of the pointer, not the array.

If you need to send an array as a function argument, the common way to do it is
to send the size of the array as well:

void my_func(char *array, int array_size)

If you have a array of ints, int array[32], you would have to get the size like this:

sizeof(array) / sizeof(int);

This is cumbersome at best. You should probably use a C++ vector instead:

vector<int> vec;

add a integer to the vector

vec.push_back(3);

and get the number of elements like this

vec.size();

To send the vector as an argument to a function, the prefered way to do it is

With write access:
void my_func(vector<int>& vec)

Without write access:
void my_func(const vector<int>& vec)

Share this post


Link to post
Share on other sites

This topic is 4251 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this