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);
}
1
  • Do you know the compiler for which you have to solve that question? Commented Apr 17, 2015 at 9:51

2 Answers 2

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++.

Sign up to request clarification or add additional context in comments.

6 Comments

this is not working strlen(*input1) again giving 4 as output in function.
@Guru That's because input1 is not an array. It's char**. Please read How to use arrays in C++, especially the array-to-pointer conversion part.
@jrok:so what should i do to get the output 8 in function??
@Guru you need to pass the size as a parameter to the function. Check the link in my previous comment.
@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.
|
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.

2 Comments

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
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.