i just want to print the number of the digits of the second argument. Why is that wrong??Code:nt main (int argc, char **argv[]){ int i=0; scanf ("%s",*argv[1]); while (argv[1][i]!='\0') i++; printf ("%d",i); }
i just want to print the number of the digits of the second argument. Why is that wrong??Code:nt main (int argc, char **argv[]){ int i=0; scanf ("%s",*argv[1]); while (argv[1][i]!='\0') i++; printf ("%d",i); }
Well first of all your main declaration is wrong and will probably return a syntax error...
scanf() is a keyboard input function... you push the buttons down and stuff pops up on the screen.Code:int main (int argc, char *argv[])
argv[] is an array of strings taken from the program's command line... scanf() will destroy them.
if you want the length of a string, there's no reason to reinvent the wheel... just #include <string.h> and use strlen()...
Code:printf("argv[1] is %d characters", strlen(argv[1]));
Get rid of the scanf. argv[1] already holds an argument passed into the program from the command line, you don't need to be trying to read into space that already holds valid data. And I believe argv[1] is really the first argument passed into the program, argv[2] is the second.
All you appear to be doing is counting up until the null terminating character. You could therefore just call strlen on that string and get rid of the loop.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
Thanks a lot guys!! But now i want to check with if -else the second argument string so i can choose what function to call.
so i did [code]
if (argv[1][] == "example")
printf ("example\n");
[\code]
but that is wrong again.. Any help about this?
Yep... you are working in a language that has *no concept* of text. In C everything is a number and "strings" are just arrays of numbers that represent the characters in the text. Thus... you cannot assign, compare or manipulate strings except through specialized library functions written for the task.
What you're most likely looking for is strcmp() ... look it up in your C library documentation to get the full informtion.
Code:if ( !strcmp(argv[1],"example")) puts(argv[1]);
Thanks again!!
Another small problem came up..
i want to print new- and the name of argv[i] but i can't tell how much space do i have to keep in array t. I tried to use malloc (strlen argv[i]) =k but an error came up that i cannot insert a variable in the array size.Code:int main (int argc, char *argv[]){ char t[4]="new-"; strcat (t,argv[1]); printf("%s",t) return 0; }
You only allocated 4 bytes, used them all, leaving no room for even the trailing 0 and then added more...
try... char t[100];
yes i undestand but isn't that waste of space? i mean by using the k = strlen(argv[i]) and then define the size of t to be t[k] can't i use only the space appropriate?
Last edited by iceaway; 09-30-2011 at 08:37 AM.
If you actually know the appropriate space... yes. But don't forget that when you go strcat() you need the sum of the length of all strings available to you... and you may not know their length at compile time. You can use malloc() of course...
But I wouldn't be too stingy with space... it's far better to use a bit extra than to not have enough. I have one program here where I malloc 16 megabytes for data manipulation so, in machines with 4 and 8gb of memory, I really wouldn't be too worried about 10 or 20 bytes...
Thanks a lot for your help! But will i be very annoying if i asked you iceaway to give me a small example based on my subject???
You can set a variable size array at runtime under the ISO99 standard, but you probably must tell the compiler to do this. You still cannot change the size.
What compiler are you using?
WRT to malloc:
You must free() *t when you are done with it. BTW, "t" is a terrible name for a variable.Code:#include <errno.h> char *t = malloc(strlen(argv[1])); if (!t) { printf("malloc: %s\n", strerror(errno)); exit(errno); }
As Tater says, if you are certain the space you need will not exceed a certain maximum, just allocate that. Eg:
4 kB is a big string, but very little memory. The OS gives you a fixed size stack anyway -- usually 8+ MB -- so you might as well use it.Code:char t[4096];
Last edited by MK27; 09-30-2011 at 08:55 AM.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge