Thread: plaguing warning messages when building project...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    plaguing warning messages when building project...

    Hello,
    I am receiving these two error mesages when I am building my project. They are:
    Code:
    31: warning: initialization makes pointer from integer without cast
    32: warning: passing argument 1 of 'strlen' from incompatible pointer type
    Here are the lines from main.c the errors are pointing to:
    Code:
    int main (int argc, char *argv[]) {
      BLOWFISH_CONTEXT context;
    
      unsigned long *input_key = strtoul(argv[1], NULL, 16);  //<-- line 31
      unsigned long key_length = strlen(input_key);           //<-- line 32
    What I believe is the corresponding function declaration in my header file:
    Code:
    void BlowfishInitialize(BLOWFISH_CONTEXT *context, unsigned long *input_key, int key_length);
    and the blow_initialize file:
    Code:
    #include "blow.h"
    #include "boxes.h"
    
    void BlowfishInitialize(BLOWFISH_CONTEXT *context, unsigned long *input_key, int key_length) {
      int i, j, k;
      unsigned long data, data_left, data_right;
    
      for (i = 0; i < 4; i++) {
        for (j = 0; j < 256; j++)
          context->S[i][j] = S_BOX[i][j];
      }
    
      j = 0;
      for (i = 0; i < N + 2; ++i) {
        data = 0x00000000;
        for (k = 0; k < 4; ++k) {
          data = (data << 8) | input_key[j];
          j = j + 1;
          if (j >= key_length)
            j = 0;
        }
        context->P[i] = P_BOX[i] ^ data;
      }
    
      data_left = 0x00000000;
      data_right = 0x00000000;
    
      for (i = 0; i < N + 2; i += 2) {
        BlowfishEncrypt(context, &data_left, &data_right);
        context->P[i] = data_left;
        context->P[i + 1] = data_right;
      }
    
      for (i = 0; i < 4; ++i) {
        for (j = 0; j < 256; j += 2) {
          BlowfishEncrypt(context, &data_left, &data_right);
          context->S[i][j] = data_left;
          context->S[i][j + 1] = data_right;
        }
      }
    }
    What I am trying to do is taking the command line argument of the key which from what I understand will be a string and "convert" it to a hex number. Can someonee help me with this? If you need more code let me know.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Code:
    unsigned long *input_key = strtoul(argv[1], NULL, 16);
    Why are you trying to use a pointer? The strtoul() function returns an unsigned long not a pointer.

    Code:
    unsigned long key_length = strlen(input_key);           //<-- line 32
    The strlen() function requires a C-string not a pointer to an unsigned long.


    Jim
    Last edited by jimblumberg; 07-12-2012 at 11:29 AM.

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by jimblumberg View Post
    Code:
    unsigned long *input_key = strtoul(argv[1], NULL, 16);
    Why are you trying to use a pointer? The strtoul() function returns an unsigned long not a pointer.

    Code:
    unsigned long key_length = strlen(input_key);           //<-- line 32
    The strlen() function requires a C-string not a pointer to an unsigned long.


    Jim
    When I remove the pointer from here:
    Code:
    unsigned long input_key = strtoul(argv[1], NULL, 16);
    here:
    Code:
    void BlowfishInitialize(BLOWFISH_CONTEXT *context, unsigned long input_key, int key_length);
    and in the blow_initialize file, I get this error
    Code:
    17: error: subscripted values is neither array nor pointer
    and it points to line 17 in the above blow_initialize code.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Show how you are trying to call your function. Plus post the complete error message exactly as it appears in your development environment. And show how BLOWFISH_CONTEXT is defined.

    Jim

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by jimblumberg View Post
    Show how you are trying to call your function. Plus post the complete error message exactly as it appears in your development environment. And show how BLOWFISH_CONTEXT is defined.

    Jim
    This is the message from the build log:
    Code:
    -------------- Build: Debug in blow ---------------
    
    Compiling: main.c
    C:\Users\Clint Sharp\Documents\C Programs\blow\main.c: In function 'main':
    C:\Users\Clint Sharp\Documents\C Programs\blow\main.c:32: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast
    c:\program files (x86)\codeblocks\mingw\bin\../lib/gcc/mingw32/4.4.1/../../../../include/string.h:49: note: expected 'const char *' but argument is of type 'long unsigned int'
    C:\Users\Clint Sharp\Documents\C Programs\blow\main.c:59: warning: passing argument 2 of 'BlowfishInitialize' makes pointer from integer without a cast
    C:\Users\Clint Sharp\Documents\C Programs\blow\blow.h:19: note: expected 'long unsigned int *' but argument is of type 'long unsigned int'
    Linking console executable: bin\Debug\blow.exe
    Output size is 52.83 KB
    Process terminated with status 0 (0 minutes, 0 seconds)
    0 errors, 2 warnings
    here is a portion of main.c:
    Code:
    int main (int argc, char *argv[]) {
      BLOWFISH_CONTEXT context;
    
      unsigned long input_key = strtoul(argv[1], NULL, 16);
      int key_length = strlen(input_key);
    
      int rc;
        char buff[BUFF_SIZE];
    
        rc = getLine ("Enter string> ", buff, sizeof(buff));
        if (rc == NO_INPUT) {
            printf ("No input\n");
            return 1;
        }
    
        if (rc == TOO_LONG) {
            printf ("Input too long\n");
            return 1;
        }
    
      char *plaintext_string = buff;
      int plaintext_length = strlen(plaintext_string);
    
      unsigned char ciphertext_buffer[BUFF_SIZE];
      unsigned char *ciphertext_string = &ciphertext_buffer[0];
      int ciphertext_length = 0;
    
      unsigned long message_left;
      unsigned long message_right;
      int block_len;
    
      BlowfishInitialize(&context, input_key, key_length);
    Here is blow.h
    Code:
    #define BLOW_H_INCLUDED
    
    #define MAXKEYBYTES 56
    #define N           16
    #define OK          0
    #define NO_INPUT    1
    #define TOO_LONG    2
    #define BUFF_SIZE   256
    
    typedef struct {
      unsigned long P[16 + 2];
      unsigned long S[4][256];
    } BLOWFISH_CONTEXT;
    
    unsigned long F(BLOWFISH_CONTEXT *context, unsigned long x);
    void BlowfishEncrypt(BLOWFISH_CONTEXT *context, unsigned long *xl, unsigned long *xr);
    void BlowfishDecrypt(BLOWFISH_CONTEXT *context, unsigned long *xl, unsigned long *xr);
    void BlowfishInitialize(BLOWFISH_CONTEXT *context, unsigned long *input_key, int key_length);
    
    #endif // BLOW_H_INCLUDED
    Here is blow_context.c
    Code:
    #include "blow.h"
    
    unsigned long F(BLOWFISH_CONTEXT *context, unsigned long x) {
      unsigned short a, b, c, d;
      unsigned long  y;
    
      d = x & 0x00FF;
      x >>= 8;
      c = x & 0x00FF;
      x >>= 8;
      b = x & 0x00FF;
      x >>= 8;
      a = x & 0x00FF;
      y = context->S[0][a] + context->S[1][b];
      y = y ^ context->S[2][c];
      y = y + context->S[3][d];
    
      return y;
    }
    And all of blow_initialize.c
    Code:
    #include "blow.h"
    #include "boxes.h"
    
    void BlowfishInitialize(BLOWFISH_CONTEXT *context, unsigned long *input_key, int key_length) {
      int i, j, k;
      unsigned long data, data_left, data_right;
    
      for (i = 0; i < 4; i++) {
        for (j = 0; j < 256; j++)
          context->S[i][j] = S_BOX[i][j];
      }
    
      j = 0;
      for (i = 0; i < N + 2; ++i) {
        data = 0x00000000;
        for (k = 0; k < 4; ++k) {
          data = (data << 8) | input_key[j];
          j = j + 1;
          if (j >= key_length)
            j = 0;
        }
        context->P[i] = P_BOX[i] ^ data;
      }
    
      data_left = 0x00000000;
      data_right = 0x00000000;
    
      for (i = 0; i < N + 2; i += 2) {
        BlowfishEncrypt(context, &data_left, &data_right);
        context->P[i] = data_left;
        context->P[i + 1] = data_right;
      }
    
      for (i = 0; i < 4; ++i) {
        for (j = 0; j < 256; j += 2) {
          BlowfishEncrypt(context, &data_left, &data_right);
          context->S[i][j] = data_left;
          context->S[i][j + 1] = data_right;
        }
      }
    }
    Thanks for your help thus far.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You need to start to learn to read your error messages. For your first error message:
    C:\Users\Clint Sharp\Documents\C Programs\blow\main.c:32: warning: passing argument 1 of 'strlen' makes pointer from integer without a cast
    c:\program files (x86)\codeblocks\mingw\bin\../lib/gcc/mingw32/4.4.1/../../../../include/string.h:49: note: expected 'const char *' but argument is of type 'long unsigned int'
    What line is this error referring to in the first line above? Line 32 and line 32 of your actual code should be
    Code:
    int key_length = strlen(input_key);
    What type of variable is being passed as argument 1 in this line (input_key)? Is it a const char *?

    For your second error:

    C:\Users\Clint Sharp\Documents\C Programs\blow\main.c:59: warning: passing argument 2 of 'BlowfishInitialize' makes pointer from integer without a cast
    C:\Users\Clint Sharp\Documents\C Programs\blow\blow.h:19: note: expected 'long unsigned int *' but argument is of type 'long unsigned int'
    And this should be:
    Code:
    BlowfishInitialize(&context, input_key, key_length);
    And let's look at this functions signature:
    Code:
    void BlowfishInitialize(BLOWFISH_CONTEXT *context, unsigned long *input_key, int key_length);
    Look closely at how you are calling your function, look at argument 1 versus argument 2 do you see any difference?

    Jim

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by jimblumberg View Post
    You need to start to learn to read your error messages. For your first error message:

    What line is this error referring to in the first line above? Line 32 and line 32 of your actual code should be
    Code:
    int key_length = strlen(input_key);
    What type of variable is being passed as argument 1 in this line (input_key)? Is it a const char *?

    For your second error:


    And this should be:
    Code:
    BlowfishInitialize(&context, input_key, key_length);
    And let's look at this functions signature:
    Code:
    void BlowfishInitialize(BLOWFISH_CONTEXT *context, unsigned long *input_key, int key_length);
    Look closely at how you are calling your function, look at argument 1 versus argument 2 do you see any difference?

    Jim
    Well changed everrything back to char and got no build errors. But now I am right back to entering a string and not a hex number. Not sure how I would do that. Any idea?

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Post your current code.

    Jim

  9. #9
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by jimblumberg View Post
    Post your current code.

    Jim
    Here is main.c
    Code:
    int main (int argc, char *argv[]) {
      BLOWFISH_CONTEXT context;
    
      char *input_key = argv[1];
      int key_length = strlen(input_key);
    
      int rc;
        char buff[BUFF_SIZE];
    
        rc = getLine ("Enter strings> ", buff, sizeof(buff));
        if (rc == NO_INPUT) {
            printf ("No input\n");
            return 1;
        }
    
        if (rc == TOO_LONG) {
            printf ("Input too long\n");
            return 1;
        }
    
      char *plaintext_string = buff;
      int plaintext_length = strlen(plaintext_string);
    
      unsigned char ciphertext_buffer[BUFF_SIZE];
      unsigned char *ciphertext_string = &ciphertext_buffer[0];
      int ciphertext_length = 0;
    
      unsigned long message_left;
      unsigned long message_right;
      int block_len;
    
      BlowfishInitialize(&context, input_key, key_length);
    and blow.h
    Code:
    #define BLOW_H_INCLUDED
    
    #define MAXKEYBYTES 56
    #define N           16
    #define OK          0
    #define NO_INPUT    1
    #define TOO_LONG    2
    #define BUFF_SIZE   256
    
    typedef struct {
      unsigned long P[16 + 2];
      unsigned long S[4][256];
    } BLOWFISH_CONTEXT;
    
    unsigned long F(BLOWFISH_CONTEXT *context, unsigned long x);
    void BlowfishEncrypt(BLOWFISH_CONTEXT *context, unsigned long *xl, unsigned long *xr);
    void BlowfishDecrypt(BLOWFISH_CONTEXT *context, unsigned long *xl, unsigned long *xr);
    void BlowfishInitialize(BLOWFISH_CONTEXT *context, char *input_key, int key_length);
    
    #endif // BLOW_H_INCLUDED
    and blow_initialize.c
    Code:
    #include "blow.h"
    #include "boxes.h"
    
    void BlowfishInitialize(BLOWFISH_CONTEXT *context, char *input_key, int key_length) {
      int i, j, k;
      unsigned long data, data_left, data_right;
    
      for (i = 0; i < 4; i++) {
        for (j = 0; j < 256; j++)
          context->S[i][j] = S_BOX[i][j];
      }
    
      j = 0;
      for (i = 0; i < N + 2; ++i) {
        data = 0x00000000;
        for (k = 0; k < 4; ++k) {
          data = (data << 8) | input_key[j];
          j = j + 1;
          if (j >= key_length)
            j = 0;
        }
        context->P[i] = P_BOX[i] ^ data;
      }
    
      data_left = 0x00000000;
      data_right = 0x00000000;
    
      for (i = 0; i < N + 2; i += 2) {
        BlowfishEncrypt(context, &data_left, &data_right);
        context->P[i] = data_left;
        context->P[i + 1] = data_right;
      }
    
      for (i = 0; i < 4; ++i) {
        for (j = 0; j < 256; j += 2) {
          BlowfishEncrypt(context, &data_left, &data_right);
          context->S[i][j] = data_left;
          context->S[i][j + 1] = data_right;
        }
      }
    }
    and finally although probably not needed blow_context.c
    Code:
    #include "blow.h"
    
    unsigned long F(BLOWFISH_CONTEXT *context, unsigned long x) {
      unsigned short a, b, c, d;
      unsigned long  y;
    
      d = x & 0x00FF;
      x >>= 8;
      c = x & 0x00FF;
      x >>= 8;
      b = x & 0x00FF;
      x >>= 8;
      a = x & 0x00FF;
      y = context->S[0][a] + context->S[1][b];
      y = y ^ context->S[2][c];
      y = y + context->S[3][d];
    
      return y;
    }
    I'm thinking of taking the input of the hex key in the current string format, changing that string to it hex number equivalent and making that hex number back into a string.
    i.e.
    9abcdef012345678 is entered as a string, convert that into is hex equivalent and then reintroduce that equivalent as a string again.
    Am I on the right track? Is there a function that might do this? If I am not do you have an idea?

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    What exactly do you mean by hex equivalent? Give an example of the input parameter and then what the hex equivalent of that parameter should be.

    Does the above code compile without warnings or errors?

    What include files are you including?

    What exactly are you tryint to accomplish with the following?
    Code:
    char *input_key = argv[1];
      int key_length = strlen(input_key);
    What is the purpose of your input_key variable? Why can't you just use argv[1] in your strlen() call and your function call?

    Code:
      int key_length = strlen(argv[1]);
    ...
    BlowfishInitialize(&context, argv[1], key_length);

    Jim

  11. #11
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by jimblumberg View Post
    What exactly do you mean by hex equivalent? Give an example of the input parameter and then what the hex equivalent of that parameter should be.

    Does the above code compile without warnings or errors?

    What include files are you including?

    What exactly are you tryint to accomplish with the following?
    Code:
    char *input_key = argv[1];
      int key_length = strlen(input_key);
    What is the purpose of your input_key variable? Why can't you just use argv[1] in your strlen() call and your function call?

    Code:
      int key_length = strlen(argv[1]);
    ...
    BlowfishInitialize(&context, argv[1], key_length);

    Jim
    Yes, no compilation error whatsoever. The instructions are to take a single line command arguement of a key in HEX. It then reads and ASCII messge until end of file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Warning messages compiling C program
    By puth in forum C Programming
    Replies: 12
    Last Post: 09-09-2010, 08:57 AM
  2. Warning when building!
    By electrolove in forum C Programming
    Replies: 21
    Last Post: 02-12-2003, 09:39 PM
  3. How do I get Rid of Warning messages after I compile??
    By BBain80 in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2002, 01:45 PM
  4. Warning Messages
    By Isometric in forum Windows Programming
    Replies: 2
    Last Post: 11-25-2001, 01:24 AM
  5. problems with too many warning messages?
    By Isometric in forum C Programming
    Replies: 9
    Last Post: 11-25-2001, 01:23 AM