473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

confused with function declarations

Dear All,

I am confused with prototypes in C. I saw the following code in a C book:
void init_array_1(int data[])
{
/* some code here */
}
void init_array_2(int *data_ptr)
{
/* some code here*/
}

int main()
{
int array[MAX];

void init_array_1();
void init_array_2();

/*some code ommited*/

return (0);
}

I don't understand why we need the two lines of function declarations
inside main:
void init_array_1();
void init_array_2();

Normally I would put them before main() and put those function
definitions after main. If the two functions block have been put before
main, I would thought no function declarations are needed any more.

Thank you very much.

Dec 21 '05 #1
10 1552
Xiaoshen Li wrote:

Dear All,

I am confused with prototypes in C. I saw the following code in a C book:

void init_array_1(int data[])
{
/* some code here */
}

void init_array_2(int *data_ptr)
{
/* some code here*/
}

int main()
{
int array[MAX];

void init_array_1();
void init_array_2();

/*some code ommited*/

return (0);
}

I don't understand why we need the two lines of function declarations
inside main:
void init_array_1();
void init_array_2();
We don't.
Normally I would put them before main() and put those function
definitions after main.
So would I.
If the two functions block have been put before
main, I would thought no function declarations are needed any more.


You would have thought correctly.

--
pete
Dec 21 '05 #2

"Xiaoshen Li" <xl**@gmu.edu> wrote in message
news:do**********@osf1.gmu.edu...
Dear All,

I am confused with prototypes in C. I saw the following code in a C book:
void init_array_1(int data[])
{
/* some code here */
}
void init_array_2(int *data_ptr)
{
/* some code here*/
}

int main()
{
int array[MAX];

void init_array_1();
void init_array_2();

/*some code ommited*/

return (0);
}

I don't understand why we need the two lines of function declarations
inside main:
void init_array_1();
void init_array_2();

Normally I would put them before main() and put those function definitions
after main. If the two functions block have been put before main, I would
thought no function declarations are needed any more.


It'd be interesting to know which book you were using, and what the subject
under discussion was when this code appeared.

What you've posted shows two function *definitions* - ahead of main - and
two differing predecs for those same functions inside of main. However, the
latter predecs change the shape of things - as the *say* that the compiler's
no idea what you're going to pass to these two functions at runtime. So,
they're somewhat in conflict with the external definitions [and the implicit
declarations that these make - when they appeared].

As they're seen later by the compiler, the latter predecs essentially
overrule the earlier definitions seen?

For example ...

:::This says that init_array_1 expects an int[] to be passed to it.

void init_array_1(int data[])
{
}
int main(void)
{

::: this says 'forget what you've heard about this function - you don't know
what it'll be passed!!!'
:::
void init_array_1();

char x;

::: pass a char to init_array_1 - it [the compiler] had better not
complain - as we told it to forget stuff!

init_array_1(x);
}

Lastly, it is more usual to see predecs ahead of main ... possibly because
include files mainly provude these, and are *normally* included ahead of any
code [although there's no real reason to do this - just as long as things
are seen before they're used].


Dec 21 '05 #3

"pete" <pf*****@mindspring.com> wrote in message
news:43***********@mindspring.com...
Xiaoshen Li wrote:

Dear All,

I am confused with prototypes in C. I saw the following code in a C book:

void init_array_1(int data[])
{
/* some code here */
}

void init_array_2(int *data_ptr)
{
/* some code here*/
}

int main()
{
int array[MAX];

void init_array_1();
void init_array_2();

/*some code ommited*/

return (0);
}

I don't understand why we need the two lines of function declarations
inside main:
void init_array_1();
void init_array_2();


We don't.
Normally I would put them before main() and put those function
definitions after main.


So would I.
If the two functions block have been put before
main, I would thought no function declarations are needed any more.


You would have thought correctly.


I would say 'have you nothing better to do!' [at 11.00pm] - but I hate
reality-reflection - so, I won't say it!

Damn it, oops, Ctrl+Z

damn it again!

Dec 21 '05 #4
pemo wrote:

"pete" <pf*****@mindspring.com> wrote in message
news:43***********@mindspring.com...
Xiaoshen Li wrote:

Dear All,

I am confused with prototypes in C.
I saw the following code in a C book:

void init_array_1(int data[])
{
/* some code here */
}

void init_array_2(int *data_ptr)
{
/* some code here*/
}

int main()
{
int array[MAX];

void init_array_1();
void init_array_2();

/*some code ommited*/

return (0);
}

I don't understand why we need
the two lines of function declarations
inside main:
void init_array_1();
void init_array_2();


We don't.
Normally I would put them before main() and put those function
definitions after main.


So would I.


.... except that I would have written them as protoypes,
instead of merely as declarations.

#define MAX 1

void init_array_1(int *data);
void init_array_2(int *data_ptr);

int main(void)
{
int array[MAX];

return 0;
}

void init_array_1(int *data)
{
/* some code here */
}

void init_array_2(int *data_ptr)
{
/* some code here*/
}

--
pete
Dec 22 '05 #5
The code is from the book "Practical C Programming" 1997, Chapter 13,
which is available in the link:

http://www.oreilly.com/catalog/pcp3/...h13.html#48674

Example 13-6.

I really like this book. Hard to believe that the author could be wrong.

Dec 22 '05 #6
On 2005-12-22, Xiaoshen Li <xl**@gmu.edu> wrote:
The code is from the book "Practical C Programming" 1997, Chapter 13,
which is available in the link:

http://www.oreilly.com/catalog/pcp3/...h13.html#48674

Example 13-6.

I really like this book. Hard to believe that the author could be wrong.


What exactly are you confused about?
Dec 22 '05 #7
Xiaoshen Li wrote:
The code is from the book "Practical C Programming" 1997, Chapter 13,
which is available in the link:

http://www.oreilly.com/catalog/pcp3/...h13.html#48674

Example 13-6.

I really like this book. Hard to believe that the author could be wrong.


I don't. I know of enough bad technical books to find it very easy to
believe an author is wrong. In this case though, I believe it is more a
case fo using a very bad style than something that is completely wrong.

I also note that this author is using the term "procedure" when in C it
is usual to refer even to void functions as functions.

It also says, "Finally, there is a special pointer called NULL. It
points to nothing. (The actual numeric value is 0.) The standard include
file, locale.h, defines the constant NULL. (This file is usually not
directly included, but is usually brought in by the include files
stdio.h or stdlib.h.)" which is wrong or misleading on several points.

It is incorrect in saying that NULL is the name of a pointer, NULL is a
macro that expands to a null pointer.
It is correct that locale.h defines the NULL macro.
It is at least misleading saying that that stdio.h and stdlib.h include
locale.h, since on many systems it may not and even if it does you still
won't get the other things that locale.h defines.

It shows an example that modifies a string literal with a comment that
it is legal. This is wrong, the compiler is not required to complain,
but it is also not required to produce working code since attempting to
modify a string literal is undefined behaviour.

It shows using the %p format specifier without casting the pointer to
void*, %p is only valid for pointers to void.

There are probably other errors that I have not spotted on a quick look.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Dec 22 '05 #8
Xiaoshen Li wrote:

The code is from the book "Practical C Programming" 1997, Chapter 13,
which is available in the link:

http://www.oreilly.com/catalog/pcp3/...h13.html#48674

Example 13-6.

I really like this book.
Hard to believe that the author could be wrong.


Many C tutorials are written by people who are wrong.
Placing function declarations inside of function definitions,
is awkward style. (also bad because it's awkward)

The use of nonprototype function declarations,
is bad style.

The
int main()
style definition, is also obsolescent.
int main(void)
is better.

http://www.open-std.org/jtc1/sc22/wg14/www/docs/n869/
N869
6.11.4 Function declarators
[#1] The use of function declarators with empty parentheses
(not prototype-format parameter type declarators) is an
obsolescent feature.

--
pete
Dec 22 '05 #9
Flash Gordon <sp**@flash-gordon.me.uk> writes:
Xiaoshen Li wrote:
The code is from the book "Practical C Programming" 1997, Chapter
13, which is available in the link:
http://www.oreilly.com/catalog/pcp3/...h13.html#48674
Example 13-6.
I really like this book. Hard to believe that the author could be
wrong.


I don't. I know of enough bad technical books to find it very easy to
believe an author is wrong. In this case though, I believe it is more
a case fo using a very bad style than something that is completely
wrong.

I also note that this author is using the term "procedure" when in C
it is usual to refer even to void functions as functions.

It also says, "Finally, there is a special pointer called NULL. It
points to nothing. (The actual numeric value is 0.) The standard
include file, locale.h, defines the constant NULL. (This file is
usually not directly included, but is usually brought in by the
include files stdio.h or stdlib.h.)" which is wrong or misleading on
several points.

It is incorrect in saying that NULL is the name of a pointer, NULL is
a macro that expands to a null pointer.


No, NULL is a macro that expands to a null pointer constant.
[snip]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 22 '05 #10
Keith Thompson wrote:
Flash Gordon <sp**@flash-gordon.me.uk> writes:
Xiaoshen Li wrote:
<snip>
It is incorrect in saying that NULL is the name of a pointer, NULL is
a macro that expands to a null pointer.


No, NULL is a macro that expands to a null pointer constant.
[snip]


Damn, I missed one word. That'll teach me to reply hurriedly when I
should be working :-)

Actually, it probably won't. ;-)
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Dec 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
8
by: Jim Moon | last post by:
Hi. I'm curious about this syntax: <variable>=function(){...} I'm not finding a definition of this syntax, but I see it used at this website:...
34
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
6
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the...
13
by: Marvin | last post by:
Hi: I have been programming with OOP and C++ for over 10 years but I am new to javascript. I am very confused about one aspect and would appreciate it if someone could explain the differences...
3
by: arnuld | last post by:
1) int i = 3; 2.) int* pi; 3.) int* pi2 = &i 4.) char* pc; 5.) char c; 6) char c2 = 'a' 7.) char* pc2 = &c2 8.) char* ps = "Stroustroup"; 9.) extern double d;
9
by: Lalatendu Das | last post by:
I have seen a header file in which one structure is defined with extern declaration in a header file and declared one variable of that structure in one C-file. The code goes as mentioned below . I...
29
by: Ravishankar S | last post by:
Dear C Experts, While prepating a content for a C course,I made section on function prototypes. Could you kindly provide me your comments on its correctness. Thank you ! Q12: What is the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.