Why char* Name = "string"; is not working in C++? [closed] - c++

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);

Related

Invalid conversion from "const char *" to "char *" in strcat() [closed]

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 3 years ago.
Improve this question
The following sample of code
...
#include <cstring>
#include <set>
constexpr int BUFFER_MAX_SIZE = 1024;
struct connection
{
...
char inputBuffer[BUFFER_MAX_SIZE];
char outputBuffer[BUFFER_MAX_SIZE];
};
int main(int argc, char **argv)
{
...
std::set<connection> clientConnections;
...
for (auto &clientConnection : clientConnections)
{
char buffer[BUFFER_MAX_SIZE];
...
strncat(clientConnection.inputBuffer, buffer, BUFFER_MAX_SIZE);
...
strncat(clientConnection.outputBuffer, buffer, BUFFER_MAX_SIZE);
...
}
}
causes the error that is in the title for both strncat() calls. I have no idea why does it happen because as you can see inputBuffer and outputBuffer members are NOT declared as constant. Besides before I have made some changes in the code it worked fine. Can something else affect on this?
I suppose those lines are enough but I could add more ones later if required.
Looks like you have the order of the source and destination buffers reversed in your call to strncat()
char * strncat ( char * destination, const char * source, size_t num );

Convert int to char of int value [closed]

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;

Modify a char* in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to modify a c string in C++.
void modify(char* s)
{
s[0] = 'a';
}
If I do this, there will be some undefined behavior and can't run.
Let's assume s[0] is valid. I know char* s is immutable. Is there any possibility that I can modify the s[0] in place, which means, without creating a new string. Do the modification on the original string.
I think you might be misunderstanding some other answers you have seen on the web.
It is only undefined behavior to modify a string constant, not any char*.
As long as you strdup the constant string into a non-constant string, you can make whatever changes you want with it, because it is now in a mutable region of memory.
#include <stdio.h>
#include <string.h>
void modString(char* changeMe) {
changeMe[0] = 'g';
}
int main(){
char* foo = strdup("food");
puts(foo);
modString(foo);
puts(foo);
free(foo);
}

find bugs in the c++ code below [closed]

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.

Char pointer in c++ [closed]

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.