-1

In my program, I have a char array (char data[]) from which i have to extract the data and store it in an integer variable(value). To do so, I have used atoi in my program. Problem is that I sometime receive null in the char array(data). If i use atoi on this value, I get the value 0 in integer variable instead of (null)

The real function is quite big and I cannot post the entire code here. From function mentioned below, I hope you guys will get some idea about what I am doing here.

Get_Data(char data[])
{
    int value;
    value = atoi(p);
}

I recently read that we should use strtol instead of atoi. I tried that too. but I am still getting 0 in variable value.

I want to know which function should i use so that i get (null) in integer variable value?

By null, I mean empty character array here

Please provide solutions specific to the problem. In the above function, if data gets empty char value, how do i make sure that my int variable value also gets empty value and not zero??

Kung-fu-panda
  • 47
  • 1
  • 1
  • 8

3 Answers3

3

atoi return zero for

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.

Also you requrement for returning NULL is not possible because you assign those value in int and int can't hold not NULL.

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
  • Except that on most machines, an `int` can hold `NULL`; `NULL` is a "null pointer constant", not a null pointer. – James Kanze Jul 21 '14 at 10:14
  • @JamesKanze you say in this sense `int *ptr = NULL;` right? – Jayesh Bhoi Jul 21 '14 at 10:17
  • 1
    Sort of. What I'm really saying is that `int i = NULL;` is perfectly legal, and with most compilers, won't even elicit a warning. (g++ is the only compiler I know of which warns.) – James Kanze Jul 21 '14 at 10:21
  • NULL is defined as 0 in most implementations. – MVTC Jul 21 '14 at 11:27
  • 1
    @JamesKanze You're talking meaningless corner theories. Once you store `NULL` in an `int`, it becomes just another number and ceases to be `NULL`. In most sane implementations we even know that number is `0`. If you know you have `NULL` data and that's at all important to you, use an `if` statement for goodness sakes. (Oh, and it's **undefined** to pass `NULL` to `atoi()` even though it'll likely return `0` 99.9% of the time it could also SEGFAULT, so yeah...) – ebyrob Sep 14 '17 at 17:31
  • Did anyone here has experienced that sometime the function `atoi` generates SIGSEGV and sometimes the value 0 ? – Krishna Oza Sep 05 '18 at 13:43
  • @darth_coder yes, I just had it seg faulting on my raspberry pi, but not on Ubuntu x86_64. (I did not check the actual return value on the latter, though.) – Konrad May 13 '19 at 22:16
2

Because atoi tries to read as much as it can.

You should use strtol instead , like this:

char* endptr = NULL
char* myString = "OUPS";
long n = strtol(myString, &endptr, 10);

if (*endptr != '\0') {
  // ERROR
}

Read the reference link for more explanation (and example too).

NoDataFound
  • 11,381
  • 33
  • 59
  • Another way would be to check endptr == myString since it gets set to whenever no conversion took place (https://en.cppreference.com/w/cpp/string/byte/strtol). – Entalpi Jan 14 '22 at 14:01
1

Isn't that good?

There is no Integer value in C++ spelled as null

NULL in an integer form is 0 only.

foobar
  • 2,887
  • 2
  • 30
  • 55