pointer w/o a cast.

This is a discussion on pointer w/o a cast. within the C Programming forums, part of the General Programming Boards category; what I am trying to do is make a program where if the first argument is equal to a randomly ...

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

    pointer w/o a cast.

    what I am trying to do is make a program where if the first argument is equal to a randomly generated "password", it will printf that you got through, I keep getting strcmp arg 2 makes pointer without a cast error. Here is the code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(int argc, char *argv[]) {
    	char pass[20], *pas;
    	int ranmb, len, x;
    	char range[] = "abcdefghijklmnopqrstuvwxyz1234567890", *rang;
    	char lenrange[] = { 1, 2, 3, 4, 5 }, *lenrang;
    	pas = pass;
    	lenrang = lenrange;
    	rang = range;
    	if(argc!=2) {
    		printf("Enter the password as an argument to enter, \n");
    		printf("It is also case sensitive.\n");
    		exit(0);
    	}
    	srand((unsigned)time(NULL));
    	ranmb=rand()%37;
    	len=rand()%6;
    	for(x=0;x<*(lenrang+len);x++) {
    		*pas=*(rang+ranmb);
    		ranmb=rand()%37;
    	}
    	if(strcmp(argv[1],*pas)==0) {
    		printf("You got through!");
    		exit(0);
    	}
    	else {
    		printf("Enter the password as an argument to enter, \n");
    		printf("It is also case sensitive.\n");
    		exit(0);
    	}
    }

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    you have to cast, put (char*) in front of both arguments of strcmp.

    and include string.h for strcmp.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by no-one
    you have to cast, put (char*) in front of both arguments of strcmp.

    and include string.h for strcmp.
    No. You're wrong. The real problem is the fact that they're dereferencing a pointer:
    char pass[20], *pas;
    ....
    f(strcmp(argv[1],*pas)==0) {
    Don't dereference it. Remove the * from the function call.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You also forgot to #include <string.h>

    One of the loops ain't too clever, particularly where it does this:
    *pas=*(rang+ranmb);

    You only ever update *pas, which is the first character in the array. So you never make the string longer than one character, and you also never terminate it with a \0.

    What's the point of the lenrange array, when it's just filled with 1 - 5 ? It seems like this is an overkill:
    *(lenrang+len)

    This is a simple example that prints 5 random characters from the array:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define ASIZE(a) (sizeof(a)/sizeof(a[0]))
    
    int main(void) 
    {
      char range[] = "abcdefghijklmnopqrstuvwxyz1234567890";
      char buf[BUFSIZ];
      int i, num;
      
      for (i = 0; i < 5; i++)
      {
        num = rand() % (ASIZE(range) - 1);
        buf[i] = range[num];
      }
      
      buf[i] = '\0';
      puts (buf);    	
    	
      return 0;
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. test void pointer before cast (instanceof)
    By jeanluca in forum C Programming
    Replies: 7
    Last Post: 05-29-2009, 04:26 AM
  2. Pointer from a cast warning
    By swgh in forum C Programming
    Replies: 4
    Last Post: 07-30-2007, 08:38 AM
  3. Including The Right DLLs
    By bumfluff in forum Game Programming
    Replies: 8
    Last Post: 12-28-2006, 02:32 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. how to cast a char *mystring to a structure pointer ??
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 07:59 AM

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