简体   繁体   English

赋值从没有强制转换的指针中获取整数

[英]Assignment makes integer from a pointer without a cast

int main(int argc, char *argv[]) {
    if(argc!=3) {
        printf("You must pass exactly three para \n");
        return 0; 
    }

    char *buffer = argv[1];
    //printf("The length of the buffer string is %d\n",buflen);
    char *mystring = argv[2];
    //printf("The length of the user string is %d\n",len);
    addstring(buffer, mystring);
    return 0; 
}

int addstring(char *buffer, char *mystring)
{
    int buflen = strlen(buffer);
    int len = strlen(mystring);
    char *dest;
    *dest = (char *)malloc(buflen + len + 1);
    printf("The size of destination is %lu\n",sizeof(dest));

    dest = strcpy(dest,buffer);
    dest = (dest + buflen);
    dest = strcpy(dest,mystring);
    printf("The final string is %p",dest);
    return 0;
}

In the above code, the function addstring(..) shoes this error Assignment makes integer from a pointer without a cast . 在上面的代码中,函数addstring(..)此错误Assignment makes integer from a pointer without a castAssignment makes integer from a pointer without a cast的值addstring(..) Assignment makes integer from a pointer without a cast addstring(..) I know I'm taking the value of a pointer and putting it in integer, but how may I do it to resolve this error? 我知道我要获取一个指针的值并将其放在整数中,但是如何解决该错误呢?

Even after changing *dest to dest , your function addstring is not works properly.. Simply try like this 即使将*dest更改为dest ,您的函数addstring也无法正常工作。

int addstring(char *buffer, char *mystring)
{
 int buflen = strlen(buffer);
 int len = strlen(mystring);
 char *dest;
 dest = (char *)malloc(buflen + len + 1);
 printf("The size of destination is %d\n",sizeof(dest));

 strcpy(dest,buffer);
 strcat(dest,mystring);
 printf("The final string is %s\n",dest);
 return 0;
}

You have done 你做完了

*dest = (char *)malloc(buflen + len + 1); 

instead of 代替

dest =malloc(buflen + len + 1);

Your program saying warning to me for this line 您的程序为此行对我说了警告

    printf("The size of destination is %lu\n",sizeof(dest));

sizeof() return type is not long unsigned int. sizeof()返回类型不是long unsigned int。

So use %d or %u or %zu as a access specifier in printf() statement. 因此,在printf()语句中将%d%u%zu用作访问说明符。

change 更改

char *dest;
*dest = (char *)malloc(buflen + len + 1);

to

char *dest;
dest = (char *)malloc(buflen + len + 1);

EDIT: As @POW said, you need not cast the result of malloc 编辑:正如@POW所说,您无需强制转换malloc的结果

There are multiple issue in your code. 您的代码中存在多个问题。 Please check the below code 请检查以下代码

int addstring(char *buffer, char *mystring)
{
   int buflen = strlen(buffer);
   int len = strlen(mystring);
   char *dest;
   /* No need to type-cast the malloc() */
   dest = malloc(buflen + len + 1); /* *dest holds the value, dest holds the address */
   printf("The size of destination is %lu\n",sizeof(dest));

   strcpy(dest,buffer);
   strcpy(dest+buflen,mystring);/* copy the second string to dest after buffer is copied */
   printf("The final string is %s\n",dest); /*To print a string use %s, %p is to print pointer*/
   return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM