开发者

correct format for function prototype

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?

Th开发者_Go百科anks...

Latest news: It's working fine thanks to Jonathan Leffler


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:

void create_out_file(const char *file_name, const long double *z1, size_t z_size)
{
    ...
}

Note that the choice between pointer and array notation is not arbitrary for global variables. There is a lot of difference between these two:

extern char *something;
extern char  anotherthing[];

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.


declarations: long double z1[] and long double *z1 are completely same.

*z1 will point to the first array element of z1

yes. there is no such entity array in c. There are only pointers and some thin syntax sugar for accessing them [ ].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜