|
From: Rob Slight on 11 Dec 2006 17:37 Hi I new to Unix programming and was wondering if anyone could help me. I wish to put the argv[1] into a intger intNumbersTest but when I use the code below I get a funny number like -33333382. What am I doing wrong. I also wish to put the 4th argument into a char but thats not working also. int main(int argc, *argv[]) { int intSlave; int intNumbersTest; FILE *fptr; if(argc>1) { intNumbersTest = (int)argv[1]; printf("Numbers %d \n",intNumbersTest); if (argc==3) { intSlave = (int)argv[2]; printf("Numbers %d \n",intSlave); } } if(argc==4) { char Filename[128] = argv[4]; } } If anyone could help please Rob
From: Pascal Bourguignon on 11 Dec 2006 17:45 "Rob Slight" <rob[at]urlcool.net> writes: > Hi > > I new to Unix programming and was wondering if anyone could help me. I wish > to put the argv[1] into a intger intNumbersTest but when I use the code > below I get a funny number like -33333382. What am I doing wrong. I also > wish to put the 4th argument into a char but thats not working also. > > int main(int argc, *argv[]) m.c:3: error: parse error before '*' token > If anyone could help please Once you correct this error, the answer to your question should be clear. -- __Pascal Bourguignon__ http://www.informatimago.com/ NOTE: The most fundamental particles in this product are held together by a "gluing" force about which little is currently known and whose adhesive power can therefore not be permanently guaranteed.
From: John Gordon on 11 Dec 2006 17:54 In <Yt6dnemSaNNrQODYRVnyuwA(a)bt.com> "Rob Slight" <rob[at]urlcool.net> writes: > I new to Unix programming and was wondering if anyone could help me. I wish > to put the argv[1] into a intger intNumbersTest but when I use the code > below I get a funny number like -33333382. What am I doing wrong. I also > wish to put the 4th argument into a char but thats not working also. > int main(int argc, *argv[]) > { > int intSlave; > int intNumbersTest; > FILE *fptr; > if(argc>1) > { > intNumbersTest = (int)argv[1]; > printf("Numbers %d \n",intNumbersTest); You can't convert from a string to an integer that way. The number you're getting is the memory address of the string, which obviously isn't what you wanted. Do this instead: sscanf(argv[1], "%d", &intNumbersTest); > if(argc==4) > { > char Filename[128] = argv[4]; In general, you can't use the = sign when working with strings. If you want to make a copy of a string, use the strcpy() function. strcpy(Filename, argv[4]); However, you'll want to move the declaration of Filename up to the top of main(). The way you're declaring it now, it's limited in scope, and as soon as you pass out of the if() statement, the variable is lost. -- John Gordon "... What with you being his parents and all, gordon(a)panix.com I think that you could be trusted not to shaft him." -- Robert Chang, rec.games.board
From: Rob Slight on 11 Dec 2006 18:03 Thank you that seems to work. However I get "warning: incompatible implicit declaration of built-in function strcpy" from GCC. The program works fine anyway. Rob "John Gordon" <gordon(a)panix.com> wrote in message news:elknik$6ae$1(a)reader2.panix.com... > In <Yt6dnemSaNNrQODYRVnyuwA(a)bt.com> "Rob Slight" <rob[at]urlcool.net> > writes: > >> I new to Unix programming and was wondering if anyone could help me. I >> wish >> to put the argv[1] into a intger intNumbersTest but when I use the code >> below I get a funny number like -33333382. What am I doing wrong. I >> also >> wish to put the 4th argument into a char but thats not working also. > >> int main(int argc, *argv[]) >> { >> int intSlave; >> int intNumbersTest; >> FILE *fptr; > >> if(argc>1) >> { >> intNumbersTest = (int)argv[1]; >> printf("Numbers %d \n",intNumbersTest); > > You can't convert from a string to an integer that way. The number you're > getting is the memory address of the string, which obviously isn't what > you wanted. > > Do this instead: > > sscanf(argv[1], "%d", &intNumbersTest); > >> if(argc==4) >> { >> char Filename[128] = argv[4]; > > In general, you can't use the = sign when working with strings. If you > want to make a copy of a string, use the strcpy() function. > > strcpy(Filename, argv[4]); > > However, you'll want to move the declaration of Filename up to the top > of main(). The way you're declaring it now, it's limited in scope, and > as soon as you pass out of the if() statement, the variable is lost. > > -- > John Gordon "... What with you being his parents and all, > gordon(a)panix.com I think that you could be trusted not to shaft > him." -- Robert Chang, rec.games.board >
From: Måns Rullgård on 11 Dec 2006 19:30 "Rob Slight" <rob[at]urlcool.net> writes: > Thank you that seems to work. > > However I get "warning: incompatible implicit declaration of built-in > function strcpy" from GCC. The program works fine anyway. #include <string.h> -- M�ns Rullg�rd mru(a)inprovide.com
|
Next
|
Last
Pages: 1 2 Prev: find in current directory - SUN OS Next: Will not access function via pthread_create |