1

Possible Duplicate:
void pointer as argument

I'm trying to make a simple function in C, but I get an empty output and I can't figure out why:

int encrypt(unsigned char *message, char *key,  unsigned char *buffered_message)
{
    /* ... */

    buffered_message = calloc(1, (blocks * block_size));

    /* ... */

    printf("Message: %s\n", buffered_message);

    return strlen(buffered_message);
}

Inside the function, the message is printed out without problems. But when I try to use my function in main, something goes wrong.

int main()
{
    /* ... */

    unsigned char *encrypted;

    int len = encrypt(message, key, encrypted);

    if (len > 0)
    {
        printf("The encrypted message  %s\n", encrypted);
    }

    return 0;
}
Community
  • 1
  • 1
user1656466
  • 145
  • 1
  • 9
  • [See here](http://stackoverflow.com/a/12059728/596781). That answer applies verbatim to your `buffered_message`. Further duplicates: [#1](http://stackoverflow.com/q/11966454/596781), [#2](http://stackoverflow.com/a/11926448/596781), [#3](http://stackoverflow.com/a/9729853/596781). – Kerrek SB Sep 26 '12 at 09:33
  • @Kerrek So do I get this right, my buffered_message exists only in the function encrypt and thats why I cant use it in main? Sorry if it's a stupid question, but how can I get the encrypted message. I tryed to return buffered_message in encrypt, but then I get: warning: return makes pointer from integer without a cast – user1656466 Sep 26 '12 at 09:46
  • 1
    You have to pass a pointer to the pointer and dereference it in the function. Like I explained in every one of my other answers. You need to change the argument type to `unsigned char **`. – Kerrek SB Sep 26 '12 at 09:47
  • I've taken the liberty to trim your question down to the important content. – Kerrek SB Sep 26 '12 at 09:52
  • Oh, by the way, `strlen` looks dodgy. The encrypted message may well contain null bytes. Since you're encrypting entire blocks anyway, there shouldn't be a need to communicate a string length, since the caller already *knows* the expected length. – Kerrek SB Sep 26 '12 at 10:01

1 Answers1

1

You need to pass double pointer since you are allocating memory and copying to the pointer local to the function.

The following should work, though i haven't compiled.

int encrypt(unsigned char *message, char *key,  unsigned char **buffered_message)
{
    /* ... */
    *buffered_message = calloc(1, (blocks * block_size));
    /* ... */
    printf("Message: %s\n", *buffered_message);
    return strlen(*buffered_message);
}

From main

unsigned char *encrypted;
int len = encrypt(message, key, &encrypted);
printf("The encrypted message  %s\n", encrypted);
fkl
  • 5,412
  • 4
  • 28
  • 68
  • 2
    And yes, as already mentioned, using strlen is a bad idea on an encrypted message because strlen would return length upto first \0, which might be very common in an encrypted message. – fkl Sep 26 '12 at 10:06
  • 1
    The main function is wrong. You should define `unsigned char * encrypted` and then pass `&encrypted` to the function. Otherwise you end up dereferencing an unintialized pointer – Armen Tsirunyan Sep 26 '12 at 10:07
  • @ArmenTsirunyan. Yeah i only second thought that, and was editing answer on the way. Thanks for confirming. Edited the answer – fkl Sep 26 '12 at 10:10
  • Thanks everybody. I guess I have to read a litle more about pointers to pointers. But I'm very greatfull to all of you, for answering so fast. – user1656466 Sep 26 '12 at 10:58