Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am running the below code. I am getting run time error.
#include <iostream>
using namespace std;
int main() {
char *p="hello";
//p="Hi";
*p='G';
cout<<*p;
return 0;
}
if this is giving error then what is use of const char *p="hello";In this case my string should be constant not in char *p="hello"
char *p="hello";
*p='G';
You make p point to a constant, "hello". But then you try to modify what p points to. By definition, constants cannot be modified.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
When I write a function like below and use it in main(), it doesn't work.
Why does this happen? (I am a beginner).
void addBst(char *name, char *num);
int main(void)
{
addBst("a", "b");
return 0;
}
In C++, a string literal is a const char[N] array, where N is the length of the string, including the null terminator.
Since C++11, it is illegal to assign a string literal to a non-const char* pointer, as your code is doing. You need to use a const char* pointer instead, eg:
void addBst(const char *name, const char *num);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm studing about c++ semantics and syntax, I really don't know what is the problem with this code, it compile but stop working. I will apreciate your help, thanks.
#include <iostream>
#include <string.h>
using namespace std;
char* func(char* M)
{
int initval = 2;
char *x= new char[10];
x="idea";
strcpy(x, M+initval);
return x;
}
int main()
{
char* x;
char s[10]= "alguna";
x= func(s);
cout << *x << endl;
return 0;
}
Before this is closed, the x="idea"; is where your problem lies. You throw away your buffer and point it to a constant value, then try to assign to it, which almost always is illegal (should always be illegal, but apparently it is compiling for you...).
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to find a way to convert an int to char of the corresponding value of the int (assuming int is one digit). (example 1='1' 5='5' 9='9') I've tried
int a=5;
char b=char(a+48);
whenever I try to run this the program crashes. How can I set up a system that works correctly?
It can be done using the following code:
char c = (char)(48 + a);
You can also use the '0' char value, instead of 48. It will improve code readability and let you not remember the value 48:
int a = 5;
char c = (char)((int)'0' + a);
As mentioned in comments, you can do this without explicit casts:
char c = '0' + a;
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have been asked to find bug in the following code
This code just prints abcdef, it seems pretty harmless to me but will appreciate any suggestions
#include <iostream>
using namespace std;
const char* append(const char* s1, const char* s2) {
string s(s1);
s += s2;
return s.c_str();
}
void foo() {
const char* total = append("abc", "def");
cout<<"total = "<<total<<endl;
}
int main() {
foo();
return 0;
}
You have a dangling pointer. s is destroyed when it goes out of scope, meaning the pointer to its internal data is no longer valid.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
XO Game C++
This code returns only "It’s a Tie !!" .. What's the error in my code?
#include <stdio.h>
int main()
{
char a1,b1,c1,a2,b2,c2,a3,b3,c3;
scanf("%c%c%c\n%c%c%c\n%c%c%c",&a1,&b1,&c1,&a2,&b2,&c2,&a3,&b3,&c3);
if(a1==b1==c1=='X'||a2==b2==c2=='X'||a3==b3==c3=='X'||a1==a2==a3=='X'
||b1==b2==b3=='X'||c1==c2==c3=='X'||a1==b2==c3=='X'||c1==b2==a3=='X'){
printf("X wins\n");
}
if(a1==b1==c1=='O'||a2==b2==c2=='O'||a3==b3==c3=='O'||a1==a2==a3=='O'
||b1==b2==b3=='O'||c1==c2==c3=='O'||a1==b2==c3=='O'||c1==b2==a3=='O'){
printf("O wins\n");
}
if(a1!=b1!=c1||a2!=b2!=c2||a3!=b3!=c3||a1!=a2!=a3
||b1!=b2!=b3||c1!=c2!=c3||a1!=b2!=c3||c1!=b2!=a3){
printf("It’s a Tie !!\n");
}
}
a1==b1==c1=='X'
This doesn't do what you think it does. It compares a1 to the boolean result of b1==c1=='X', probably giving false.
To check whether they are all equal, you need
a1=='X' && b1=='X' && c1=='X'