• Advertisement

Archived

This topic is now archived and is closed to further replies.

What is the int/long version of strlen()?

This topic is 5010 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

Good morning, how can I find out what is the last element in an int/long array? How can the computer know when one integer array begins, and when another ends? In strings there''s the null terminator(0) but all I found were random addresses for int/long arrays, or the address of another int/long array i allocated earlier in the program.

Share this post


Link to post
Share on other sites
Advertisement
If you''ve got a local array it''s possible through the sizeof operator.


int list[] = { 1, 2, 3 };
int count = sizeof(list) / sizeof(list[0]);


But normally it doesn''t, you''ll have to keep track of it yourself.

Share this post


Link to post
Share on other sites
Well if your talking about C strickly, there isn't way to tell from the array alone it's size except for as you said your self C style strings end with the null character '\0'. I guess this has something to do with with passing arrays in a function if thats the case you should also provide another parameter that will tell it's size like:

C style:


#include <stddef.h>
void foobar(int array[], size_t size);


If you don't like this then you could make structure that has a pointer to some type and the size of the array when constructed, then make some functions that will create, destroy, manipulate them.

If your using C++ then its been recommended to avoid using raw dynamic arrays use a vector it provides a methods to find out stuff like it's size etc.

[edited by - snk_kid on June 9, 2004 5:26:47 AM]

Share this post


Link to post
Share on other sites

  • Advertisement