-
August 13th, 2004, 11:54 AM
#1
pointer and strings question
i've made my own copy of the tool "echo", this is not to improve it or anything, but only to learn about strings and pointers...
Code:
#include <stdio.h>
#include <strings.h>
#include <stdlib.h>
int main(int argc,char** argv[])
{
if(argc>1)
{
char *str[100];/*one array for every argument*/
char *str2=" ";
int loop=0;
int count=1;
while (loop<=argc)/*loop to copy every argument to a string[]*/
{
str[loop]=argv[count];
loop++;
count++;
}
for(loop=0;loop<argc-1;loop++)/*loop to print the strings with a space between them(just like they were entered)*/
{
printf("%s",str[loop]);
printf("%s",str2);
}
return 0;
}
return EXIT_FAILURE;
}
now the question: what can i do to improve this code? isn't there a possibility to read argv[1] as one complete string, even if a space is entered?
-
August 13th, 2004, 01:04 PM
#2
If I had more than 100 arguments there may be an overflow security problem with this code:
Code:
char *str[100];/*one array for every argument*/[
Surely this would be better:
Code:
char *str[argc];/*one array for every argument*/
But what do i know - I've never written any c!
IT, e-commerce, Retail, Programme & Project Management, EPoS, Supply Chain and Logistic Services. Yorkshire. http://www.bigi.uk.com
-
August 13th, 2004, 01:35 PM
#3
steve is right on.
Now this:
Code:
int main(int argc,char** argv[])
It should be either "char **argv" or "char *argv[]"
For an "echo" program, the code is very unefficient. You could simply:
Code:
printf("%s ",argv[loop+1]); // assuming loop starts in 0
and get rid of str, str2 and count.
Since you want to learn pointers and strings, never mind. But you could also do:
Code:
str[loop] = malloc(strlen(argv[count]));
strcpy(str[loop], argv[count]);
to make things more interesting
AFAIK, there's no way to treat arguments as one complete string (and stored in argv[1]). But you could concat them all to one big string using strcat() before printing them.
Peace always,
<jdenny>
Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds
-
August 13th, 2004, 02:53 PM
#4
thanks guys, you've been a great help
and yes steve, i should have done that !
-
August 18th, 2004, 10:08 PM
#5
Adding some real input validation would be good. What types of values should you expect and what would you consider totally invalid?
Also, I'd try for better names for your char arrays, str and str2 aren't the most clear to noobs or people who are trying to figure out what you are doing.
You might also look into invariants and assertions. While not totally necessary, they will give you a better understanding of what they are, why they are used, and how to use them to prove the validity of your program logic.
"When I get a little money I buy books; and if any is left I buy food and clothes." - Erasmus
"There is no programming language, no matter how structured, that will prevent programmers from writing bad programs." - L. Flon
"Mischief my ass, you are an unethical moron." - chsh
Blog of X
-
August 19th, 2004, 06:45 AM
#6
yes your right Juridian, this only leaves me with one question left:
Code:
char input[80];
scanf("%s",input);
i know this doesn't test for input, but is there a way to test for it without having to use the function getchar() or getch() ?
cause when you use that function, a user isn't able to use the backspace when he made a typo. and if you scanf the complete string, it will react only when the <enter> button is pressed. only problem is that then you already scanned the input and might be vulnerable to a buffer overflow.
do you see what i mean?
-
August 19th, 2004, 10:10 AM
#7
how would you do that, something in the line of:
Code:
if(strlen(input)>79)
{
printf("\nPlease try again: ");
fgets(input);
}
a buffer overflow would overwrite the second time, wouldn't it?
-
August 20th, 2004, 10:31 AM
#8
What about:
char input[80];
fgets(input, 79, stdin);
input[strlen(input) - 1] = 0; // remove the newline character.
Than you can be sure that the program will only store up to 79 chars (80 if counting the terminating null char) to input. Editing is OK on a DOS prompt, but doesn't work properly on a Cygwin prompt though (I don't have any *nix box around).
Peace always,
<jdenny>
Always listen to experts. They\'ll tell you what can\'t be done and why. Then go and do it. -- Robert Heinlein
I\'m basically a very lazy person who likes to get credit for things other people actually do. -- Linus Torvalds
-
August 21st, 2004, 09:12 AM
#9
thanks, i'll give it a try
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|