Invalid conversion from "const char *" to "char *" in strcat() [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 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 );

Related

Why char* Name = "string"; is not working 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 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);

How to copy char * to an array of 16 bytes [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 4 years ago.
Improve this question
How to copy char * to an array of 16 bytes.
const char *SK = "1234456789999978";
sample_aes_gcm_128bit_key_t alice[16];
memcpy(alice, (sample_aes_gcm_128bit_key_t*)SK, 16); //gives error
Definition of sample_aes_gcm_128bit_key_t
typedef uint8_t sample_aes_gcm_128bit_key_t[16];
Error:
error: expected constructor, destructor, or type conversion before ‘(’ token
memcpy(alice, (sample_aes_gcm_128bit_key_t*)SK, 16);
This works for me:
#include <iostream>
#include <cstring>
int main()
{
typedef int sgx_aes_gcm_128bit_key_t;
const char *SK = "1234456789999978";
sgx_aes_gcm_128bit_key_t alice[16];
std::copy(SK, SK + strlen(SK), alice);
for(auto& i : alice){
std::cout << i - '0';
}
return 0;
}
Take care that your typedef name matches the name you use. (It didn't in question.) And you can remove the cast to sgx_aes_gcm_128bit_key_t. Hopefully this accomplishes what you're trying to do.
If you want to use memcpy with C++, that is fully possible. Inside memcpy you should use casts to (void*)
memcpy((void*)alice, (void*)SK, 16);
Make sure to get types and size right to prevent segmentation fault.

Need help whit c++ pointers [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 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...).

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;

reinterpret_cast double to char* [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I reinterpret cast from double to char* (I need it to store the data of double in file in bytes). Below is the code and I don't know why it doesn't work:
#include <iostream>
int main(int argc, char **argv)
{
const double tmpDouble = 1234.;
char *tmpChar = reinterpret_cast<char*>(tmpDouble);
return 0;
}
If what you had there worked, it probably wouldn't be what you want - the pointer's value would just be 1234 - effectively pointing to that address which might contain anything (not that it's accessible).
If you just want to have the double in binary format, you could do
const byte* pDouble = reinterpret_cast<const byte*>(&tmpDouble);
// |
// note the address here
But first check whatever it is you're using to write to file for a function prototype that takes a double directly.