Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

c - Function declaration inside of function — why?

I am reading the book "Programming in C" and found in Chapter 10 an example like this:

#include <stdio.h>

void test (int??*int_pointer)
{
?????*int_pointer = 100;
}

int main (void)
{
?????void test (int??*int_pointer);
?????int??i = 50, *p = &i;

?????printf ("Before the call to test i = %i
", i);

?????test (p);
?????printf ("After the call to test i = %i
", i);

?????return 0;
}

I understand the example, but I don't understand the line void test (int *int_pointer); inside of main. Why do I define the signature of test again? Is that idiomatic C?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's definitely not idiomatic C, despite being fully valid (multiple declarations are okay, multiple definitions are not). It's unnecessary, so the code will still work perfectly without it.

If at all, perhaps the author meant to do

void test (int *int_pointer);

int main (void) {

    ...

}

in case the function definition was put after main ().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...