抱怨 FILE * 未初始化
void openUpNow(FILE *x, FILE *y)
{
x = fopen("xwhatever", "r");
y = fopen("ywhatever", "r");
}
int _tmain(int argc, _TCHAR* argv[ ])
{
FILE *x, *y;
openUpNow(x, y);
}
警告 C4700:使用了未初始化的局部变量“x”
警告 C4700:使用了未初始化的局部变量“y”
补救措施?
void openUpNow(FILE *x, FILE *y)
{
x = fopen("xwhatever", "r");
y = fopen("ywhatever", "r");
}
int _tmain(int argc, _TCHAR* argv[ ])
{
FILE *x, *y;
openUpNow(x, y);
}
warning C4700: uninitialized local variable 'x' used
warning C4700: uninitialized local variable 'y' used
Remedy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
无论如何,我认为这不是你想要做的。
假设您希望
openUpNow()
将文件打开到x
和y
中,您应该使用:换句话说,您需要传递将指针
x
和y
放入函数中。由于您的代码现在是这样,对
openUpNow()
的调用不会执行任何操作(并泄漏文件句柄),因为指针是按值传递的。I don't think that's what you want to do anyway.
Assuming you want
openUpNow()
to open the files intox
andy
you should use:In other words, you need to pass the address of the pointers
x
andy
into the function.As your code is right now, the call to
openUpNow()
doesn't do anything (and leaks the file-handles) since pointers are passed by value.使用未初始化的变量通常会导致未定义的行为,因此编译器会发出警告,以便您可以了解此类变量。
此外,您需要通过引用而不是通过值传递指针,或者您在函数内部得到的是原始指针的副本。
更正版本:
Using Uninitialized variables often results in Undefined Behavior hence the compilers warn of it so that you can be aware of such variables.
Also, you need to pass the pointers by reference and not by value or what you get inside the function is a copy of the original pointer.
Corrected version:
是的,该代码已损坏。在
_tmain
中,x
和y
未初始化并且具有垃圾值。然后将这些值传递给openUpNow
。幸运的是,openUpNow
忽略了它们。但随后您会丢弃 fopen 调用中的新值。你想要:Yeah, that code is broken. In
_tmain
,x
andy
are uninitialized and have garbage values. Then you pass those values toopenUpNow
. Fortunately,openUpNow
ignores them. But then you throw away the new values from the calls tofopen
. You want: