assignment makes pointer from integer without cast [enabled by default]

匿名 (未验证) 提交于 2019-12-03 02:41:02

问题:

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.

回答1:

Declare getInpyt and encrypt function before their first use otherwise compiler assumes these functions return int and accept variable number of parameters.

You can declare them by using any one of 3 ways given below.

  1. Include appropriate header files containing their declarations.
  2. Include declarations manually in global scope or local scope before you first access them.
  3. Move the definition of these functions before you first access them.


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!