Reputation: 1744
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.
Upvotes: 1
Views: 6979
Reputation: 14510
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);
Upvotes: 0
Reputation: 1506
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];
}
Upvotes: 0
Reputation: 11058
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.
Upvotes: 2
Reputation: 9071
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.
Upvotes: 1