below is my code
#include <stdio.h>
#include <string.h>
#include <iostream>
int main()
{
std::string data;
data = "hello world";
char string[] = new char [data.length()+1];;
strcpy(string, data.c_str());
}
I got an error..
file.cpp: In function ‘int main()’:
file.cpp:14:46: error: initializer fails to determine size of ‘string’
What should i do as I want copy the content of string data into char string[]
Thanks for helping.
更改为以下内容:
char* string = new char[data.size()+1];
Use char *string
, not char string[]
.
The char string[]
means that you define an array , and size of the array should be evaluated from the initializer in compile-time.
In order to get it to compile, you'd have to use a char*
instead of a char[]
. char[]
in this case requires a constant size, and since data
is not a compile-time constant, the size cannot be determined.
Try something like this :
string Name="Hello FILE!"
int TempNumOne=Name.size();
char Filename[100];
for (int a=0;a<=TempNumOne;a++)
{
Filename[a]=Name[a];
}
You should do :
char* string = new char[data.length()+1];
// ^
strcpy
take two pointers as argument. char string[]
means you are declaring an array.
Here is the prototype of the function :
char *strcpy(char *dest, const char *src);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.