NULL constant is actually 0, like #define NULL 0 . There isn't any standard alternatives in native C++, aside from using proper C++ functions insteod of legacy C-like ones.
You might try strtol http://www.cplusplus.com/reference/cstdlib/strtol/
Although this too returns zero for invalid input, you can check the value of the endptr parameter afterwards, to deduce how many characters were consumed in the conversion, and compare it with the length of the original string, using strlen().
Or better, use c++ stream operations, e.g. stringstream.
If you are using a C11 compliant compiler you may also be able to use the stox() series of functions. These functions throw an exception you can catch if they have an error in processing the string.
How about int to char. You can compare char, so if i turn my int to char I can make an if statement. So my question is. Is there a int to char function of some sort in standard c?
char besede[3250][9]; //besede[4] is a char '-'
int znak = atoi(besede[4]); //transform char to int
char ch = znak; //make that int a char again
if(besede[4] == ch){ //check if a new char is the same as char before
printf("Yes");
}
else{
printf("No");
}
I get: forbids comparison between pointer and integer error.
This doesn't seem to work either. It returns No every time even if besede[i] == 0.
1 2 3 4 5 6 7 8 9
int znak = atoi(besede[4]);
char ch[1];
sprintf(ch,"%s",znak);
if(besede[4] == ch){
printf("Yes");
}
else{
printf("No");
}
int znak = atoi(besede[4]); //transform char to int
char ch = znak; //make that int a char again
if(besede[4][0] == ch){ //check if a new char is the same as char before
printf("Yes");
}
else{
printf("No");
}
But it always returns No even if besede[i][0] == 0;
int znak = atoi(besede[4]);//znak equals to 8 (if we lucky and there is \0 in besede[4][1])
char ch = znak;//ch equals to 8 or BACKSPACE symbol from ascii table
if(besede[4][0] == ch)//No '0' != BACKSPACE
#include <cstdio>
#include <cstring>
#include <cstdlib>
usingnamespace std;
int main()
{
char str[9] = "76543";
// Convert char string to int
int num = atoi(str);
printf("num = %d\n", num);
// Convert int to char string
char buf[30];
sprintf(buf, "%d", num);
printf("buf = %s\n", buf);
// Compare original string with new string
if (!strcmp(str, buf))
printf("strings are equal\n");
else
printf("strings are NOT equal\n");
return 0;
}