strncmp/strcpy 破坏源代码
今天我试图与 char * string 变得友好......但似乎我失败了:) 每次我调用 strcmp/strncmp/strcpy 函数时,我的源代码都会损坏...
这是代码片段
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
int UID;
char name[20];
char surname[20];
};
char * getString(int minChars, int maxChars);
struct student * myStud;
int main(int argc, char** argv) {
myStud = (struct student*)malloc(sizeof(struct student));
while(1)
{
printf("\nEnter new name: ");
strcpy(myStud->name,getString(1,19));
printf("\n The values is now %s",myStud->name);
}
return (EXIT_SUCCESS);
}
char * getString(int minChars, int maxChars)
{
char string[maxChars+1];
scanAgain:
scanf("%s",&string);
if(strlen(string)<minChars)
{
printf("\nToo few symbols, try again: ");
goto scanAgain;
}
if(strlen(string)>maxChars)
{
printf("\nToo many symbols, try again: ");
goto scanAgain;
}
string[maxChars]='\0';
return(string);
}
输出:
Enter new name: Alekasdasd
The values is now Alekasda�#
Enter new name:
我只是一个初学者,所以它可能非常简单...可能不是。 哦,顺便说一句,使用linux和netbeans作为SDK,gcc作为编译器。
today i was trying to get friendly with char * string... but it seems im failing :)
Every time i call strcmp/strncmp/strcpy function my source gets corrupted...
here is the snippet
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
int UID;
char name[20];
char surname[20];
};
char * getString(int minChars, int maxChars);
struct student * myStud;
int main(int argc, char** argv) {
myStud = (struct student*)malloc(sizeof(struct student));
while(1)
{
printf("\nEnter new name: ");
strcpy(myStud->name,getString(1,19));
printf("\n The values is now %s",myStud->name);
}
return (EXIT_SUCCESS);
}
char * getString(int minChars, int maxChars)
{
char string[maxChars+1];
scanAgain:
scanf("%s",&string);
if(strlen(string)<minChars)
{
printf("\nToo few symbols, try again: ");
goto scanAgain;
}
if(strlen(string)>maxChars)
{
printf("\nToo many symbols, try again: ");
goto scanAgain;
}
string[maxChars]='\0';
return(string);
}
Output:
Enter new name: Alekasdasd
The values is now Alekasda�#
Enter new name:
im just a beginner so it might be something very simple... might be not.
oh and by the way, using linux and netbeans as SDK, gcc as compiler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里的“string”数组仅分配给 getString() 函数的范围。一旦它返回(超出范围),它就不再存在,并将被程序的其余部分覆盖。 “return(string)”语句返回不再分配的数据的指针——而不是数据本身。发生这种情况是由于 C 中的隐式数组到指针转换。
您的 getString() 函数不应这样做,而应采用 char* 作为参数,该参数在调用函数中分配。
The "string" array here is only allocated for the scope of the getString() function. Once it returns (goes out of scope), it ceases to exist and will be overwritten by the rest of your program. The "return(string)" statement returns the pointer of this data that's not allocated anymore -- not the data itself. This happens due to the implicit array-to-pointer conversion in C.
Instead of doing this, your getString() function should take a char* as an argument, which is allocated in the calling function.
我发现 getString() 函数有两个问题:
&
标记,而只是指向缓冲区的指针,string
。也就是说,将以下行更改为:
阅读
手册页
man 3 scanf
中您不希望在scanf()
调用中使用 & 符号的原因如下:I see two problems with your getString() function:
static
so that the memory used for it is not released (stack, popped) when the function returns.&
token, but simply the pointer to the buffer,string
.That is, change the lines:
to read
The reason you do not want the ampersand in the
scanf()
call is the following from the man page,man 3 scanf
:240行不是“片段”。
正如詹姆斯在评论中建议的那样,将代码减少到重现问题所需的最少行数。在这个阶段,问题的原因对您来说应该很明显——如果没有尝试再次发帖。
240 lines is not a "snippet".
As James suggested in his comment, reduce the code to the minimum number of lines needed to reproduce the problem. At that stage the cause of the problem should become obvious to you -- if not try posting again.
您将返回一个指向堆栈变量的指针。
当 getString 返回时,
string
无效。您的返回值指向这个无效字符串。使用:
另外,
goto
?请停止这种行为 - 使用for
、while do
、do while
而不是goto
You're returning a pointer to a stack variable.
When getString returns,
string
is invalid. Your return value points to this invalid string.Use:
Also,
goto
? Stop that please - usefor
,while do
,do while
but notgoto