I'm trying to use strcpy in order to put a string in an array of strings. This is my definiton of the arrays:
char movies[10][150], movie[150];
int i = 0, j = 0;
currentChar = getchar();
while(currentChar != EOF)
{
while(currentChar!='\n')
{
movie[i] = currentChar;
currentChar = getchar();
i++;
}
strcpy(*(movies + j*10*15), (char*)movie);
j++;
currentChar = getchar();
}
Trying to debug it in c++ console, I get a 'Buffer is too small' message.
note: My input is "Django unshaved:a bit bloody, but overall a solid film" which does not exceed the 150 chars.
thanks for helping.
edit: code edited to show full picture
You should use this code instead:
strcpy(movies[j], movie);
IMHO, it doesn't make sense to define a two-dimensional array and then use it as if it was one large one-dimensional array defined as char[1500].
Also, which compiler are you using that generates such a message for C
-code?
您的复制函数调用应为:
strcpy((movies + j*150), (char*)movie);
Finally I solved the problem. Apparently, in the end of 'movie' there wasn't a '\\n' character and strcpy could not handle it.
Adding a '\\n' at the end of the string solves the problem.
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.