I have googled this and see many answers, but none fits my situation. This is my main():
char * cString;
puts("Please enter data you want to encrypt.");
cString = getInput(cString, &iStringSize);
printf("The message is: %s\n\n", cString);
char * strEncrypt;
strEncrypt = encrypt(cString, offset);
printf("The encrypted message is: %s\n\n", strEncrypt);
return 0;
This program basically reads in an arbitrary input, then encrypt it. This is the getInput function:
char * getInput(char *cString, int * iStringSize)
{
puts("Begin reading input.");
char buffer[CHUNK];
int iBufferSize;
while(fgets(buffer, CHUNK - 1, stdin) != NULL)
{
iBufferSize = strlen(buffer);
*iStringSize += iBufferSize;
cString = realloc(cString, sizeof(char) * (*iStringSize + 1));
strcat(cString, buffer);
}
printf("String size is: %d\n", *iStringSize);
puts("Reading successful");
return cString;
}
As shown above, cString is char pointer. The getInput function also returns char pointer. However, I keep getting the message: assignment makes pointer from integer without cast [enabled by default]
This happens when I compile the code.
The same happens to this function
char * encrypt(char *str, int offset)
{
puts("Begin encryption.");
char * strEncrypt = malloc(sizeof(char) * (strlen(str) + 1));
int i;
for(i = 0; i < strlen(str); i++)
{
//substitution algorithm using int offset variable.
//I accessed str and strEncrypt using pointer arithmetic
}
puts("Encryption success!");
return strEncrypt;
}
Please no suggestion on error handling of realloc.
getInput()function you are incrementingiStringSizevalue, before that it should be initialised to zero. - VinaycStringtogetInputinvoking undefined behavior. Sending it toreallocjust solidifies that improper action. Initialize it to NULL or a valid allocation result prior to callinggetInput. - WhozCraig