评论我的代码? [英] Comments on my code?
问题描述
Hi-
我正在从一些旧的讲义中学习C,但他们没有
解决方案所以我想要一些如果有人有时间,我会回复我的代码。
这是一个练习:编写一个程序来修剪任何领先的空白
来自字符串并返回一个新分配的缓冲区,包含已修剪的
字符串。不要忘记处理错误。
main(argc,argv)
int argc; char ** argv;
{
char * strtrim(),* r = 0;
puts(argc> 1&& (r = strtrim(* ++ argv))?r:未指定错误;;
免费(r);
}
>
/ *修剪初始空白* /
char * strtrim(s)
char * s;
{
char * r,* malloc();
for(; isspace(* s); s ++);
r = malloc(strlen(s)+ 1);
如果(r)
strcpy(r,s);
返回r;
}
这看起来对我来说没问题并且工作正常,但是编译器会产生一些关于冲突定义的神秘警告我并不是真的
明白。
-
为什么我们再也不说话了?
< blockquote> Platonic Solid写道:
Hi-
我从一些旧的讲义中学习C,但他们不知道没有
解决方案,所以我想要一些feedba如果有人有时间,请查看我的代码。
这是一个练习:编写一个程序来修剪任何领先的空白
来自一个字符串并返回一个新分配的缓冲区,包含已修剪的
字符串。不要忘记处理错误。
main(argc,argv)
int argc; char ** argv;
{
char * strtrim(),* r = 0;
puts(argc> 1&& (r = strtrim(* ++ argv))?r:未指定错误;;
免费(r);
}
>
/ *修剪初始空白* /
char * strtrim(s)
char * s;
{
char * r,* malloc();
for(; isspace(* s); s ++);
r = malloc(strlen(s)+ 1);
如果(r)
strcpy(r,s);
返回r;
}
这看起来对我来说没问题并且工作正常,但是编译器会产生一些关于冲突定义的神秘警告我并不是真的
明白。
-
为什么我们再也不说话了?
你不应该像那样声明malloc。
#include< stdlib.h>
从不定义你自己的库函数定义。
你需要
#include< string.h>
for strlen''原型。
和:
char * strtrim(s)
char * s ;
这是老式的C.更好的是:
char * strtrim(char * s)
PS当s为NULL时会发生什么?
jacob
Platonic Solid写道:
Hi-
我正在从一些旧的讲义中学习C,但他们没有
解决方案所以我会就像有人有时间一样反馈我的代码。
这是一个练习:写一个程序来修剪任何领先的空白
来自一个字符串并返回一个新分配的缓冲区,其中包含已修剪的
字符串。不要忘记处理错误。
main(argc,argv)
int argc; char ** argv;
{
char * strtrim(),* r = 0;
puts(argc> 1&& (r = strtrim(* ++ argv))?r:未指定错误;;
免费(r);
}
>
/ *修剪初始空白* /
char * strtrim(s)
char * s;
{
char * r,* malloc();
for(; isspace(* s); s ++);
r = malloc(strlen(s)+ 1);
如果(r)
strcpy(r,s);
返回r;
}
这看起来对我来说没问题并且工作正常,但是编译器会产生一些关于冲突定义的神秘警告我并不是真的
明白了。
你写的程序很久以前就是编写代码的正确方法,
所以如果你的讲义真的很旧,然后干得好。不过,现在做事情并不是一种正确的方式。试试这个版本,看看你是否理解我所做的更改:
#include< stdio.h / *包含声明库的标题* /
#include< stdlib.h / *您正在使用的函数和对象。你应该* /
#include< string.h / *不要在你自己的程序中声明它们,特别是* /
#include< ctype.h / * the错误道;这就是导致你的警告* /
/ *
*你的功能的前瞻声明不应该是本地的
*另一个功能。另外,您可以在声明中指定参数类型
*以及定义。这样做允许
*编译器检查并自动转换你将要传递给函数的价值
*。
*
*我重命名了这个函数,因为从技术上来说,strtrim对于它来说不是有效的名字
*,但是为什么你可能不会感兴趣的原因
*这个阶段。
* /
char * trimstr(char *);
/ *
*参数的类型应该在参数列表中指定,
*不在...和{。之间。它仍然有效,但有相同的问题
*因为没有在函数声明中指定参数类型。
*你应该指定返回类型,甚至如果它是int。
* /
int main(int argc,char ** argv){
/ *
*出于可读性原因,我建议不要使用?:,
*并使用if语句。
* /
if(argc 1){
char * r = trimstr(argv [1]);
if(!r){
/ *错误消息通常应该发送到stderr,而不是stdout * /
fputs(" Unspecified error\\\
,stderr);
} else {
put(r);
free(r);
}
}
/ * main返回int(即使在原始版本中),所以返回一个int * /
返回0;
}
char * trimstr(char * s){
char * r;
/ *为了便于阅读,你可以重写你的for循环* /
while(isspace(* s))
s ++;
/ *
* malloc不需要在这里手动声明,< stdlib.h>
包括
*。并且malloc不再返回''char *'',但''void *''
* void可能不在你的讲义中,但在
$ b中$ b *指针的上下文,它意味着指向任何对象的指针,但是你
*没有对象的类型。
* /
r = malloc(strlen(s)+1);
if(r)
strcpy(r,s);
返回r;
}
2007年9月1日12:53,jacob navia写道:
PS当s为NULL时会发生什么?
如果仔细观察,我会检查argc> 1,所以它永远不会被调用,因为它需要使用s
null。
>
-
为什么我们再也不说话了?
Hi-
I am learning C from some old lecture notes, but they don''t have
solutions so I''d like some feedback on my code if anyone has the time.
This is an exercise: "Write a program to trim any leading whitespace
from a string and return a newly allocated buffer containing the trimmed
string. Don''t forget to handle errors."
main(argc, argv)
int argc; char **argv;
{
char *strtrim(), *r=0;
puts(argc>1 && (r=strtrim(*++argv)) ? r : "Unspecified error");
free(r);
}
/* trims initial whitespace */
char *strtrim(s)
char *s;
{
char *r, *malloc();
for( ; isspace(*s); s++);
r=malloc(strlen(s)+1);
if(r)
strcpy(r,s);
return r;
}
This looks OK to me and works correctly, but the compiler produces some
mysterious warnings about conflicting definitions that I don''t really
understand.
--
How come we never talk anymore?
Platonic Solid wrote:Hi-
I am learning C from some old lecture notes, but they don''t have
solutions so I''d like some feedback on my code if anyone has the time.
This is an exercise: "Write a program to trim any leading whitespace
from a string and return a newly allocated buffer containing the trimmed
string. Don''t forget to handle errors."
main(argc, argv)
int argc; char **argv;
{
char *strtrim(), *r=0;
puts(argc>1 && (r=strtrim(*++argv)) ? r : "Unspecified error");
free(r);
}
/* trims initial whitespace */
char *strtrim(s)
char *s;
{
char *r, *malloc();
for( ; isspace(*s); s++);
r=malloc(strlen(s)+1);
if(r)
strcpy(r,s);
return r;
}
This looks OK to me and works correctly, but the compiler produces some
mysterious warnings about conflicting definitions that I don''t really
understand.
--
How come we never talk anymore?You should not declare malloc like that.
#include <stdlib.h>
and never define your own definitions for library functions.
You need
#include <string.h>
for strlen''s prototype.
And:char *strtrim(s)
char *s;This is old fashioned C. Better is:
char *strtrim(char *s)P.S. What happens when s is NULL?
jacob
Platonic Solid wrote:Hi-
I am learning C from some old lecture notes, but they don''t have
solutions so I''d like some feedback on my code if anyone has the time.
This is an exercise: "Write a program to trim any leading whitespace
from a string and return a newly allocated buffer containing the trimmed
string. Don''t forget to handle errors."
main(argc, argv)
int argc; char **argv;
{
char *strtrim(), *r=0;
puts(argc>1 && (r=strtrim(*++argv)) ? r : "Unspecified error");
free(r);
}
/* trims initial whitespace */
char *strtrim(s)
char *s;
{
char *r, *malloc();
for( ; isspace(*s); s++);
r=malloc(strlen(s)+1);
if(r)
strcpy(r,s);
return r;
}
This looks OK to me and works correctly, but the compiler produces some
mysterious warnings about conflicting definitions that I don''t really
understand.The program you wrote would be a correct way to write code a long time ago,
so if your lecture notes were really old, then well done. It''s not a right
way to do things now, though. Try this version, and see if you understand
the changes I made:
#include <stdio.h /* include the headers that declare the library */
#include <stdlib.h/* functions and objects you are using. you should */
#include <string.h/* not declare them in your own program, especially*/
#include <ctype.h /* the wrong way; that is what caused your warning */
/*
* a forward declaration for your function should not be local to
* another function. also, you can specify the types of the parameters
* in the declaration as well as the definition. doing so allows the
* compiler to check and automatically convert the values you will be
* passing to the function.
*
* I renamed the function because technically, strtrim isn''t a valid name
* for it, but the reasons why probably won''t be of interest to you at
* this stage.
*/
char *trimstr(char *);
/*
* the types of the parameters should be specified in the parameter list,
* not between the ) and {. it''s still valid, but has the same problems
* as not specifying the parameter types in the function declaration.
* and you should specify the return type, even if it''s int.
*/
int main(int argc, char **argv) {
/*
* for readability reasons, I would recommend against your use of ?:,
* and use if statements instead.
*/
if (argc 1) {
char *r = trimstr(argv[1]);
if (!r) {
/* error messages should normally go to stderr, not stdout */
fputs("Unspecified error\n", stderr);
} else {
puts(r);
free(r);
}
}
/* main returns int (even in your original version), so return an int */
return 0;
}
char *trimstr(char *s) {
char *r;
/* for readability, you can rewrite your for loop */
while (isspace(*s))
s++;
/*
* malloc doesn''t need to be declared manually here, with <stdlib.h>
* included. and malloc doesn''t return ''char *'' anymore, but ''void *''
* void is probably not covered in your lecture notes, but in the
* context of pointers, it means a pointer to any object, but you
* don''t have the object''s type.
*/
r = malloc(strlen(s)+1);
if(r)
strcpy(r,s);
return r;
}
On 1 Sep 2007 at 12:53, jacob navia wrote:P.S. What happens when s is NULL?If you look carefully I check argc>1, so it never gets called with s
null.
--
How come we never talk anymore?
这篇关于评论我的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!