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 years ago.
Improve this question
I am trying to find the occurrence of characters of one string(s1) in other string(s2).
This is part of my code.
for(;i<strlen(s1);i++)
{
int x=strchr(s2,s1[i]);
if(x>0)
count++;
}
But on compiling I get an error pointing to strchr() and says
error: invalid conversion from ‘char*’ to ‘int’ [-fpermissive]
Anyone explain what is problem in using strchr() function.
Assignment is wrong strchr doesn't returns int but returns address of first char in string found:
int x=strchr(s2,s1[i]);
should be:
char* x = strchr(s2, s1[i]);
// ^ returns char*
Read manual
char *strchr(const char *s, int c);
RETURN VALUE
The strchr() and strrchr() functions
return a pointer to the matched character or NULL if the character
is not found. The terminating null byte is considered part of the
string, so that if c is specified as '\0', these functions return a
pointer to the terminator.
And so:
if(x>0)
should be:
if(x != NULL)
or just if(x)
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 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
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 7 years ago.
Improve this question
I have a user defined class, one of whose members is a char* type. When I try to initialize it in the constructor I get an error saying error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.
However, when I changed strcpy to strcpy_s, it would still give the following error IntelliSense: no instance of overloaded function "strcpy_s" matches the argument list argument types are: (char *, char *)
Let's say Student is the class and char* name; is one of the data members.So, my constructor is like:
Student (char* s = NULL) {
if (s != NULL) {
name = new char[strlen(s) + 1];
//strcpy(name,s);
strcpy_s(name,s);
}
}
It's because strcpy_s requires an additional parameter to specify how many bytes to copy.
See here: http://www.cplusplus.com/forum/beginner/118771/
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
#include<cstdio>
#include<cstring>
#include<cstdlib>
int main(){
int t;
scanf("%d",&t);
while(t--){
char *str;
scanf("%s",str);
int l,n,nop=1;
l=strlen(str);
printf("Length %d",l); //causing segmentation fault
if(l%2==0)n=l/2;
else n=(l-1)/2;
for(int i=0;i<n;i++)nop=nop+abs(str[i]-str[l-1-i]);
printf("%d\n",nop);
}
return 0;
}
I am getting error when I print the length of string. If I remove that line the code works but with incorrect output.
The code works fine with DevC++
You need to initialize str by allocating space for it:
char *str = (char *)malloc(SIZE);
Otherwise it will invoke undefined behavior. Also, declare l of size_t type because strlen returns type is size_t and change %d to %zu in printf
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.