0

How to get length or size of array?

i have defined array string array string s[] = {"Physics","Chemistry","Maths","English","Hindi"}; yes we know its 5 but how to get in cpp and assign to int veriable thanks.

29th Jan 2017, 12:00 AM
Santosh Gawali
Santosh Gawali - avatar
5 Respostas
+ 1
try this int counter = 1; while (s!=NULL){ counter++; s++; }
29th Jan 2017, 12:05 AM
Baran
Baran - avatar
+ 1
(sizeof(s)/sizeof(*s)) Only works for 'real' arrays, not pointers.
29th Jan 2017, 12:08 AM
Robobrine
Robobrine - avatar
+ 1
That's because you need (sizeof(s)/sizeof(*s))
29th Jan 2017, 3:33 AM
Robobrine
Robobrine - avatar
0
int main() { string s[]={"Physics","Chemistry","Maths","Hindi","English"}; cout <<sizeof (s)<<endl ; j int counter =1; while (s!=NULL) {counter ++; s++;} cout <<s<<endl ; return 0; } friends i ve tried above but sizeof not giving 5 & counter is giving error thanks
29th Jan 2017, 3:31 AM
Santosh Gawali
Santosh Gawali - avatar
0
thanks Robobrine, it worked. i was trying sizeof(s); only. thanks both.
29th Jan 2017, 3:47 AM
Santosh Gawali
Santosh Gawali - avatar