When I compiled this C source, a C compiler spat out a message which
was "Declaration syntax error
in function main()". I am having tested function pointer example
program. Of course, I am a novice at C so
that I don't know exactly which part is incorrect. Would you help me??
-----------------------------sort.c-------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sort_a_to_z(const void *first, const void *second);
int sort_z_to_a(const void *first, const void *second);
void main(void)
{
int ctr = 0;
int total;
char list[10][256];
printf("\n\nPress <Enterafter each word. Enter QUIT to end\n");
gets(list[ctr]);
while (stricmp(list[ctr], "QUIT") != NULL)
{
ctr++;
if(ctr == 10)
break;
gets(list[ctr]);
}
total = ctr;
qsort((void *)list, total, sizeof(list[0]), sort_a_to_z);
printf("\nThe items sorted A to Z\n");
for(ctr = 0; ctr < total; ctr++)
{
printf("\n%s", list[ctr]);
}
qsort((void *)list, total, sizeof(list[0]), sort_z_to_a);
printf("\n\nThe items sorted Z to A\n");
for(ctr = 0; ctr < total; ctr++)
{
printf("\n%s", list[ctr]);
}
int sort_a_to_z(const void *first, const void *second)
{
return(strcmp((char*)first, (char*)second);
}
int sort_z_to_a(const void *first, const void *second)
{
return(strcmp((char*)second, (char*)first);
}
} 9 1590
Denny wrote:
When I compiled this C source, a C compiler spat out a message which
was "Declaration syntax error
in function main()". I am having tested function pointer example
program. Of course, I am a novice at C so
that I don't know exactly which part is incorrect. Would you help me??
void main(void)
That should be int main(void).
while (stricmp(list[ctr], "QUIT") != NULL)
You don't declare stricmp.
>
int sort_a_to_z(const void *first, const void *second)
{
return(strcmp((char*)first, (char*)second);
Missing )
}
int sort_z_to_a(const void *first, const void *second)
{
return(strcmp((char*)second, (char*)first);
Missing )
--
Ian Collins.
On Tue, 26 Feb 2008 22:09:23 -0800, Denny wrote:
When I compiled this C source, a C compiler spat out a message which was
"Declaration syntax error
in function main()". I am having tested function pointer example
program. Of course, I am a novice at C so that I don't know exactly
which part is incorrect. Would you help me??
-----------------------------sort.c-------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sort_a_to_z(const void *first, const void *second);
int sort_z_to_a(const void *first, const void *second);
void main(void)
{
[...]
int sort_a_to_z(const void *first, const void *second) {
return(strcmp((char*)first, (char*)second);
}
int sort_z_to_a(const void *first, const void *second) {
return(strcmp((char*)second, (char*)first);
}
}
In addition to what Ian Collins said, you need to move sort_a_to_z and
sort_z_to_a outside main. Standard C doesn't have nested functions, and
even on some compilers that support them as an extension, it wouldn't work
the way you've defined the functions.
On Feb 26, 10:09*pm, Denny <e...@paran.comwrote:
When I compiled this C source, a C compiler spat out a message which
was "Declaration syntax error
in function main()". I am having tested function pointer example
program. Of course, I am a novice at C so
that I don't know exactly which part is incorrect. Would you help me??
-----------------------------sort.c-------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sort_a_to_z(const void *first, const void *second);
int sort_z_to_a(const void *first, const void *second);
void main(void)
{
* * * * int ctr = 0;
* * * * int total;
* * * * char list[10][256];
* * * * printf("\n\nPress <Enterafter each word. Enter QUIT to end\n");
* * * * gets(list[ctr]);
In addition to what everyone else said: http://home.att.net/~jackklein/ctips01.html#safe_gets
[snip]
On Tue, 26 Feb 2008 22:09:23 -0800 (PST), Denny <em**@paran.com>
wrote:
>When I compiled this C source, a C compiler spat out a message which was "Declaration syntax error in function main()". I am having tested function pointer example
It would be nice if you told us where the error occurred.
>program. Of course, I am a novice at C so that I don't know exactly which part is incorrect. Would you help me??
-----------------------------sort.c------------------------------- #include <stdio.h> #include <stdlib.h> #include <string.h>
int sort_a_to_z(const void *first, const void *second); int sort_z_to_a(const void *first, const void *second);
void main(void)
Already covered.
>{
int ctr = 0; int total; char list[10][256];
printf("\n\nPress <Enterafter each word. Enter QUIT to end\n");
gets(list[ctr]);
You have no idea if 256 characters will be sufficient to hold the
input.
> while (stricmp(list[ctr], "QUIT") != NULL)
stricmp is a common but not a standard function. The common ones
return an int. NULL can legally be defined as (void*)0. On systems
which do so this is a constraint violation requiring a diagnostic. If
you want to compare to 0, use 0.
> { ctr++; if(ctr == 10) break;
gets(list[ctr]); } total = ctr;
qsort((void *)list, total, sizeof(list[0]), sort_a_to_z);
The cast is unnecessary.
> printf("\nThe items sorted A to Z\n");
for(ctr = 0; ctr < total; ctr++) { printf("\n%s", list[ctr]); }
qsort((void *)list, total, sizeof(list[0]), sort_z_to_a);
printf("\n\nThe items sorted Z to A\n");
for(ctr = 0; ctr < total; ctr++) { printf("\n%s", list[ctr]); }
int sort_a_to_z(const void *first, const void *second)
Already covered.
> { return(strcmp((char*)first, (char*)second);
Already covered.
> }
int sort_z_to_a(const void *first, const void *second) { return(strcmp((char*)second, (char*)first); } }
Remove del for email
=?UTF-8?q?Harald_van_D=C4=B3k?= wrote:
>
On Tue, 26 Feb 2008 22:09:23 -0800, Denny wrote:
When I compiled this C source,
a C compiler spat out a message which was
"Declaration syntax error in function main()".
I am having tested function pointer example program.
Of course, I am a novice at C so that I don't know exactly
which part is incorrect. Would you help me??
-----------------------------sort.c-------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int sort_a_to_z(const void *first, const void *second);
int sort_z_to_a(const void *first, const void *second);
void main(void)
{
[...]
int sort_a_to_z(const void *first, const void *second) {
return(strcmp((char*)first, (char*)second);
}
int sort_z_to_a(const void *first, const void *second) {
return(strcmp((char*)second, (char*)first);
}
}
In addition to what Ian Collins said,
you need to move sort_a_to_z and sort_z_to_a outside main.
Standard C doesn't have nested functions,
and even on some compilers that support them as an extension,
it wouldn't work the way you've defined the functions.
A function definition is the only kind of declaration
not allowed inside of a function definition.
A function definition is the only kind of declaration
not terminated by a semicolon.
--
pete
On Thu, 28 Feb 2008 06:35:22 -0500, pete wrote:
A function definition is the only kind of declaration not allowed inside
of a function definition.
A function definition is the only kind of declaration not terminated by
a semicolon.
A function definition is the only kind of declaration that isn't
syntactically a declaration.
=?UTF-8?q?Harald_van_D=C4=B3k?= wrote:
A function definition is the only kind of declaration that isn't
syntactically a declaration.
I have no idea of what you think you mean by that.
--
pete
pete <pf*****@mindspring.comwrites:
=?UTF-8?q?Harald_van_D=C4=B3k?= wrote:
>A function definition is the only kind of declaration that isn't syntactically a declaration.
I have no idea of what you think you mean by that.
A function definition is not a declaration, because it does not
end with a semicolon:
declaration:
declaration-specifiers init-declarator-listopt ;
--
"I hope, some day, to learn to read.
It seems to be even harder than writing."
--Richard Heathfield
Ben Pfaff wrote:
>
pete <pf*****@mindspring.comwrites:
=?UTF-8?q?Harald_van_D=C4=B3k?= wrote:
A function definition is the only kind of declaration that isn't
syntactically a declaration.
I have no idea of what you think you mean by that.
A function definition is not a declaration, because it does not
end with a semicolon:
declaration:
declaration-specifiers init-declarator-listopt ;
Thank you.
--
pete This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Rob F |
last post by:
I am trying to compile a string class which was working before but
subsequent attempts to make it more 'object orientated' (privatizing
the member variables and implenting query functions) it...
|
by: xuatla |
last post by:
Hi,
I encountered the following compile error of c++ and hope to get your help.
test2.cpp: In member function `CTest CTest::operator+=(CTest&)':
test2.cpp:79: error: no match for 'operator='...
|
by: newbiecpp |
last post by:
I have hard time to understand run-time environment. Let assume that I have
a program that has a simple variable alpha. When this variable is
statically allocated, the compiler can use the...
|
by: David Smith |
last post by:
I am having a problem. I am attempting to compile the fte text editor
(fte-20020324-common.zip and fte-20020324-src.zip on Redhat linux 8.0.
Among
the error messages I get is the following:...
|
by: steve |
last post by:
Well I've been working all morning and have finally found the source of
my "bus error (signal 10)" errors. The source is odd. The error
occurs in any function where I make the function call:
...
|
by: Peter |
last post by:
Hi,
I am trying to compile an existing project (originally c) in .NET (rename .c
files to .cpp). After fixing some problems, here are the ones that I don't
know how to deal with:...
|
by: Allan M. Bruce |
last post by:
I have a small implementation of a queue which I am trying to get to compile
but cant figure out why it doesnt work. I have copied the minimum
compilable code below. On gcc I get
temp2.c: In...
|
by: TonyJ |
last post by:
Hello!
I get compile when using VS2005 but not in VS2003.
The compile error is the following
"Error 1 error C3867: 'MeltPracCommon::ReasonDialog::tbReason_TextChanged':
function call missing...
|
by: cpptutor2000 |
last post by:
Could someone please help me? I have the following C struct.
typedef struct
_FIREWALL_RULE_INFO
{
ULONG Precedence;
BOOL bEnabled;
BOOL ...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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,...
| |