c - 反转输入字符串的函数。显示反转的字符串,但只有指针,没有括号[],没有库。函数将在内存中更改

标签 c pointers memory reverse

int *i;

ters_cevir(){

    char *term=i;
    char *som=i;
    char som1;
    while (*term != '\0') { term++; }

    while (*som != '\0') {
        som1=som*;
        *term=som;
        term--;
        som++;
    }
}

int main() {   
    char *isim=malloc(sizeof(char)); 
    i=&isim;
    printf("Reverse words=");
    scanf("%s",isim);
    printf("Kelimenizin tersi:\n ");
    ters_cevir(); // When I call this, it must make the reverse one that make from memory
    while (*isim != '\0') {
        printf("%c",*isim);
        isim++;
        sayac++;
    }
    return 0;
}

最佳答案

您好,我已经修改了您的代码。请参阅下面也请参阅我的评论:-

void ters_cevir(char *isim){

    char *term=isim;
    //char *som=isim;
    //char som1;
    while (*isim != '\0') { isim++; }

    while (*term != '\0') {
        //som1=som*;
        *--isim=*term++//isim was pointing to the null character so we are pre decrement that pointer  and post decrement term
                    //here we are coping the string in reverse order in isim
        //term--;
        //som++;
    }
}

int main() {   
    char *isim=malloc(50);//you need enough space to store a string. you have just allocated only one byte which was not enough
    //i=&isim;
    printf("Reverse words=");
    scanf("%s",isim);
    printf("Kelimenizin tersi:\n ");
    ters_cevir(isim); // now it will work fine. Here you are passing the address of isim
    while (*isim != '\0') {
        printf("%c",*isim);
        isim++;
        sayac++;
    }
    return 0;
}

关于c - 反转输入字符串的函数。显示反转的字符串,但只有指针,没有括号[],没有库。函数将在内存中更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49372981/

相关文章:

windows - 是否可以从一个应用程序访问另一个应用程序的内存?如何?

memory - Go 中的磁盘备份数组

c - 在 C 中获取 inf 输出的简单精度测试

c - 中断 vector 处理程序地址

c - 为什么 fscanf 不转换 set 然后转换 char?

c++ - 应该如何设计两个紧密相关的数据结构的所有权?

c - 在c中求gcd的欧几里得程序

c - 对数组指针自增的疑问

java - Android内存限制测试问题?

c - 段错误无效内存 0x0