-1

The following is my file named crack.c:

#include <stdio.h>
#define _XOPEN_SOURCE
#include <unistd.h>

void execute(char *alpha)
{
    char *beta = crypt(alpha);
    printf("%s", beta);
}

int main(int argc, string argv[]){

        ....

        execute(argv[1]);
        else{
            printf("You submitted %d command line arguments.  That's an issue.  You need to submit exactly one.", argc);
            return 1;
        }
}

The following is what I type into the command line:

jharvard@appliance (~/Dropbox/hacker2): clang -o crack -lcrypt crack.c

The following is what the command line spits back out at me:

crack.c:8:19: warning: implicit declaration of function 'crypt' is
invalid in
      C99 [-Wimplicit-function-declaration]
    string beta = crypt(alpha);
                  ^ crack.c:8:12: warning: incompatible integer to pointer conversion initializing
      'string' (aka 'char *') with an expression of type 'int'
      [-Wint-conversion]
    string beta = crypt(alpha);
           ^      ~~~~~~~~~~~~ 2 warnings generated.

Anyone know what's going on?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
CaptainForge
  • 1,365
  • 7
  • 21
  • 46
  • `else` without `if`? o.O – haccks Jan 23 '14 at 14:50
  • 1
    Add `#include `. Also, the code presented won't compile: else without an if and missing open bracket in main. – 001 Jan 23 '14 at 14:50
  • @haccks There's an if , it just didn't copy/paste for whatever reason. Sorry, will fix. – CaptainForge Jan 23 '14 at 14:52
  • `_printf("You submitted %d command line arguments. That's an issue. You need to submit exactly one.", argc);` If you do not submit any further argument, `argc` will be `1` storing the name of your outfile. Think about s.th. like `argc - 1`. – pzaenger Jan 23 '14 at 14:57
  • 1
    `crypt` signature doesn't match the way you're calling it. What are you trying to do with it? – n. m. could be an AI Jan 23 '14 at 15:00
  • 5
    This may or may not be your problem, but, `#define _XOPEN_SOURCE 600` instead of just `#define _XOPEN_SOURCE`, and move it above *all* the headers. It doesn't do anything if it isn't at the very top of the file. – zwol Jan 23 '14 at 15:01
  • C does not have a **string** data type. If the compiler isn't spitting up on it, you're probably actually compiling as C++. C++ is a different (though related) language. – Phil Perry Jan 23 '14 at 15:09
  • @PhilPerry I'm guessing his `cs50.h` does some fugly typedeffing of `string` as `char *`. – Jite Jan 23 '14 at 15:10
  • @Phil Perry The error msg "'string' (aka 'char *') with an expression of type 'int'" appears to understand `string` as a `char *`. – chux - Reinstate Monica Jan 23 '14 at 15:10
  • Still, it would be best to understand that it's not standard (or at least, "traditional") C, and may not be widely portable. – Phil Perry Jan 23 '14 at 15:12
  • @PhilPerry Totally agree about that (hence the fugly part). I don't think it's the problem though. – Jite Jan 23 '14 at 15:13
  • 1
    Of all the comments posted, the one that is directly related to the given error is the one from @JohnnyMopp : a failure for the compiler to know what `crypt` is, and thus implicitly assuming it is a function taking an unknown number of parameters and returning `int`. Similar to the problem of casting `malloc()` without including `stdlib.h`. At least this time there is no cast, so the compiler yells. Further, it is the only one backed by the posted (albeit broken) code from the OP. – WhozCraig Jan 23 '14 at 16:17

3 Answers3

3

I had the same problem, and I realized that changing the header

#define _XOPEN_SOURCE
#include <unistd.h>

by

#define _GNU_SOURCE
#include <crypt.h>

makes disapear the error in compilation time

1

The function signature of crypt is:

char * crypt (const char *key, const char *salt)

It seems that you forgot one parameter! So your line:

string beta = crypt(alpha);

Should be something like that:

string beta = crypt(alpha, salt);
jmlemetayer
  • 4,774
  • 1
  • 32
  • 46
0

Make sure you have the define and include for crypt as the very first lines at the top of your file before any other includes.

#define _XOPEN_SOURCE
#include <unistd.h>