A program, always paragraph error, hope you can help me to see where there is a problem?

-sharpinclude <stdio.h>
-sharpinclude <stdlib.h>
-sharpinclude <string.h>
int getmax(int a, int b)
{
    if(a>b)
    {
        return a;
    }else
    {
        return b;
    }
}

void  swap(void * a, void * b)
{
    void * tmp;
    int la=sizeof(a);
    int lb=sizeof(b);
    int max=getmax(la,lb);    
    void * t=(void *)malloc(max);
    if(t ==NULL)
    {
        printf("%s\n","");
        exit(0);
    }
    memcpy(t,a,max);
    memcpy(a,b,max);
    memcpy(b,t,max);
    printf("%d\n",sizeof(a));
    printf("%d\n",sizeof(b));
    printf("%d\n",max);
    free(t);

}

int main()
{
    char * str="123";
    char * str2="456";
    swap((void *)str,(void *)str2);
    printf("%s\n",str);
    printf("%s\n",str2);

}

C
Mar.24,2021

char * str="123";
char * str2="456";

the two strings requested in this way are actually two pointers str and str2 pointing to two string constants respectively.

so when you execute this code:

memcpy(a,b,max);
memcpy(b,t,max);

will report an error because you are trying to modify the string constant.

< hr >

in addition, if you give two strings of different length, your program will also make mistakes. This is left to you to think about and solve

.

if you just want to compile and pass, you can make the following changes:

  char * str="123";
  char * str2="456";


modify to

char str1[]="123";
char str2[]="456";
        
    

secondly, memcpy should not use max, twice when copying. You should use lb, once and la once. Do you want to check their sizes before memcpy, such as char str1 [3] = "12" str2 [4] = "123", so there is a problem with the exchange?
there must be a lot of things that haven't been taken into account.


this is my modified program, which is running normally for the time being.

-sharpinclude <stdio.h>
-sharpinclude <stdlib.h>
-sharpinclude <string.h>
-sharpinclude <assert.h>
int getmax(int a, int b)
{
    if(a>b)
    {
        return a;
    }else
    {
        return b;
    }
}

void  swap(void * a, void * b,int la, int lb)
{
    void * tmp;
    int max=getmax(la,lb);    
    void * t=(void *)malloc(max);
    if(t == NULL)
    {
        printf("%s\n","");
        exit(0);
    }
    memcpy(t,a,max);
    memcpy(a,b,max);
    memcpy(b,t,max);
    
    free(t);

}

int main()
{
    char str[]="123456789";
    char str2[]="abcd";
    printf("%s\n",(char *)str);
    printf("%s\n",(char *)str2);

    swap((void *)str,(void *)str2, sizeof(str), sizeof(str2));
    printf("%s\n",(char *)str);
    printf("%s\n",(char *)str2);

}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b409d6-2c51c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b409d6-2c51c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?