Code:
int encrypt (char * originaltext)
{
char * ciphertext;
if (ciphertext)
{
ciphertext = (char *)malloc(100);
*ciphertext = *originaltext + 10;
return *originaltext + 10;
}
}
Your if statement doesn't make sense; not where it is at. If you're checking to see if your malloc() succeeded, you should do something like this.
Code:
char* string_ptr;
string_ptr = malloc( sizeof( *string_ptr ) * string_length ); // no casting malloc(), m'kay?
if( string_ptr == NULL )
{
fprintf( stderr, "malloc() for string_ptr failed\n" );
exit( EXIT_FAILURE );
}
You're not doing what you think you are, here.
Code:
*ciphertext = *originaltext + 10;
What this will do is assign the decimal char value of the first character in originaltext string + 10 to the memory location pointed to by ciphertext. Observe what the following code does.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int
main( void )
{
char* string_ptr;
string_ptr = malloc( sizeof( *string_ptr ) * 100 );
strncpy( string_ptr, "Quick red fox jumps over lazy brown dog.", 100 );
fprintf( stdout, "Printing original string:\n%s\n\n", string_ptr );
*string_ptr = *string_ptr + 10; // 'Q' just became '['
fprintf( stdout, "Printing modified string:\n%s\n\n", string_ptr );
return 0;
}
What you don't seem to realize is that originaltext and ciphertext are both pointers to the beginning of the block you malloc()'ed.