Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can i find out the length of an array?

Status
Not open for further replies.

pigsie

Programmer
Dec 19, 2000
307
GB
Hi how can i find out the length of an array of characters?


Kola
 
If you are using an array of char's, sizeof() works just fine since it returns the # of bytes:

char array[25];
std::cout << sizeof( array ) <<
std::endl;

Mike L.G.
mlg400@linuxmail.org
 
sizeof will give you the correct answer there but look at this

void PrintSize(char* array)
{
cout<<sizeof(array)<<endl;
}

main()
{
char array[25];
cout<<sizeof(array)<<endl;
PrintSize(array);

}


you will see 25 and 4 output from this. strlen(array) will give you the lenght of the string contained within the array but not the full size of the array. I dont know if that is what you where looking for.

Matt
 
Hi

Thanks for the reply I wanted the size of the array something which returns the length of the array. Perhaps
I am barking up the wrong tree so i'll tell you what i am trying to acheive. I need to be able to store some records in memory but give the user the ability to add/remove records etc. How i am doing this is by creating a copy of a an array every time a new record is added/deleted. I am trying to keep it simple.

Thanks
 
why not use a linked list instead ? makes it much simpler.
 
I dont know how to make a link list in c++ only java and with c++ im assuming that the link list would use pointers which i dont know much about. Also the reason i wanted to keep it simple was to teach my younger brother about c++ as hes currently trying to learn it (not that i know it but i can explain some of the concepts).

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top