赋值使指针从整数开始而不进行强制转换[默认启用] [英] assignment makes pointer from integer without cast [enabled by default]

查看:742
本文介绍了赋值使指针从整数开始而不进行强制转换[默认启用]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用谷歌搜索了,看到了很多答案,但是都不符合我的情况.这是我的main():

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;

该程序基本上读取任意输入,然后对其进行加密.这是getInput函数:

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;
}

如上所示,cString是char指针. getInput函数还返回char指针.但是,我不断收到消息:赋值使指针从整数开始而没有强制转换[默认启用]

当我编译代码时会发生这种情况.

This happens when I compile the code.

此功能同样发生

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;
}

请不要对realloc的错误处理提出建议.

Please no suggestion on error handling of realloc.

推荐答案

在首次使用之前声明getInpyt并加密函数,否则编译器会假定这些函数返回int并接受可变数量的参数.

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. 包括包含其声明的适当头文件.
  2. 在您首次访问声明之前,先在全局范围或本地范围中手动添加声明.
  3. 在您首次访问这些功能之前,请先移动它们的定义.

这篇关于赋值使指针从整数开始而不进行强制转换[默认启用]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆