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.
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 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 );
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
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.
Improve this question
I know this warning have been asked many times. But I can't think of how to edit my code.
I only include the code on the error/warning part.
const unsigned char *ad[100];
unsigned long long ad[100];
int main
{
adlen = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);
if(adlen > 0)
{
ad[i] = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
i++;
adlen--;
}
}
After compiling, I will get a warning.
warning: assignment makes pointer from integer without a cast
ad[i] = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
^
I also tried searching online for another way to declare.
const unsigned char *ad; //or const unsigned char *ad = malloc(100);
unsigned long long ad[100];
int main
{
adlen = CDC_Device_BytesReceived(&VirtualSerial_CDC_Interface);
if(adlen > 0)
{
ad[i] = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
i++;
adlen--;
}
}
But I will end up getting an error
error: assignment of read-only location '(ad + (sizetype)((unsigned int)i * 1u))'
ad[i] = CDC_Device_ReceiveByte(&VirtualSerial_CDC_Interface);
^
I cannot change and have to declare const unsigned char *ad as a pointer so I have to probably add/change something to my code in int main but I cannot think of how/what to do.
I will be glad if anyone can help/guide me on what to do with this warning and error. Thank you!
The function CDC_Device_ReceiveByte returns a value of type int16_t so you need
int16_t ad[100];
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 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.