简体   繁体   English

如何通过C中的function反转字符串数组中的每一个字符串?

[英]How to reverse every string in an array of strings through a function in C?

I have been trying to solve this issue for whole day, and could not do it on my own.我一整天都在尝试解决这个问题,但我自己无法解决。 Searching the inte.net didn't help me solve it either搜索 inte.net 也没有帮助我解决它

So, this the function prototype:所以,这是 function 原型:

void invert(char **arr, int n);

First argument is an array of strings, and the second one is number of strings in an array.第一个参数是字符串数组,第二个参数是数组中的字符串数。

This is my code:这是我的代码:

#include <stdio.h>
#include <string.h>
void invert(char** arr, int n)
{
    int i, j, len;
        for(j=0;j<n;j++)
        {
            len=strlen(arr[j]);
             for(i=0;i<len/2;i++)
                {
                    char tmp = arr[j][i];             
                    arr[j][i] = arr[j][len - i - 1]; 
                    arr[j][len - i - 1] = tmp; 
                }
        }
}
int main()
{
    int n=3, i;
    char **arr;
    arr[0]="John";
    arr[1]="Doe";
    arr[2]="Programmer";
    invert(arr, n);
    for(i=0;i<3;i++)
    {
        printf("%s ",arr[i]);
    }
}

The code breaks when it reaches the line:代码在到达该行时中断:

arr[j][i] = arr[j][len - i - 1];

and I can't figure out why.我不知道为什么。 The function receives an array of strings perfectly (tested it with some printf statements for characters of specific strings), and the char tmp succesfully recieves a correct character, but the program crashed when it reaches the line mentioned earlier. function 完美接收了一个字符串数组(针对特定字符串的字符用一些 printf 语句进行了测试), char tmp成功接收到一个正确的字符,但是程序在到达前面提到的那一行时崩溃了。 Printf statements after that line don't work.该行之后的 Printf 语句不起作用。

Did I miss anything?我错过了什么吗? Can someone explain what am I doing wrong?有人可以解释我做错了什么吗? Thank you!谢谢!

For starters this code snippet对于初学者这个代码片段

char **arr;
arr[0]="John";
arr[1]="Doe";
arr[2]="Programmer";

invokes undefined behavior because the pointer arr is uninitialized and has an indeterminate value.调用未定义的行为,因为指针arr未初始化并且具有不确定的值。

Moreover this approach in any case is wrong because you may not change string literals.此外,这种方法在任何情况下都是错误的,因为您可能不会更改字符串文字。

What you need is to declare a two-dimensional array as for example您需要声明一个二维数组,例如

enum { N = 11 };

//...

char arr[3][N] =
{
    "John", "Doe", "Programmer"
};

In this case the function declaration will look like在这种情况下,function 声明将如下所示

void invert( char arr[][N], int n );

The enumeration must be declared before the function declaration.枚举必须在function声明之前声明。

Instead of the two-dimensional array you could declare an array of pointers like您可以声明一个指针数组,而不是二维数组

char s1[] = "John";
char s2[] = "Doe";
char s3[] = "Programmer";
char * arr[3] = { s1, s2, s3 };

In this case the function declaration may be as shown in your question在这种情况下,function 声明可能如您的问题所示

void invert(char** arr, int n)

So what you need to do with minimal changes is to substitute this code snippet所以你需要做的是用最小的变化来替换这个代码片段

char **arr;
arr[0]="John";
arr[1]="Doe";
arr[2]="Programmer";

for this code snippet对于这个代码片段

char s1[] = "John";
char s2[] = "Doe";
char s3[] = "Programmer";
char * arr[3] = { s1, s2, s3 };

To begin with, what you have here:首先,你在这里有什么:

   char **arr;

is a pointer to pointer to char .是指向 char 的指针

Secondly, even if you had an array of pointers to char , like so:其次,即使您有一个指向 char 的指针数组,如下所示:

char *arr[3];

And then assigning each string literal :然后分配每个字符串文字

arr[0]="John";
arr[1]="Doe";
arr[2]="Programmer";

would still invoke Undefined behavior , since you are attempting to modify a string literal which is read only .仍会调用未定义的行为,因为您正试图修改只读字符串文字

What you need is, either a 2D array of chars :您需要的是二维字符数组

char arr[][100] = {"John", "Doe", "Programmer"};

and also change the function signature to:并将 function 签名更改为:

void invert(char arr[][100], int n)

or you have to dynamically allocate memory and use a function like strcpy(), strdup(), memcpy() etc:或者您必须动态分配 memory 并使用 function,例如strcpy(), strdup(), memcpy()等:

char **arr;

arr = malloc(n * sizeof(char *)); // or sizeof(*arr)

if (arr == NULL) {
    fprintf(stderr, "Malloc failed to allocate memory\n");
    exit(1);
}

arr[0] = strdup("John"); // good idea to also check if strdup returned null
arr[1] = strdup("Doe");
arr[2] = strdup("Programmer");

invert(arr, n);
for(i=0;i<3;i++)
{
    printf("%s ",arr[i]);
}

for (i = 0; i < 3; i++) {
    free(arr[i]);
}

free(arr);

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

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