Home > Mobile >  finding size of char array in C
finding size of char array in C

Time:10-29

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.

CodePudding user response:

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.

CodePudding user response:

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 .

CodePudding user response:

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)

  • Related