-1

I am getting the char array from user and trying to find the size of it and it is not working somehow.

My code looks like this:

int main()
{
    char str[] ={}
    cout << "Enter a characters ";
    cin >> str;
    int arrSize = sizeof(str);
    cout << arrSize;

    return 0;
}

When I define array like code below, it will work:

int main()
{
    char str[] ={"1234"}
    int arrSize = sizeof(str);
    cout << arrSize;

    return 0;
}

I am not used to C++, please someone help me.

Sho
  • 3
  • 1
  • 4
    Size would be defined by that line: `char str[] ={};`, using `std::string` would be simpler. – Jarod42 Oct 28 '21 at 10:00
  • 1
    For strings, *always* use `std::string`. If you want a collection of other data, then if the size is known at compile-time and will never change use `std::array`. Otherwise use `std::vector`. – Some programmer dude Oct 28 '21 at 10:03
  • 1
    `sizeof` is not a function. It's an operator that gives you the size of an object known at _compile time_. Read this: https://en.cppreference.com/w/cpp/language/sizeof, or read about the sizeof operator in your learning material. – Jabberwocky Oct 28 '21 at 10:04
  • Also, `char str[] ={}` is invalid. Not only because of the missing semicolon, but also because all arrays *must* have a size of at least one element, and you attempt to make the size zero. you also then attempt to add data to the array, but arrays have a fixed size and are not dynamic. – Some programmer dude Oct 28 '21 at 10:05
  • your first example is undefined behaviour and shouldn't even compile, since 0-size arrays are not allowed – Raildex Oct 28 '21 at 10:05
  • Do not mix `C` with `C++`, just use `std::string`. – Marek R Oct 28 '21 at 10:12
  • If a C++ routine's parameter is a char array, it actually receives just a pointer to where its located in memory, and you can't tell its size. If the string is null terminated, strlen will return the content's length. If not, you'll get an invalid value or segmentation fault. Which is why other responders have recommended using an std::string. – Uri Raz Oct 28 '21 at 10:14

3 Answers3

1

Welcome to the wonderfull world of c++ stdlib. If using c++ better use whole strength of stdlib.

string str;
std::getline(cin, str);

then you can use the str.size() to get its length. Lookup cppreference.com for any help with stdlib functions and classes.

Marc-André
  • 31
  • 1
  • 4
1

There are two mistakes in your given code snippet:

Mistake 1

In C++, the size of an array must be a compile time constant. So you cannot write code like:

int n = 10;
int arr[n];    //incorrect

Correct way to write this would be:

const int n = 10;
int arr[n];    //correct

For the same reason the following code is incorrect in your code as well:

char str[] ={};//this defines(and declares) an empty array. This statement is not fine(that is it is incorrect) because we cannot have 0 size arrays in c++

cin >> str;  //incorrect because a built in array is fixed size container and you cannot add more elements(than its size) to it(both by user input or by the programmer itself)

Solution to Mistake 1

char str[100] ={}; //this defines an array of fixed size 100. 
cin >> str; //this is correct now, but note that you can only safely enter upto 100 characters. If you try to add more than 100 than this will also become incorrect

Mistake 2

You're calculating the size of the array in the wrong way. The correct way would be :

int arrSize = sizeof(str)/sizeof(char);// since sizeof(char) = 1 you can omit the denominator but note you can omit it only for char type

Using the correct formula sizeof(str)/sizeof(char) is important for array of other types like double, int etc. This is the general formula. But since sizeof(char) = 1; so the formula that you used is correct(only for char). So if you have an array of double then you should use sizeof(str)/sizeof(double);.

Also, note that you can/should instead use std::string to take input from the user and then use size() method to calculate how long the input was like:

   std::string str;
   std::cin >> str;
   std::cout<<"size is "<<str.size()<<std::endl;

Note you can also use std::size in C++17 for finding the size of the array.(pointed out by @eerorika in the comments below)

Jason
  • 36,170
  • 5
  • 26
  • 60
  • `sizeof(char)` is defined by the standard to `1`, no need to divide by `1`. – mch Oct 28 '21 at 10:24
  • @mch Using the correct formula is important when we have arrays to other types like double, int etc. For `char` the formula used by the OP is okay. – Jason Oct 28 '21 at 10:27
  • `this defines(and declares) an empty array. This statement is fine` It's not fine. Empty array variables aren't allowed in C++. – eerorika Oct 28 '21 at 11:35
  • `then you should use sizeof(str)/sizeof(double);` You shouldn't use that. Use `std::size(str)` instead. – eerorika Oct 28 '21 at 11:39
  • @eerorika Are there any disadvantages of using `sizeof(str)/sizeof(double);` ? It (`std::size`) is introduced in C++ 17. So `sizeof(str)/sizeof(double);` will work on all C++ versions. – Jason Oct 28 '21 at 11:42
  • @AnoopRana Biggest disadvantage is the possibility of using the wrong type in the divisor and thus silently getting the wrong size. It's also less readable. It also works only with arrays, while `std::size` works also with containers so it can be use in a template that supports both. If you're stuck in old C++, you can write your own `size` template. It's trivial. – eerorika Oct 28 '21 at 11:55
  • @eerorika That is never a disadvantage. It is the responsibility of the programmer to write correct type in the divisor(just like other things in C++). Also `std::size` is an alternative that can be used on/with other containers. The programmer can decide whether to choose `sizeof(str)/sizeof(double);` or the more general `std::size` depending upon the requirements(like whether he/she is working on C++14 or C++17 etc). There is no reason to not use `sizeof(str)/sizeof(double);`. – Jason Oct 28 '21 at 12:08
  • @AnoopRana `It is the responsibility of the programmer to write correct type in the divisor` That's a huge disadvantage. Programmers are humans. Humans make mistakes. Assuming that won't be a problems is pure hubris. – eerorika Oct 28 '21 at 12:10
  • @eerorika `std::size` is surely better(more readable as you've said and it is also more general) than the old `sizeof(str)/sizeof(double);` way of finding the size of the array. – Jason Oct 28 '21 at 12:21
1

Arrays in C are static. After creating empty array of chars char str[] = {} you cannot fill it with arbitrary number of characters. Size of static C-style array is calculated in compile-time, as well as sizeof() operator. If you for some reason really have to use C-style string (array of chars), firstly allocate enough space in your array and than use strlen() function to determine string length.

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

int main()
{
    char str[256];
    cin >> str;
    int arrSize = strlen(str);
    cout << arrSize;
}

However since you are working with C++ (not C), it would be better to use std::string in your case.

#include <iostream>
using namespace std;

int main()
{
    string str;
    cin >> str;
    int arrSize = str.size();
    cout << arrSize;
}

Btw, you don't need return 0; in C++.

hant0508
  • 528
  • 1
  • 9
  • 19
  • Thank you so much for this! Using string and using strlen both solved my problem! I will continue working on the c++! – Sho Oct 28 '21 at 15:51