473,800 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

very basic C question--K&R on a Mac


Hello,

I just started learning C a couple weeks ago from Kernighan and
Ritchie (first edition -- I can't afford the newer second edition), and
have really enjoyed it so far. But I am having trouble making the code
at the end of Chapter 5 -- the sort function that uses function pointers
-- to work on my machine (233 Mhz iMac, 160 MB RAM, OS 9.2 ; MPW
environment). When I try to compile sort.c with the Symantec C compiler
it gives me all sorts of strange errors about how the syntax (copied
straight from the book!) is screwed up. Now when I'm at school and
working on an OSX machine running gcc, sort.c will compile correctly
along with everything else, but the input is all wrong (the pointers
aren't working properly). I *think* I've traced the problem in the
gcc-compile to sort.c as well, but I'm not sure. I really don't like
being stalled with this because I want to continue doing the exercises
(which are really fun) that are based on this code.

Having looked at the code many times by now and made sure I copied
the base code from the book correctly, I'm at my wits end. Does anyone
know any reason why the Kernighan & Ritchie code from the end of Chapter
5 (1st edition) might not compile correctly on a Mac? I suspect it has
to do with some obscure detail involving the inner workings of the
computer (that would necessitate a different arrangement of pointers?),
or (less likely) some incompatibility with ANSI C, but I'm not sure. I'd
much appreciate any constructive advice anyone could give me... thanks.
I can provide my source files for someone to look at if you would think
it would help.

Ben
da***@mail.utex as.edu
Nov 13 '05
27 3388
Mark McIntyre <ma**********@s pamcop.net> wrote in message news:<8j******* *************** **********@4ax. com>...
On Fri, 31 Oct 2003 10:14:54 -0600, in comp.lang.c , Ben
Jacobs-Swearingen <da***@mail.ute xas.edu> wrote:
sort(v, n, comp, exch) /* sort strings v[0] ... v[n-1] */
char *v[]; /* into increasing order */
int n;
int (*comp)(), (*exch)();
int sort(char **v, int n, int(*comp(), int *exch())


int sort(char **v, int n, int (*comp)(), int (*exch)())
{

/* ... */

}

The * binds more tightly to the left than the right in variable
declarations, so you need the parens to tell the compiler that the
variables themselves are pointers, instead of functions that return
pointers.

I think you wanted to post that, which is why your parens don't nest.
(The last paren actually closes the malformed expression begun right
after your second `int'. Your first paren is unclosed.)
Nov 13 '05 #11

On Fri, 31 Oct 2003, Ben Jacobs-Swearingen wrote:

"Glen Herrmannsfeldt" <ga*@ugcs.calte ch.edu> wrote:

Could you explain a little more? Bad declarations in main shouldn't
cause those problems.
Sure -- the problem that was screwing the gcc compiler at school was a
simple typo in the main() function -- I had a pointer where there should
have been a simple address marker (i.e. I had *c instead of c):

sort(*lineptr, nlines, strcmp, swap);

At the time I didn't know whether or not the two problems were related.
Now I know that they aren't, so you are right :)

The reason for the errors at home (the ones that prompted the original
message), as I figured out earlier tonight (after I sent that message),
is that evidently according to my compiler "comp" and "exch" already
have function definitions (probably in some header file on my computer
-- who knows). Changing the names of the functions to "c" and "e" fixed
the problem and I got a clean compile. I've had this problem
occasionally in other pieces of code, but in those cases it was more
obvious that the function names might have become part of the standard
library.


Many compilers have a "standard C" switch or two that you can turn
on if you want to compile... well... standard C. (In almost all
cases, that standard is C89, not C99, BTW.) That *should* get rid
of those spurious declarations of "comp", etc., if you can find
such a switch for your compiler.
Now everything is working again and I'm plugging through the book
(which'll be discarded in favor of K&R 2, when it arrives).


Just thought I should point out that I like K&R1 a heck of a lot
more than K&R2 -- but then I already know C, so I don't have to
learn it from the book anymore. :-) But I *did* learn C on my
own from K&R1 and a few other sources, and there is one thing to
be said for the First Edition: partly because it's missing the
Second Edition's standard library reference, it's a heck of a
lot *shorter*! (Plus, you never know when an out-of-print book
might turn out to be worth something... hang onto it, at least.
;-)

-Arthur
Nov 13 '05 #12
On Fri, 31 Oct 2003 21:35:29 -0500, in comp.lang.c , Sheldon Simms
<sh**********@y ahoo.com> wrote:
On Fri, 31 Oct 2003 22:33:48 +0000, Mark McIntyre wrote:
On Fri, 31 Oct 2003 10:14:54 -0600, in comp.lang.c , Ben
Jacobs-Swearingen <da***@mail.ute xas.edu> wrote:
sort(v, n, comp, exch) /* sort strings v[0] ... v[n-1] */
^
File "sort.c"; line 1 #Error: identifier expected


this is the sort of error that you will get using old-style
declarations.


Why do you say that? Where's the error?


Most likely its a typo, but its also possible the compiler is being
invoked in C++ mode and simply rejects pre-ansi function definitions.
I've seen some (nonconforming) C compilers that did that too.

In any events, retyping the definition in "modern" terms will almost
certainly remove the problem, either by fixing the typo, or by
silencing the compiler !

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #13
On 31 Oct 2003 21:11:20 -0800, in comp.lang.c ,
li************* **@yahoo.com (August Derleth) wrote:
Mark McIntyre <ma**********@s pamcop.net> wrote in message news:<8j******* *************** **********@4ax. com>...
On Fri, 31 Oct 2003 10:14:54 -0600, in comp.lang.c , Ben
Jacobs-Swearingen <da***@mail.ute xas.edu> wrote:
>sort(v, n, comp, exch) /* sort strings v[0] ... v[n-1] */
>char *v[]; /* into increasing order */
>int n;
>int (*comp)(), (*exch)();

int sort(char **v, int n, int(*comp(), int *exch())


int sort(char **v, int n, int (*comp)(), int (*exch)())


Absolutely. Typing too fast, not proofreading.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #14
Ben Jacobs-Swearingen wrote:
The reason for the errors at home
(the ones that prompted the original message),
as I figured out earlier tonight (after I sent that message),
is that evidently according to my compiler "comp" and "exch" already
have function definitions (probably in some header file on my computer
-- who knows).
Changing the names of the functions to "c" and "e" fixed
the problem and I got a clean compile. I've had this problem
occasionally in other pieces of code, but in those cases it was more
obvious that the function names might have become part of the standard
library.


K&R is full of example functions,
which have the same name as standard functions.
It is something that you will have to continue to watch out for.

--
pete
Nov 13 '05 #15
On Sat, 01 Nov 2003 10:00:20 +0000, Mark McIntyre wrote:
On Fri, 31 Oct 2003 21:35:29 -0500, in comp.lang.c , Sheldon Simms
<sh**********@y ahoo.com> wrote:
On Fri, 31 Oct 2003 22:33:48 +0000, Mark McIntyre wrote:
On Fri, 31 Oct 2003 10:14:54 -0600, in comp.lang.c , Ben
Jacobs-Swearingen <da***@mail.ute xas.edu> wrote:

sort(v, n, comp, exch) /* sort strings v[0] ... v[n-1] */
^
File "sort.c"; line 1 #Error: identifier expected

this is the sort of error that you will get using old-style
declarations.
Why do you say that? Where's the error?


Most likely its a typo,


Did you not read? I already said that the code compiled fine
with gcc 3.3.1. There was no typo.
but its also possible the compiler is being
invoked in C++ mode and simply rejects pre-ansi function definitions.
I've seen some (nonconforming) C compilers that did that too.

In any events, retyping the definition in "modern" terms will almost
certainly remove the problem, either by fixing the typo, or by
silencing the compiler !


Wrong answer. The code was fine. The problem was that the name
"comp" was already defined by the implementation. Making the
definition modern wouldn't have helped at all.

-Sheldon

Nov 13 '05 #16
On Sat, 01 Nov 2003 13:45:39 -0500, in comp.lang.c , Sheldon Simms
<sh**********@y ahoo.com> wrote:
On Sat, 01 Nov 2003 10:00:20 +0000, Mark McIntyre wrote:
On Fri, 31 Oct 2003 21:35:29 -0500, in comp.lang.c , Sheldon Simms
<sh**********@y ahoo.com> wrote:
On Fri, 31 Oct 2003 22:33:48 +0000, Mark McIntyre wrote:
Most likely its a typo,


Did you not read? I already said that the code compiled fine
with gcc 3.3.1. There was no typo.


Given that the OP has admitted elsethread to a typo, I stand
uncorrected. :-)
Making the definition modern wouldn't have helped at all.


I disagree. It would at the very least have rendered the error
meaningful as the compiler would have had to complain about a
redeclaration which was different.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #17
On Sun, 02 Nov 2003 00:24:44 +0000, Mark McIntyre wrote:
On Sat, 01 Nov 2003 13:45:39 -0500, in comp.lang.c , Sheldon Simms
<sh**********@y ahoo.com> wrote:
On Sat, 01 Nov 2003 10:00:20 +0000, Mark McIntyre wrote:
On Fri, 31 Oct 2003 21:35:29 -0500, in comp.lang.c , Sheldon Simms
<sh**********@y ahoo.com> wrote:

On Fri, 31 Oct 2003 22:33:48 +0000, Mark McIntyre wrote:

Most likely its a typo,


Did you not read? I already said that the code compiled fine
with gcc 3.3.1. There was no typo.


Given that the OP has admitted elsethread to a typo, I stand
uncorrected. :-)


This discussion has nothing to do with the typo. The OP was using gcc
on Mac OSX at school. There the program compiled, but didn't work properly
when he ran it. THAT was caused by a typo, but that is not what we were
talking about.

We were talking the OP's problem with Symantec C on Mac OS 9 at home,
where the program didn't even compile. You said: "this is the sort of
error that you will get using old-style declarations"

I interpreted that to mean that the function would have compiled if
it had a "new style" parameter declaration list. If that's what you
meant, that was wrong. If you just meant that with old-style
declarations you can get cryptic and unhelpful error, then ok.
Making the definition modern wouldn't have helped at all.


I disagree. It would at the very least have rendered the error
meaningful as the compiler would have had to complain about a
redeclaration which was different.


It depends on why a name-clash was occurring. In this case, you're
right.

-Sheldon
Nov 13 '05 #18
On Fri, 31 Oct 2003 10:14:54 -0600
Ben Jacobs-Swearingen <da***@mail.ute xas.edu> wrote:

<snip>
Alright, here's the code sort.c that won't compile at home. It's
copied straight from K&R (page 116 of the first edition; it's at the
end of the pointers chapter where they talk about function pointers),
as I was trying to get the base program working before I went on to do
the exercises:

sort(v, n, comp, exch) /* sort strings v[0] ... v[n-1] */
char *v[]; /* into increasing order */
int n;
int (*comp)(), (*exch)();
This style is obsolete and one of the many reasons you need a more up to
date reference. You should always specify the return type (void means it
does not return anything) and put the parameter list including types in
the brackets after the function name as follows:

void sort(char *v[], int n, int (*comp)(char *a,char *b),
void (*exch)(char *a[],char *b[]))
/* sort strings v[0]...v[n-1]*/
{
int gap, i, j;

for(gap = n/2; gap > 0; gap /= 2)
for(i = gap; i < n; i++)
for(j = i-gap; j >= 0; j -= gap) {
if((*comp)(v[j], v[j+gap]) <= 0)
break;
(*exch)(&v[j], &v[j+gap]);
}
}

looked at it again this morning and it still seems the same as the
Kernighan code (on page 116 of the first edition; don't even know if
it's included in the second edition). When I try to run it through the
Symantec C compiler at home it screams at me:

---

sort(v, n, comp, exch) /* sort strings v[0] ... v[n-1] */
^
File "sort.c"; line 1 #Error: identifier expected
<snip>

Apart from being old style it looks OK to my and my compiler accepts
your original code with only warnings about the return type defaulting
to int and reaching the end of a function returning int without having
returned one. Neither of these warnings is required with C90.
I know that function declarations are somewhat different in ANSI C,
and have tried declaring the function as

sort(char *v[], int n, (*comp)(), (*exch)())
Better, but see my suggestion further up.
but it doesn't make any difference. I'm going to school soon and I'll
keep on investigating the problem wit gcc -- hope to hear back from
you guys soon. I appreciate the patience with a non-computer-expert
newbie!
You should find that gcc accepts it.My guess is that either you are
invoking the Symantic C compiler incorrectly or it is a buggy compiler.
Unfortunately for you problems with your specific compiler are not
topical here so you will have to investigate that somewhere else.
Decided to bite the bullet and order the book anyway, as you said,
glen, there's little point trying to learn a version of C designed for
the PDP-11 and its contemporaries.


It's well worth the money IMHO.
--
Mark Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spamtrap, it is real and I read it.
Nov 13 '05 #19
On Sun, 02 Nov 2003 01:23:29 -0500, in comp.lang.c , Sheldon Simms
<sh**********@y ahoo.com> wrote:
meant, that was wrong. If you just meant that with old-style
declarations you can get cryptic and unhelpful error, then ok.


Correct. Without proper prototypes, the compiler is struggling to help
you. They were added to the language for a reason, I suspect.
Making the definition modern wouldn't have helped at all.


I disagree. It would at the very least have rendered the error
meaningful as the compiler would have had to complain about a
redeclaration which was different.


It depends on why a name-clash was occurring. In this case, you're
right.


Agreed.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #20

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

Similar topics

5
1495
by: hellrazor | last post by:
Hi there, First of all, I'm very much a C++ amateur (i.e., a newb). I'm having to program a win32 system service for my employer, and I'm almost done with the task, but I need help with something that appears to be very basic. I have a function within the cpp file called StopService: void StopService() {
5
5715
by: Lee David | last post by:
I went to the sun site and downloaded what I hope is the development part of java. I downloaded JDK5 with Netbeans. I installed it and now have a folder in my program group "Netbeans". Is that java? Would I execute that to create a java application? TIA, Lee
0
1539
by: Andy | last post by:
Hey All, I'm a beginner with VB.Dotnet Deployment and I'm a little confused about some very basic deployment issues . . . I've now created some core assemblies that will be used throughout all of our applications that run on various machines on our local network. I understand that the global assembly cache exists on each machine where the CLR is installed. But don't I want to establish just one global assembly cache on say one of our...
7
3184
by: jim Bob | last post by:
Hi, This is probably very simple to do so if anyone can point me to the right place for reading, it would be much appreciated. I just want to build a very basic search form where i can enter a name or part of a name into a text box, press a button, and the entered value gets inserted into a sql query and the results of the query gets displayed into a list or text box. (ie take the input from the text box and plug that variable in my...
17
2419
by: blueapricot416 | last post by:
This is a very basic question -- but I can't find the answer after looking for 20 minutes. If you code something like: function set_It() { setTimeout('Request_Complete("apple", -72)',5000) } and call it 50 times, will it cause problems because you are not
2
1153
by: Vincent Courcelle | last post by:
Hello, My question is very basic but I can't find an answer on search engines for it. How can I "pass" a variable to a control in a non programmatic way (directly in the .aspx file, not through the .aspx.cs file) ? For example : <asp:Login ID="LoginUser" runat="server" TitleText="" InstructionText="<%=Resources.Resource.MyVariable %>" Width="100%"></asp:Login>
4
1611
by: Stimp | last post by:
Hi all, I'm trying to read a particular node value from an XML file, but I've done some searching on the net and there doesn't seem to be a very basic example. Basically here's what I want to do... - In my web.config I have:
1
1709
by: questionit | last post by:
Hi Experts I need to write a small stack very basic program. It will only do the following: - pop item - push item - count number of items in stack But difficulty i have is how to implement it using Ms Access - form ?
3
1257
by: bbatson | last post by:
New to visual basic; trying to teach myself. Here is very basic code I am using to try to fill a text box on my form after updating a box that selects an employee ID. Dim strsql As String strsql = "SELECT Wage FROM Employees WHERE NameID = " & Me.EmpNameID Me.Wage = strsql However, when updated, the text just shows up in the box as opposed to looking up the wage. What am I doing wrong? Thank you!
1
204
by: Rik Wasmus | last post by:
On Tue, 17 Jun 2008 00:52:16 +0200, Twayne <nobody@devnull.spamcop.net> wrote: Euhm, no you can't. The only thing remotely close is something like: echo 'foo','bar'; .... which is nothing like concatenation at all.
0
9691
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
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,...
0
10279
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10255
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
9092
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
6815
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
5473
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
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3765
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.