Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
if char is only 1 byte (8bit, 255 max size), how 65534th char can be saved in char ?
#include <iostream>
int main() {
unsigned wchar_t a = 65534;
char b = (char)a;
std::cout << b << std::endl;
return 0;
}
also in java you can write byte b = 5000;
do anyone knows why ?
For the C++ code, the wchar_t value gets truncated to fit in the char variable. Normally there would be a warning from the compiler about this loss of data, but by using the C-style cast you've told the compiler "don't worry, I know what I'm doing" and taken responsibility for the consequences of your (questionable) actions.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
When normally comparing the string this way:
#include <iostream>
using namespace std;
int main()
{
string a = "yb";
string b = "ya";
cout<<(a>b);
return 0;
}
result comes out 1. Which is right.
But when performing the same operation in this way:
#include <iostream>
using namespace std;
int main()
{
cout<<("yb">"ya");
return 0;
}
result is coming out 0.
How is this possible?
"yb" and "ya" have type char const[3]; they are arrays located somewhere in the memory containing the chars in the string literal and the terminating 0 char.
When doing ("yb">"ya") those objects decay to char const* and you're comparing pointers. Where the data is stored is compiler implementation defined and you cannot rely on the result.
To compare std::strings, you'd need to write
std::cout<<(std::string("yb")>std::string("ya"));
instead.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I need to compare a character in a string that is in an array with another string. This is a functioning but simple version of my problem:
#include <iostream>
using namespace std;
int main() {
string a_ray[1] = {"asd"};
if (a_ray[0][0] == "a") {
bool a;
}
return 0;
}
Error message: ISO C++ forbids comparison between pointer and integer [-fpermissive]|
What causes this? And how can I do what I want to do in the correct way?
Thank you in advance!
Since you are comparing against a character, your code should be
if (a_ray[0][0] == 'a')
You are trying to compare a character with a character array, hence the error message.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int* i = new int(75);
double* d = new double(3.14159);
printf("%d\n",*i);
printf("%d\n",*d);
}
In the above code i returns a value of 75 however, d returns 1.
I tried explicitly initializing it as
*d = 3.14159
But the value is still returned as 1.
Can anyone explain what I am doing wrong here?
Use this for printing.
cout<<*i;
cout<<*d
"%f" is the (or at least one) correct format for a double if you want to use printf for printing the value of the double in C++.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Here is the code:
bool Vehicle::checkID(std::string id)
{
std::vector<int> digits;
for (char c : id)
{
if(std::isdigit(c))
{
digits.push_back(atoi(c));
}
else
{
digits.push_back(int(c));
}
}
I don't know why he throws this error for "digits.push_back(atoi(c))".
I'm a very beginner, I know this will be not that difficult for you.
You can't do:
atoi(c)
atoi() expects a char *. You probably want
digits.push_back(c - '0');
The function atoi() takes a single const char * type as a parameter.
You're calling it with a char parameter. The compiler doesn't know how to convert from one to the other.
atoi actually wants a string as input, you can't call it with a single character. A string always needs a zero character as a terminator.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm working on trying to create a CRC using data bytes, and I have written this function:
u16 crcGenerate(unsigned char bytes, int len){
u16 crc = 0;
for (int i = 0; i < len; i++){
crc = crc16__computeByteAnsi(crc, bytes[i]); }
return crc;
}
I keep getting the above error whenever this function is called, and I'm not quite sure how to fix it, or even what's wrong.
To avoid bloating this question, I've included the other primary .h and .cpp files that are referenced.
This is the Command header file (cmgCOM.h)
This is where the CRCs are computed (crc16.cpp)
This is the primary Command file (cmgCOM.cpp)
I really appreciate any help that I can get; if there's any more information that needs to be provided, let me know. I'm not very familiar with C/C++, and I don't know what's causing this issue.
Thanks.
I think you intended bytes to be an array of unsigned char but you only decalred it as a single unsigned char value.
u16 crcGenerate(unsigned char bytes[], int len){