8

Given an array of strings, i need to find out the number of strings in it.

I followed this

but this doesn't work if i am passing this into a function.

here's the code i tried

#include<string>
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

int f1(char* input1[])
{

    string s="";

    cout<<sizeof(input1)<<endl; //print 4
    cout<<sizeof(char*)<<endl;  //print 4
    int l=sizeof(input1) / sizeof(char*);
    //giving l=1 here but should be 8
}

int main()
{
    char *str2[]={"baba","sf","dfvf","fbfebgergrg","afvdfvfv","we","kkhhff","L"};
    int l=sizeof(str2) / sizeof(char*);
    cout<<l<<endl; //print 8
    cout<<sizeof(str2)<<endl; //print 32
    cout<<sizeof(char*)<<endl; //print 4
    f1(str2);
}
Community
  • 1
  • 1
prashantitis
  • 1,797
  • 3
  • 23
  • 52

2 Answers2

8

sizeof(char*) gives you the size of the char* pointer (which is 4 on your system).

sizeof(str2) is giving you the size of the array str2. There are 8 elements, each one is a pointer type. So the total size on your system is 8 x 4 = 32.

To get the length of a string, use strlen.

Do consider std::vector<std::string>> as an alternative in C++.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • this is not working strlen(*input1) again giving 4 as output in function. – prashantitis Apr 17 '15 at 09:16
  • 2
    @Guru That's because `input1` is not an array. It's `char**`. Please read [How to use arrays in C++](http://stackoverflow.com/questions/4810664/how-do-i-use-arrays-in-c), especially the array-to-pointer conversion part. – jrok Apr 17 '15 at 09:19
  • @jrok:so what should i do to get the output 8 in function?? – prashantitis Apr 17 '15 at 09:21
  • @Guru you need to pass the size as a parameter to the function. Check the link in my previous comment. – jrok Apr 17 '15 at 09:22
  • @jrok: actually it's an online contest question where i need this functionality. and there i cannot modify the given function prototype. isn't there other way by which i don't need to modify the prototype of f1 – prashantitis Apr 17 '15 at 09:25
  • 1
    @Guru If you're not allowed to change the prototype, then either you have a sentinel value to mark the end of the array, or the question is unsolvable. – Quentin Apr 17 '15 at 09:50
1

You can not know the length of an array if you only have a pointer to it. And you do have only a pointer because you cannot pass arrays by value. Arrays passed to a function will automatically decay to a pointer and the argument type char* foo[] is equivalent to char** foo. size_of doesn't help because it will only tell the size of the pointer itself.

Pass the length as an argument to f1. Or better yet, use std::vector or std::array.

i cannot modify the given function prototype

Well, that's unfortunate. Then you must resort to some trickery. The simplest workaround is to store the length in a global variable instead of a function parameter.

Another possibility is a terminating value For example, always end the array with nullptr and never allow other elements to have that value. In the same way as c-strings are terminated with null character. Then you can stop iterating the array when come across nullptr. But I assume you cannot modify the array either.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • that i know very well but actually it's an online contest question where i need this functionality. and there i cannot modify the given function prototype. isn't there other way by which i don't need to modify the prototype of f1 – prashantitis Apr 17 '15 at 09:28
  • @Guru if you know that very well, then why are you asking why `size_of` doesn't work? Anyway, you can resort to a global instead of a parameter. – eerorika Apr 17 '15 at 09:33