473,756 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(in t data[])
{
/* some code here */
}
void init_array_2(in t *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 1587
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(in t data[])
{
/* some code here */
}

void init_array_2(in t *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(in t data[])
{
/* some code here */
}
void init_array_2(in t *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(in t 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*****@mindsp ring.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(in t data[])
{
/* some code here */
}

void init_array_2(in t *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*****@mindsp ring.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(in t data[])
{
/* some code here */
}

void init_array_2(in t *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(in t *data);
void init_array_2(in t *data_ptr);

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

return 0;
}

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

void init_array_2(in t *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_Keit h) 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

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

Similar topics

29
4405
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 reffers in all objects of this class to the same data Or in other word by using the static keyword all objects of one class can share data. (This is what I want)
5
2846
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 invisible() { alert("invisible() called"); } }
8
1833
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: http://www.htmldog.com/articles/suckerfish/dropdowns/ Here's the usage (in a few different lines) I found there:
34
6440
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 started messing around in Plan 9, and that sort of thing is totally no go there :). Is this correct to allocate memory for my struct? It works on my computer, but I'm suspicious that I'm doing it wrong. --
6
7995
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 compiler can find the definition of the function prior to encountering the use of the function it will generate the prototype itself. I don't currently use this feature, I explicitly create declarations for all functions in a header file. However, I...
13
1844
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 between the following 2 groups of "statements": function myFunc (x) { a = 1;
3
4593
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
2692
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 am confused with the way it is declared and defined. a.h ------- : :
29
8084
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 difference between a function prototype and a function declaration? If you get this right, you must have done fair amount of research on C
0
9255
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9819
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3326
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.