Re: Comments on my code?
- From: Harald van Dijk <truedfx@xxxxxxxxx>
- Date: Sat, 01 Sep 2007 14:59:04 +0200
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;
}
.
- Follow-Ups:
- Re: Comments on my code?
- From: Peter J. Holzer
- Re: Comments on my code?
- References:
- Comments on my code?
- From: Platonic Solid
- Comments on my code?
- Prev by Date: Re: Comments on my code?
- Next by Date: Re: Comments on my code?
- Previous by thread: Re: Comments on my code?
- Next by thread: Re: Comments on my code?
- Index(es):
Relevant Pages
|