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
. 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
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.
So use %d
or %u
or %zu
as a access specifier in printf()
statement.
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
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.