简体   繁体   English

C语言中奇怪的printf输出

[英]Strange printf output in C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char * reverse(char *string);
int main(int argc, char *argv[])
{
    char array[10];
    array[0] = 'a';
    array[1] = 'b';
    array[2] = 'c';
    array[3] = 'd';
    array[4] = 'e';
    printf("1%s\n",array);
    char *p = reverse(array);
    printf("4%s\n",p);
    printf("5%s\n",array);
}

char * reverse(char *string)
{
    int size = strlen(string);
    char reversed[size];
    int i;
    int j = 0;
    for(i = size-1; i >= 0; i--)
    {
        reversed[j] = string[i];
        j++;
    }
    printf("2%s\n",reversed);
    string = reversed;
    printf("3%s\n",string);
    return reversed;
}

This code basically just initializes an array of values and passes it into a method that reverses these values. 这段代码基本上只是初始化一个值数组,然后将其传递给反转这些值的方法。

I am not sure if this is the best way to execute the task, since I am new to pointers and arrays in C. 我不确定这是否是执行任务的最佳方法,因为我是C语言中的指针和数组的新手。

But the real question is this: 但是真正的问题是这样的:

Can anyone figure out why in this line 任何人都可以找出原因

printf("4%s\n",p);

if you remove the preceding '4', so it looks like so 如果您删除前面的“ 4”,则看起来像这样

printf("%s\n",p);

the line won't print at all? 该行根本不会打印?

You are returning a pointer to local variable( reversed ) in the function reverse the question should actually be: Why did it work in the first place? 您要在函数reverse中返回指向局部变量( reversed )的指针,实际上该问题应该是: Why did it work in the first place? .

This code string = reversed; 此代码string = reversed; will only copy the pointer, and again the local copy of the pointer so it has no effect outside the function. 只会复制该指针,再复制该指针的本地副本,因此它不会在函数外部起作用。

To reverse a string you don't need additional memory - this can be done in-place. 要反转一个字符串,您不需要额外的内存-这可以就地完成。

Strings in C must end with the null character. C中的字符串必须以空字符结尾。 You're using strlen on a non null-terminated string. 您在非以null终止的字符串上使用strlen。

Furthermore, you just a very lucky person, because there is a serious problem with you code: you forget to add \\0 symbol at the end of string. 此外,您只是一个非常幸运的人,因为您的代码存在严重问题:您忘记在字符串的末尾添加\\0符号。

UPD: the main problem is with code line char reversed[size]; UPD:主要问题是代码行char reversed[size]; . It's a regular local variable, it has automatic duration, which means that it springs into existence when the function is called and disappears when the function returns (see this link) . 这是一个常规的局部变量,具有自动持续时间,这意味着它在调用函数时就存在,而在函数返回时就消失(请参阅此链接)

You need to change it to: 您需要将其更改为:

char *reversed = malloc((size+1)*sizeof(char));

UPD-2: another bug fixing will be: UPD-2:另一个错误修复是:

1) add array[5] = '\\0'; 1)添加array[5] = '\\0'; after all other array initializing lines 在所有其他数组初始化行之后

2) add reversed[j] = '\\0'; 2)添加reversed[j] = '\\0'; after for...loop : 之后for...loop

for(i = size-1; i >= 0; i--)
{
    reversed[j] = string[i];
    j++;
}
reversed[j] = '\0';

UPD-3: But in general it will much more correctly initialize your string in appropriate way: UPD-3:但通常,它将以适当的方式更正确地初始化您的字符串:

char array[10] = "abcde";

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

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