使用C中for循环中无法正常工作的变量定义字符串的大小

时间:2021-09-20 18:15:24

I'm trying to reverse the string input entered by user, the problem here is in function *rev, when I use size = strlen(STR); to get the length of the string and pass it into the size of the revS[size] the program outputs some garbage value for reverse string! if I pass some value instead if size in revS[10] and run the program it works as expected. I Have checked the value of size as

我试图反转用户输入的字符串输入,这里的问题是函数* rev,当我使用size = strlen(STR);获取字符串的长度并将其传递给revS [size]的大小,程序输出一些反向字符串的垃圾值!如果我传递一些值,如果大小在revS [10]并运行程序,它按预期工作。我已经检查了尺寸的值

printf("\nlength of string is: %d\n",size);

and it gives the correct value. I'm not getting where is it going wrong!

它给出了正确的价值。我不知道哪里出错了!

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

char *rev(char *);

int main()
{
    char string[100];
    printf("Enter the string to reverse: ");
    scanf("%s", string);
    printf("You entered string : %s\n Reversed string is: %s", string, rev(string));
}


char *rev(char *STR)
{
    int size, i, j = 0;
    size = strlen(STR);
    printf("\nlength of string is: %d\n", size);
    char revS[size];

    for(i = size-1; i >= 0; i--)
    {
        revS[j] = STR[i];
        j = j + 1;
    }

    revS[j] = '\0';
    return (revS);  
}

OUTPUT:

OUTPUT:

Enter the string to reverse: mahaveer

length of string is: 8
You entered string : mahaveer
 Reversed string is: ╚²b
--------------------------------
Process exited after 28.7 seconds with return value 0
Press any key to continue . . .

2 个解决方案

#1


1  

The issue is that your reversed string is allocated on the stack rather than the heap. When your rev function returns, all of the variables in that scope will be garbage collected. You can use malloc() to allocate memory dynamically on the heap. Note that the caller is responsible for calling free() on the string to avoid a memory leak.

问题是你的反向字符串是在堆栈而不是堆上分配的。当您的rev函数返回时,该范围中的所有变量都将被垃圾回收。您可以使用malloc()在堆上动态分配内存。请注意,调用者负责调用字符串上的free()以避免内存泄漏。

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

char *rev(char *);

int main() {
    char string[100];
    printf("Enter the string to reverse: ");
    scanf("%s", string);
    char *r = rev(string);
    printf("You entered string: %s\nReversed string is: %s\n", string, r);
    free(r);
}

char *rev(char *str) {
    int i, j;
    int size = strlen(str);
    char *rev = malloc(sizeof(*rev) * (size + 1));

    for (i = size - 1, j = 0; i >= 0; i--, j++) {
        rev[j] = str[i];
    }

    rev[size] = '\0';
    return rev;
}

Note that this code is susceptible to buffer overflows.

请注意,此代码容易受到缓冲区溢出的影响。

Here's a repl to play with.

这是一个可以玩的人。

#2


0  

You have tho major UBs here. First you allocate local storage array which is not available after the function return. The second one - the size is too small to accomodate the string plus terminating zero

你在这里有主要的UB。首先分配本地存储阵列,该函数在函数返回后不可用。第二个 - 大小太小,无法容纳字符串加上终止零

#1


1  

The issue is that your reversed string is allocated on the stack rather than the heap. When your rev function returns, all of the variables in that scope will be garbage collected. You can use malloc() to allocate memory dynamically on the heap. Note that the caller is responsible for calling free() on the string to avoid a memory leak.

问题是你的反向字符串是在堆栈而不是堆上分配的。当您的rev函数返回时,该范围中的所有变量都将被垃圾回收。您可以使用malloc()在堆上动态分配内存。请注意,调用者负责调用字符串上的free()以避免内存泄漏。

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

char *rev(char *);

int main() {
    char string[100];
    printf("Enter the string to reverse: ");
    scanf("%s", string);
    char *r = rev(string);
    printf("You entered string: %s\nReversed string is: %s\n", string, r);
    free(r);
}

char *rev(char *str) {
    int i, j;
    int size = strlen(str);
    char *rev = malloc(sizeof(*rev) * (size + 1));

    for (i = size - 1, j = 0; i >= 0; i--, j++) {
        rev[j] = str[i];
    }

    rev[size] = '\0';
    return rev;
}

Note that this code is susceptible to buffer overflows.

请注意,此代码容易受到缓冲区溢出的影响。

Here's a repl to play with.

这是一个可以玩的人。

#2


0  

You have tho major UBs here. First you allocate local storage array which is not available after the function return. The second one - the size is too small to accomodate the string plus terminating zero

你在这里有主要的UB。首先分配本地存储阵列,该函数在函数返回后不可用。第二个 - 大小太小,无法容纳字符串加上终止零