简体   繁体   English

我正在尝试编写C代码对String进行排序,但始终在第13行显示错误消息

[英]I'm trying to write a C code to sort String, but there always shows an error message in line 13

I'm trying to write a C code to sort strings, but there always shows an error message in line 13. 我正在尝试编写C代码对字符串进行排序,但始终在第13行显示错误消息。

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

void SortString(char *strings[], int size)
{
    char temp[10];
    for(int i =0; i < size -1; i++)
        for(int j = i+1; j<size; j++)
        {
            if (strcmp(strings[i], strings[j])>0)
            {
                strcpy(temp, strings[i]);
                strcpy(strings[i], strings[j]); //Error: Thread 1: EXC_BAD_ACCESS (code=2, address=0x100000fa6)
               strcpy(strings[j], temp);
           }
       }    }
   int main(){
   char *names[] = {"D", "C", "B", "A"};
   SortString(names, 4);    }

I know I can change *name[] into name[][20] and change void SortString(char *strings[], int size) to void SortString(char strings[][20], int size) to make the code correct, but why *name[] is wrong? 我知道我可以将*name[]更改为name[][20]然后将void SortString(char *strings[], int size)更改为void SortString(char strings[][20], int size)以使代码正确,但是* name []为什么错误?

I am referring to this page. 我指的是这个页面。

char *names[] = {"D", "C", "B", "A"};

When you declare strings like this, they will be present in a read-only memory. 当您声明这样的字符串时,它们将出现在只读存储器中。 You are trying to modify the content of the memory in your function and that is why you are getting the error. 您正在尝试修改函数中内存的内容,这就是为什么您会收到错误消息。

Best way to achieve this functionality is to allocate memory for each member of the names array and then initialize it. 实现此功能的最佳方法是为names数组的每个成员分配内存,然后对其进行初始化。

There are many ways to do it. 有很多方法可以做到这一点。 I have given an example below. 我在下面给出一个例子。

char **names = malloc(MAX_ARRAY_SIZE * sizeof(char*));
if(NULL == names) {/**/}

names[0] = malloc(strlen("D")+1); //+1 for '\0' at the end.
if(NULL == names[0]) {/* Handle it*/}
strcpy(names[0], "D");

The data you're sorting is an array of char * , pointers to character strings. 您要排序的数据是char *数组,指向字符串。 To reorder the array you just need to swap the pointers. 要对数组重新排序,您只需要交换指针即可。 You don't need to move the string contents. 您不需要移动字符串内容。 In fact you can't move the string contents in this case because they are string literals . 实际上,在这种情况下,您不能移动字符串内容,因为它们是字符串文字 You get a segfault when you try to write to read-only values. 当您尝试写入只读值时,您会遇到段错误。

I've slightly rewritten your function to just swap pointers and it seems to work now. 我稍微重写了您的函数,使其仅交换指针,现在看来可以正常工作了。

void SortString(char *strings[], int size)
{
    char *temp;
    for(int i = 0; i < size - 1; i++) {
        for(int j = i + 1; j < size; j++) {
            if (strcmp(strings[i], strings[j]) > 0) {
                temp = strings[i];
                strings[i] = strings[j];
                strings[j] = temp;
            }
        }
    }
}

Live demo on Ideone.com 在Ideone.com上进行现场演示

暂无
暂无

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

相关问题 我正在尝试用 c 编写格式字符串(命令行工具) - I'm trying to write a format string in c (command line tool) 我正在尝试使用strcmp()在C中对字符串数组进行排序 - I'm trying to use strcmp() to sort a string array in C 我正在尝试用 C 编写一个排序函数,但我在声明函数方面还很陌生,无法使其工作 - I'm trying to write a sort function in C but I'm pretty new at declaring functions and couldn't make it work 我正在尝试从c中的文件读取一行并动态分配内存,但结果总是很糟糕 - I'm trying to read a line from a file in c and dynamically allocate memory but the result is always bad 我正在尝试在 VS Code 中启用错误曲线,但它不起作用并抛出错误消息 - I'm trying to enable error squiggles in VS Code and it's not working and throwing an error message 我正在尝试这段代码,它显示了一些奇怪的输出 - I'm trying this code and it shows some weird outputs 我正在尝试计算 C 中字符串中的数字 - I'm trying to count the numbers in a string in C 当我在 VScode 上运行此代码时,它显示“test.c 已停止工作”。 我试图练习合并排序 - When I run this code on VScode, it shows “test.c has stopped working”. I was trying to Practice merge sort 我正在尝试编写一个在 C 中定义为参数的函数 - I'm trying to write a function which is defined as parameter in C 我正在尝试编写一个程序来计算 C 中连词(或具有 1,2 或 3 个字母的单词)的出现次数,我的代码运行但显示 0 个单词 - I am trying to write a program that counts the occurrences of conjunctions(or words with 1,2 or 3 letters) in C, my code runs but it shows 0 words
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM