everytime I try to compile this program I get this error:
warning: cast to pointer from integer of different size.
on line 23 where the error is occuring, i have this:
if(strcmp(*(argv+x),(char*)*pas)==0)
what s wrong?
everytime I try to compile this program I get this error:
warning: cast to pointer from integer of different size.
on line 23 where the error is occuring, i have this:
if(strcmp(*(argv+x),(char*)*pas)==0)
what s wrong?
Seems to me that you're dereferencing the pointers (depends on your declaration of them). strcmp() takes two char* as arguments, so dereferencing them is not a good idea.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
You'll have to post all the code if you want to know what's really going on.
gg
If it's to do with command line args, this might help you.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
here is the code
still working on, thanx for the help so far.Code:#include <time.h> #include <string.h> void try(void); int main(int argc, char *argv[]) { char pass[20], *pas; int ranmb, x; char range[] = "abcdefghijklmnopqrstuvwxyz1234567890", *rang; pas = pass; rang = range; if(argc==1) { printf("Requires parameters\n"); exit(0); } srand((unsigned)time(NULL)); ranmb=rand()%37; *pas=*(rang+ranmb); ranmb=rand()%37; *(pas+1)=*(rang+ranmb); for(x=0;x<argc-1;x++) { if(strcmp(*(argv+x),(char*)*pas)==0) { printf("You got through!\n"); exit(0); } if(x==argc-1) { printf("Nice Try.\n"); exit(0); } } }
You're still dereferencing at the wrong occacions.
Example:
pas is a char*
When you call strcmp(), you're dereferencing pas making it a char. But strcmp() takes two char*, not two char.
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
>>if(strcmp(*(argv+x),(char*)*pas)==0)
look out again the manual page of the strcmp function, you will find out some errors.
Also, in your code, include stdio, stdlib, cause you used functions like printf, rand and srand without using the header files.
Read this.
- why does the pass[] string have 20 elements when you only need the first 3? (see below)
- note that "rand()%37" will return a number between 0 and 36, index 36 of the range[] string is the null terminator ('\0').
- your for loop is not setup correctly. argc is the size of the argv pointer array. If argc is 5, then valid index's into argv are 0 - 4. The first argument is always your program. If you want to loop through the user's arguments, then you need:
Code:for (x=1; x<argc; x++)- strcmp() will compare two null-terminated strings. You are not gaurenteed that the pass[] string is null terminated. You gave pass[0] and pass[1] a value from the range string (which could have been null, see 2nd bullet) but if you want to use it as a string, you need to put NULL (or '\0') in pass[2].
- "*(argv+x)" is the same as "argv[x]" and "*pas" is the same as "pass[0]" - the first argument is fine, the second one is wrong. Pass in just "pas" or "pass".
- main() must return an int. Put a "return 0" before the last line of main(). You can also use "return 0" instead of exit(0).
- instead of checking if you should print "Nice Try" on every itteration of the for loop, just do it right after the loop.
- you don't really need the pas and rang pointers since "*(pas+1)" is the same as "pass[1]" and "*(rang+ranmb)" is the sames as "range[ranmb]"
gg
i don't think I am getting this, i fixed up my code to the last post a little bit. How should I fix my code, mabey then I will understand, please help.
here it is
Code:#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h> void try(void); int main(int argc, char *argv[]) { char pass[2]; int ranmb, x; char range[] = "abcdefghijklmnopqrstuvwxyz1234567890"; if(argc==1) { printf("Requires parameters\n"); return 0; } srand((unsigned)time(NULL)); ranmb=rand()%37; pass[0]=range[ranmb]; ranmb=rand()%37; pass[0]+=range[ranmb]; for(x=0;x<argc;x++) { if(strcmp(argv[x],(char*)pass[0])==0) { printf("You got through!\n"); return 0; } } printf("Nice try\n"); return 0; }
>if(strcmp(argv[x],(char*)pass[0])==0) {
you still don't get, strcmp is expecting for 2 strings, the second parameter you're passing is a char, not a string! I didn't understand the idea of your program, you're trying to compare what?
i am trying to compare the parameters of a program one by one to a randomly generated "password".
Well, your password in this case is a char?! cuz you're just using the first space of your string and maybe adding an invalid character, I mean look here:
>pass[0]=range[ranmb];
>pass[0]+=range[ranmb];
If in pass[0] I have 'z' and also you add again 'z' it wont be an valid char of your range array.