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; } 
5
  • See here. That answer applies verbatim to your buffered_message. Further duplicates: #1, #2, #3. Commented Sep 26, 2012 at 9: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 Commented Sep 26, 2012 at 9: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 **. Commented Sep 26, 2012 at 9:47
  • I've taken the liberty to trim your question down to the important content. Commented Sep 26, 2012 at 9: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. Commented Sep 26, 2012 at 10:01

1 Answer 1

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); 
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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
@ArmenTsirunyan. Yeah i only second thought that, and was editing answer on the way. Thanks for confirming. Edited the answer
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.