pointers again

This is a discussion on pointers again within the C Programming forums, part of the General Programming Boards category; everytime I try to compile this program I get this error: warning: cast to pointer from integer of different size. ...

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    pointers again

    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?

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,670
    You'll have to post all the code if you want to know what's really going on.

    gg

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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]

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    here is the code
    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);
    		}
    	}
    }
    still working on, thanx for the help so far.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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.

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    don't get it, could you explain please, thanx.

  8. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >>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.

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,670
    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

  10. #10
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    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.

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,670
    Post your latest attempt and we'll go from there.

    gg

  12. #12
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    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;
    }

  13. #13
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >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?

  14. #14
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    i am trying to compare the parameters of a program one by one to a randomly generated "password".

  15. #15
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21