非常基本的C问题 - Mac上的K& R [英] very basic C question--K&R on a Mac
问题描述
您好,
几周前我刚从Kernighan开始学习C和
Ritchie(第一版 - - 我买不起新版的第二版),到目前为止,
真的非常喜欢它。但是我在第5章结尾处制作代码时遇到了问题
- 使用函数指针的排序函数
- 在我的机器上工作(233 Mhz iMac ,160 MB RAM,OS 9.2; MPW
环境)。当我尝试使用Symantec C编译器编译sort.c
时,它给出了各种奇怪的错误,关于语法如何(直接从书中复制
!)是搞砸了。现在,当我在学校并且在运行gcc的OSX机器上运行
时,sort.c将正确编译
以及其他所有内容,但输入完全错误(指针
无法正常工作)。我想*我已经在
gcc-compile中跟踪问题了,但是我不确定。我真的不喜欢
因为我想继续进行练习
(这真的很有趣)基于这段代码。 />
现在多次查看代码并确保我正确地复制了本书中的基本代码,我的智慧结束了。有没有人
知道为什么Kernighan&章节末尾的Ritchie代码
5(第1版)可能无法在Mac上正确编译?我怀疑它有一些不明确的细节涉及到
计算机的内部运作(这需要一个不同的指针排列?),
或(不太可能)与ANSI C不兼容,但我不确定。我会非常感谢任何人都可以给我的任何建设性的建议......谢谢。
我可以提供我的源文件供别人查看你是否会想到
它会有所帮助。
Ben
da ** *@mail.utexas.edu
推荐答案
" Ben Jacobs-Swearingen" <哒*** @ mail.utexas.edu>在消息中写道
news:da ************************* @ geraldo.cc.utexas .edu ...
"Ben Jacobs-Swearingen" <da***@mail.utexas.edu> wrote in message
news:da*************************@geraldo.cc.utexas .edu...
几周前我刚从Kernighan开始学习C和Ritchie(第一版 - 我买不起新版的第二版),并且
到目前为止真的很享受。但是我在第5章末尾制作代码时遇到了麻烦 - 使用函数指针的排序函数
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
(剪辑)
K& RC中有些东西不再工作了。
你可能会看到你是否可以从图书馆拿到这本书。我总是这么认为
虽然它有点贵。
您也可以尝试找一个二手的副本。如果您发布错误消息,
有人可能会解释差异,如果有的话。在这一点上,最好不要学习K& R C.这本书的历史价值很好,但是b
。保留这本书,但要小心你的使用方法。
- glen
(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
2003年10月31日星期五,Ben Jacobs-Swearingen写道:
On Fri, 31 Oct 2003, Ben Jacobs-Swearingen wrote:
几周前我刚从Kernighan开始学习C和Ritchie(第一版 - 我可以'买不起新的第二版),到目前为止,它真的很享受。但是我在第5章结尾处制作代码时遇到了麻烦 - 使用函数指针的排序函数
- 在我的机器上工作
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
我这个学期的K& R1副本就是其他家庭,但是如果你要剪掉并且b / b
粘贴你想要编译的代码,我(当然还有其他人)
很高兴看一看,告诉你什么是对的,什么是'b $ b'错误以及过去是对的,但现在已经不行了。 :)
如果我没记错的话,在第7章或第8章之前,K& R并没有开始进入平台 -
特定的东西,所以几乎可以肯定你输入程序时遇到
的问题。但确实如此。
K& R1中的一些东西也不再合法C.
哦,这是来自K& K的代码R,或你作为解决方案写的代码
到其中一个K& R练习?当你发布代码时,请
表明它是什么,万一我们无法通过观察来判断。 :)
HTH,
-Arthur
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
我的这个学期K& R1的副本是其他的,但是如果你要剪切并粘贴你想要编译的代码,我(当然还有其他人)
将很乐意接受看看并告诉你什么是对的,什么是错的以及过去是对的但是不再存在。 :)
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. :)
好的,这是代码sort.c,它不会在家里编译。它直接从K& R复制了
(第一版的第116页;它是在
指针章节结束时他们谈论的功能指针),因为在我继续做
练习之前,我试图让基础程序正常工作:
sort(v,n,comp,exch)/ * sort strings v [0] ... v [n-1] * /
char * v []; / *增加顺序* /
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]);
}
}
再看一遍今天早上它仍然看起来像
Kernighan代码(在第一版的第116页;不知道是否
它包含在第二版)。当我尝试在家里通过
赛门铁克C编译器运行它时,它尖叫着我:
---
sort(v,n,comp,exch)/ * sort strings v [0] ... v [n-1] * /
^
File " sort.c英寸;第1行#Error:预期的标识符
#-----------------------
int(* comp)(),(* exch)();
^
文件" sort.c" ;;第4行#Error:'')''预计
#-----------------------
if((* comp)(v [j],v [j + gap])< = 0)
^
文件" sort.c" ;;第11行#Error:表达预期
#-----------------------
if(( * comp)(v [j],v [j + gap])< = 0)
^
文件" sort.c" ;;第11行#Warning 6:不使用表达式的价值
#-----------------------
break;
^
文件" sort.c" ;;第12行#Warning 6:不使用表达式的价值
#-----------------------
break;
^
文件" sort.c" ;;第12行#Error:'';''预计
#-----------------------
(* exch)(& v [j],& v [j + gap]);
^
文件" sort.c" ;;第13行#Error:未定义标识符''exch''
---
我知道ANSI C中的函数声明有些不同,并且
已经尝试将该函数声明为
sort(char * v [],int n,(* comp)(),(* exch) ())
但它没有任何区别。我很快就要去学校了,我会继续用bcc调查问题 - 希望能收到你们的回复
很快就会有人回复。我很感激耐心与非计算机专家新手!
无论如何,决定咬紧牙关并订购书籍,如你所说,格伦,
尝试学习为b-b
PDP-11及其同时代人设计的C版本没什么意义。
B
da *** @ Mail.utexas.edu
这篇关于非常基本的C问题 - Mac上的K& R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!