What is the int/long version of strlen()?
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.
If you''ve got a local array it''s possible through the sizeof operator.
But normally it doesn''t, you''ll have to keep track of it yourself.
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.
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:
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]
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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement