473,788 Members | 2,784 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 #1
27 3386

"Ben Jacobs-Swearingen" <da***@mail.ute xas.edu> wrote in message
news:da******** *************** **@geraldo.cc.u texas.edu...
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


(snip)

There are some things in K&R C that just don't work anymore.

You might see if you can get the book from the library. I always did think
it was a little expensive, though.

You also might try to find a used copy. If you post the error messages,
someone might explain the difference, if any. At this point it is probably
better not to learn K&R C. The book is nice for its historical value,
though. Keep the book, but be careful how you use it.

-- glen
Nov 13 '05 #2

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

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


My copy of K&R1 is elsehome this semester, but if you'll cut and
paste the code you're trying to compile, I (and of course others)
will be glad to take a look and tell you what's right, what's
wrong and what used to be right but isn't anymore. :)
If I recall correctly, K&R doesn't start getting into platform-
specific stuff until chapter 7 or 8, so it's almost certainly a
problem in your typing-in of the program. But it's true that
some of the stuff in K&R1 isn't legal C anymore, too.
Oh, and is this code from K&R, or code you wrote as the solution
to one of the K&R exercises? When you post the code, please
indicate which it is, just in case we can't tell by looking. :)

HTH,
-Arthur
Nov 13 '05 #3
My copy of K&R1 is elsehome this semester, but if you'll cut and
paste the code you're trying to compile, I (and of course others)
will be glad to take a look and tell you what's right, what's
wrong and what used to be right but isn't anymore. :)


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)();
{
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
#-----------------------
int (*comp)(), (*exch)();
^
File "sort.c"; line 4 #Error: ')' expected
#-----------------------
if((*comp)(v[j], v[j+gap]) <= 0)
^
File "sort.c"; line 11 #Error: expression expected
#-----------------------
if((*comp)(v[j], v[j+gap]) <= 0)
^
File "sort.c"; line 11 #Warning 6: value of expression is not used
#-----------------------
break;
^
File "sort.c"; line 12 #Warning 6: value of expression is not used
#-----------------------
break;
^
File "sort.c"; line 12 #Error: ';' expected
#-----------------------
(*exch)(&v[j], &v[j+gap]);
^
File "sort.c"; line 13 #Error: undefined identifier 'exch'

---

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)())

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!

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.

B
da***@Mail.utex as.edu
Nov 13 '05 #4

"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> wrote in message
news:Pi******** *************** ***********@uni x48.andrew.cmu. edu...

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

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


My copy of K&R1 is elsehome this semester, but if you'll cut and
paste the code you're trying to compile, I (and of course others)
will be glad to take a look and tell you what's right, what's
wrong and what used to be right but isn't anymore. :)
If I recall correctly, K&R doesn't start getting into platform-
specific stuff until chapter 7 or 8, so it's almost certainly a
problem in your typing-in of the program. But it's true that
some of the stuff in K&R1 isn't legal C anymore, too.


Well, K&R allow writing into string constants, though I don't remember where
in the book they do that. That could be a problem really fast, though.
That is the first one I think of, though not related to function pointers.

-- glen
Nov 13 '05 #5

Figured it out -- had a bad pointer declaration in main() -- thanks for
the patience!

B
da***@mail.utex as.edu
Nov 13 '05 #6

"Ben Jacobs-Swearingen" <da***@mail.ute xas.edu> wrote in message
news:3110200311 36453426%da***@ mail.utexas.edu ...

Figured it out -- had a bad pointer declaration in main() -- thanks for
the patience!


Could you explain a little more? Bad declarations in main shouldn't cause
those problems.

Since C doesn't allow internal functions, though, if you had the wrong
number of } I might understand those errors.

-- glen
Nov 13 '05 #7
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)();
This form of function definition is obsolete, and you reallly need to
avoid it. Some compilers will complain about it nowadays. Also you're
using "implicit int" for the function itself, which is disallowed in
the newest C standard.

int sort(char **v, int n, int(*comp(), int *exch())
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. You really really need to get a newer book than K&R1,
its over 20 years out of date.
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)())


you need to put in the two missing "int"s.
--
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 #8
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:

int sort(char **v, int n, int(*comp(), int *exch())
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?

When I saw this code the only thing I could think of is
that the identifier comp is already defined as a type somewhere.

gcc 3.3.1, at least, invoked with

"gcc -Wall -W -ansi -std=c99 -pedantic -O2 -c test.c"

completes with only two warnings:

test.c:2: warning: return type defaults to `int'
test.c: In function `sort':
test.c:15: warning: control reaches end of non-void function
You really really need to get a newer book than K&R1,
its over 20 years out of date.


Right!

Nov 13 '05 #9
In article <xGzob.71194$HS 4.627461@attbi_ s01>,
"Glen Herrmannsfeldt" <ga*@ugcs.calte ch.edu> wrote:
"Ben Jacobs-Swearingen" <da***@mail.ute xas.edu> wrote in message
news:3110200311 36453426%da***@ mail.utexas.edu ...

Figured it out -- had a bad pointer declaration in main() -- thanks for
the patience!


Could you explain a little more? Bad declarations in main shouldn't cause
those problems.

Since C doesn't allow internal functions, though, if you had the wrong
number of } I might understand those errors.


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.

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). Thanks for
all the help, guys -- nice to know that there are some patient people
out there!

B
da***@mail.utex as.edu
Nov 13 '05 #10

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
1538
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
3180
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
2417
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
1152
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
1609
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
1256
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
9656
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
9498
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
10370
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10177
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
10113
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,...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
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
5402
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
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.