0

I want to use the numbers that strlen() returns in a function that requires an int value.

functionName((strlen(word)+strlen(otherWord)+1));

This piece of code doesn't work because they return size_t.

Is there any way to convert that result to int so I can make addition operations and use them as int?

Basa
  • 101
  • 3
  • 9
  • 2
    You can use a `static_cast()`. – πάντα ῥεῖ Feb 28 '14 at 17:34
  • Thanks for answering. Unfortunately I believe I've asked the question wrong because your solutions don't work. The error I (still) get is _'strlen' was not declared in this scope_. @Cyber – Basa Feb 28 '14 at 17:51
  • Use either `#include `, or even better `#include ` and use `std::strlen()`. – πάντα ῥεῖ Feb 28 '14 at 18:00
  • I was using _string_, it works with _cstring_ and with or without static cast. I just declared it like this using cstring: `int lungimeNumeSiPrenume=strlen(nume)+strlen(prenume)+1;` – Basa Feb 28 '14 at 18:08

6 Answers6

5

Most of the other answers are using C-style casts which you shouldn't do in C++: You should static cast.

functionName( static_cast<int>((strlen(word)+strlen(otherWord)+1));
Community
  • 1
  • 1
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
2

First you need to check if you can add the values, then you need to check if you can cast to int, and the you cast..

sizt_t size = strlen(word);
if (std::numeric_limits<size_t>::max()-size >= strlen(otherword))
{
    size += strlen(otherword);
    if (std::numeric_limits<size_t>::max()-size >= 1))
    {
        size += 1;
        if (size <= std::numeric_limits<int>::max())
        {
            functionName(int(size));
        }
        else
        {
            //failed
        }
    }
    else
    {
        //failed
    }
}
else
{
     //failed
}
KoKuToru
  • 4,055
  • 2
  • 20
  • 21
0

Unless you have veeery long strings here it is safe to use cast:

functionName((int)(strlen(word)+strlen(otherWord)+1));

if you want you can use static_cast<int> but there is no differences now.

functionName(static_cast<int>(strlen(word)+strlen(otherWord)+1));
Avt
  • 16,927
  • 4
  • 52
  • 72
0

size_t is internally unsigned int on most sensible platforms. Typecast it to int and be done with it:

functionName(static_cast<int>((strlen(word)+strlen(otherWord)+1));

or C-style (which is discouraged but legal):

functionName((int)((strlen(word)+strlen(otherWord)+1));

How likely is it that the sum total of those lengths would be over 0x7fffffff (that's the limit of int on 32-bit platforms)? Probably not very likely.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
0

This piece of code does work, unless your compiler does something very strange or you haven't shown us the actual code.

The only thing that could possibly go wrong is an overflow error (unsigned ints can hold larger values than signed ints). You could check that with std::numeric_limits<int> if you really think your strings can become that incredibly large.

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
  • You were right - it does work only not with _string.h_, but with _cstring.h_. – Basa Feb 28 '14 at 18:10
  • 1
    You mean the other way round, don't you? And if you really use , replace that with or , even if your compiler allows . – Christian Hackl Feb 28 '14 at 18:12
  • It's without the ".h" (sorry about that), but I am using **c**string. `#include ` – Basa Feb 28 '14 at 18:16
  • 1
    OK... so, the difference between and (and equivalent cases where C++ uses old C headers) is that the latter puts all names also in namespace std, i.e. will allow you to write "std::strlen" instead of "strlen". At least if your compiler is standards-compliant... – Christian Hackl Feb 28 '14 at 18:19
0

In C++ use otherWord.length(): functionName( word.length() + otherWord.length() ).

This is one way to do a strlen() of functionName((int)(strlen(word)+strlen(otherWord)+1));.

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

int functionName(int rstr )
{
    return rstr;
}




int main ()
{
    char word[]      = "aaa";
    char otherWord[] = "bbb";

    cout<< "word =  "<<word<<"\n" ;
    cout<< "otherWord =  "<<otherWord<<"\n" ; 

    cout<<  "The sentence entered is   "<<  functionName((int)(strlen(word)+strlen(otherWord) ))<<"   characters long \n\n";

  return 0;
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28