我正在使用以下声明写入文本文件:
void create_out_file(char file_name[],long double *z1){
FILE *out;
int i;
if((out = fopen(file_name, "w+")) == NULL){
fprintf(stderr, "***> Open error on output file %s", file_name);
exit(-1);
}
for(i = 0; i < ARRAY_SIZE; i++)
fprintf(out, "%.16Le\n", z1[i]);
fclose(out);
}
其中 z1 是长度为 ARRAY_SIZE 的长双精度数组。调用函数是:
create_out_file("E:/first67/jz1.txt", z1);
我将原型定义为:
void create_out_file(char file_name[], long double z1[]);
我将其放在“int main”之前但在预处理器指令之后。我的代码工作正常。
我正在考虑将原型作为
void create_out_file(char file_name[],long double *z1).
这是正确的吗? *z1
将指向 z1
的第一个数组元素。
我的声明和原型是良好的编程实践吗?
非常感谢...
update :为了使程序通用,我将array_size定义为:
const int ARRAY_SIZE = 11;
原型变为:
void create_out_file(const char *file_name, const long double *z1, size_t z_size)
所谓的函数是:
create_out_file("/tmp/myname", z1, ARRAY_SIZE);
在函数声明中,我有
void create_out_file(const char *file_name, const long double *z1, size_t z_size)
FILE *out;
int i;
if((out = fopen(file_name, "w+")) == NULL){
fprintf(stderr, "***> Open error on output file %s", file_name);
exit(-1);
}
for(i = 0; i < z_size; i++)
fprintf(out, "%.16Le\n", z1[i]);
fclose(out);
}
此功能吗?
新更新 在编译时,对于声明中的行
for(i = 0; i < z_size; i++)
,我收到警告: : warning C4018: '<' :有符号/无符号不匹配
这里出了什么问题?
谢谢...
最新消息:感谢 Jonathan Leffler,一切正常
I'm writing to a text file using the following declaration:
void create_out_file(char file_name[],long double *z1){
FILE *out;
int i;
if((out = fopen(file_name, "w+")) == NULL){
fprintf(stderr, "***> Open error on output file %s", file_name);
exit(-1);
}
for(i = 0; i < ARRAY_SIZE; i++)
fprintf(out, "%.16Le\n", z1[i]);
fclose(out);
}
Where z1 is an long double array of length ARRAY_SIZE. The calling function is:
create_out_file("E:/first67/jz1.txt", z1);
I defined the prototype as:
void create_out_file(char file_name[], long double z1[]);
which I'm putting before "int main" but after the preprocessor directives. My code works fine.
I was thinking of putting the prototype as
void create_out_file(char file_name[],long double *z1).
Is this correct? *z1
will point to the first array element of z1
.
Is my declaration and prototype good programming practice?
Thanks a lot...
Update: To make the program general, I defined ARRAY_SiZE as:
const int ARRAY_SIZE = 11;
The prototype becomes:
void create_out_file(const char *file_name, const long double *z1, size_t z_size)
the called function is:
create_out_file("/tmp/myname", z1, ARRAY_SIZE);
and in the function declaration I have
void create_out_file(const char *file_name, const long double *z1, size_t z_size)
FILE *out;
int i;
if((out = fopen(file_name, "w+")) == NULL){
fprintf(stderr, "***> Open error on output file %s", file_name);
exit(-1);
}
for(i = 0; i < z_size; i++)
fprintf(out, "%.16Le\n", z1[i]);
fclose(out);
}
Will this work?
New Update On compilation, for the line
for(i = 0; i < z_size; i++)
in the declaration, I get the warning: : warning C4018: '<' : signed/unsigned mismatch
What's wrong here?
Thanks...
Latest news: It's working fine thanks to Jonathan Leffler
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用“const char file_name[]”或“const char *file_name” - 您不会更改函数中的名称。
同样,使用“const long double *z1”或“const long double z1[]”。
函数原型中指针和数组表示法之间的选择很大程度上是任意的。我几乎总是使用指针表示法 - 因为最终这就是传递给函数的内容。有人认为,如果要将指针用作数组,请使用数组表示法;这是一种完全合理的观点——但不是我所使用的观点。
如果将数组大小传递到函数中,您的代码会更通用:
请注意,对于全局变量,指针和数组表示法之间的选择不是任意的。这两者之间有很多区别:
一个说有一个指针大小的内存位置,其中包含字符串的地址(大概)。另一个说在某处有一个名为“anotherthing”的字符串,其值是一个指针。这可能有点微妙——但区别至关重要。 “指针==数组”同构仅适用于函数参数列表。
Use 'const char file_name[]' or 'const char *file_name' - you aren't going to change the name in the function.
Likewise, use 'const long double *z1' or 'const long double z1[]'.
The choice between pointer and array notation in a function prototype is largely arbitrary. I almost always use the pointer notation - because ultimately that is what is passed into the function. There are those who argue that if you are going to use the pointer as an array, use the array notation; it is a perfectly reasonable viewpoint - but not the one I use.
Your code is more general if you pass the array size into the function:
Note that the choice between pointer and array notation is not arbitrary for global variables. There is a lot of difference between these two:
One says there is a pointer-sized memory location that contains the address of a character string (presumably). The other says that there is a character string somewhere known by the name 'anotherthing', the value of which is a pointer. That may be a bit subtle - but the difference is crucial. The 'pointer == array' isomorphism only applies in function argument lists.
声明:
long double z1[]
和long double *z1
完全相同。是的。 c中没有这样的实体数组。只有指针和一些用于访问它们的简单语法糖
[]
。declarations:
long double z1[]
andlong double *z1
are completely same.yes. there is no such entity array in c. There are only pointers and some thin syntax sugar for accessing them
[ ]
.