C 使用getline的任意字符串

C 使用getline的任意字符串,c,allocation,bubble-sort,C,Allocation,Bubble Sort,所以基本上我的程序在我改变它之前所做的就是接受任意的值,取x个单词的数量,单词的大小也是任意的。(两者都是用户输入的)。我是通过一个多阵列实现的。 然后按照字母顺序排序 我将把它放在那里,因为我的代码是狗屎,我非常不熟悉任意字符串和指针的用法我已经在手册中读过了但是我相信这个概念需要先深入一点。无论如何,当我运行程序时,我得到了错误:“Abort trap:6”。有没有人能帮我解决这个问题,这样我就可以看到如果代码真的正常工作,代码会是什么样子,我想这会帮助我更好地理解指针和分配内存。如果你这样

所以基本上我的程序在我改变它之前所做的就是接受任意的值,取x个单词的数量,单词的大小也是任意的。(两者都是用户输入的)。我是通过一个多阵列实现的。 然后按照字母顺序排序

我将把它放在那里,因为我的代码是狗屎,我非常不熟悉任意字符串和指针的用法
我已经在手册中读过了
但是我相信这个概念需要先深入一点。无论如何,当我运行程序时,我得到了错误:“Abort trap:6”。有没有人能帮我解决这个问题,这样我就可以看到如果代码真的正常工作,代码会是什么样子,我想这会帮助我更好地理解指针和分配内存。如果你这样做了,你就永远欠债

当前代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LENGTH 10
int main(){ //8

  char *name;
  char tname[] = {0};
  char temp[] = {0};
  int i=0, j=0, n=0;
  ssize_t bytes_read;
  size_t bytes_number;


  printf("Enter the amount of words you want to input: ");
  scanf("%d", &n);
  printf("Enter %d words: ",n);

  bytes_number = MAX_LENGTH;
  name = (char *) malloc (bytes_number+ 1);
  bytes_number = 0;
  bytes_read = getline(&name, &bytes_number, stdin);

  if (bytes_read == -1){
    puts("ERROR!");
    free(name);
  }

  for (i = 0; i < n; i++){
      strcpy(&tname[i], &name[i]);
  }
  for (i = 0; i < n - 1 ; i++){
      for ( j = i + 1; j < n; j++){
          if (strcmp(&name[i], &name[j]) > 0){
              strcpy(temp, &name[i]);
              strcpy(&name[i], &name[j]);
              strcpy(&name[j], temp);
          }
      }
  }
  printf("\n------------------------------------------\n");
  printf("%-3s %4s %11s\n", "Input","|", "Output");
  printf("------------------------------------------\n");
  for (i = 0; i < n; i++)
  {
      printf("%s\t\t%s\n", &tname[i], &name[i]);
  }
  printf("------------------------------------------\n");
  }
#包括
#包括
#包括
#定义最大长度10
int main(){//8
字符*名称;
char-tname[]={0};
字符温度[]={0};
int i=0,j=0,n=0;
ssize_t字节_u u读取;
大小字节数;
printf(“输入要输入的字数:”);
scanf(“%d”和“&n”);
printf(“输入%d个字:”,n);
字节数=最大长度;
名称=(字符*)malloc(字节数+1);
字节数=0;
bytes\u read=getline(&name,&bytes\u number,stdin);
如果(字节数_读取==-1){
放置(“错误!”);
免费(姓名);
}
对于(i=0;i0){
strcpy(温度和名称[i]);
strcpy(&name[i]、&name[j]);
strcpy(&name[j],temp);
}
}
}
printf(“\n-------------------------------------------\n”);
printf(“%-3s%4s%11s\n”、“输入”、“输出”);
printf(“------------------------------------------------\n”);
对于(i=0;i
这个

是完全错误的,如果你只是想复制所有的字符,那么它只是

strcpy(tname, name);
这相当于

for (size_t i = 0 ; name[i] != '\0' ; ++i)
    tname[i] = name[i];
使用strcpy(&tname[i],&name[i])
是错误的,因为它将在从第i个字符开始的每个循环中复制
name
中的所有字节,直到找到
'\0'

但这将再次失败,因为
tname
is没有空间,它是一个只有一个元素的数组

由于要对字符串进行排序,因此不需要复制它们。只需交换指针。也

char temp[] = {0};
仅分配1个字符,因此

strcpy(temp, name);
将调用未定义的行为

试试这个,也许这就是你需要的

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

int
main(void)
{
    char **words;
    char *temp;
    int word_count;
    int actual_count;
    char *word;
    size_t length;
    int result;

    printf("Enter the amount of words you want to input: ");
    if (scanf("%d%*c", &word_count) != 1)
        return -1; // Input error
    printf("Enter '%d' words:\n", word_count);

    words = NULL;
    word = NULL;
    result = -1;
    actual_count = 0;
    length = 0;
    for (int i = 0 ; i < word_count ; ++i)
    {
        char **pointer;

        printf("Word(%d) > ", i + 1);

        if ((length = getline(&word, &length, stdin)) <= 0)
            goto cleanup;
        // Grow the array of words
        pointer = realloc(words, (i + 1) * sizeof(*pointer));
        if (pointer == NULL)
            goto cleanup; // Memory Exhausted
        // Now it's safe to overwrite `words'
        words = pointer;

        words[i] = malloc(length);
        if (words[i] == NULL)
            goto cleanup; // Memory Exhausted
        memcpy(words[i], word, length);
        words[i][length - 1] = '\0'; // Replace '\n' with '\0'
        actual_count += 1;
    }

    printf("Input : ");
    for (int i = 0 ; i < actual_count ; ++i)
        printf("%s\t", words[i]);
    printf("\n");

    for (int i = 0; i < actual_count - 1 ; i++)
    {
        for (int j = i + 1 ; j < actual_count ; ++j)
        {
            if (strcmp(words[i], words[j]) <= 0)
                continue;
            temp = words[i];
            words[i] = words[j];
            words[j] = temp;
        }
    }

    printf("Output: ");
    for (int i = 0 ; i < actual_count ; ++i)
        printf("%s\t", words[i]);
    printf("\n");

    result = 0;
cleanup:
    free(word);
    for (int i = 0; i < actual_count ; i++)
        free(words[i]);
    free(words);

    return result;
}
#包括
#包括
#包括
int
主(空)
{
字符**字;
字符*温度;
整数字数;
实际计数;
字符*字;
尺寸与长度;
int结果;
printf(“输入要输入的字数:”);
if(scanf(“%d%*c”,&word\u count)!=1)
return-1;//输入错误
printf(“输入“%d”个字:\n”,字数);
单词=空;
word=NULL;
结果=-1;
实际计数=0;
长度=0;
for(int i=0;i”,i+1);
如果((length=getline(&word,&length,stdin))

是完全错误的,如果你只是想复制所有的字符,那么它只是

strcpy(tname, name);
这相当于

for (size_t i = 0 ; name[i] != '\0' ; ++i)
    tname[i] = name[i];
使用strcpy(&tname[i],&name[i])
是错误的,因为它将在从第i个字符开始的每个循环中复制
name
中的所有字节,直到找到
'\0'

但这将再次失败,因为
tname
is没有空间,它是一个只有一个元素的数组

因为您想对字符串进行排序,所以不需要复制它们。只需交换指针即可。另外

char temp[] = {0};
仅分配1个字符,因此

strcpy(temp, name);
将调用未定义的行为

试试这个,也许这就是你需要的

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

int
main(void)
{
    char **words;
    char *temp;
    int word_count;
    int actual_count;
    char *word;
    size_t length;
    int result;

    printf("Enter the amount of words you want to input: ");
    if (scanf("%d%*c", &word_count) != 1)
        return -1; // Input error
    printf("Enter '%d' words:\n", word_count);

    words = NULL;
    word = NULL;
    result = -1;
    actual_count = 0;
    length = 0;
    for (int i = 0 ; i < word_count ; ++i)
    {
        char **pointer;

        printf("Word(%d) > ", i + 1);

        if ((length = getline(&word, &length, stdin)) <= 0)
            goto cleanup;
        // Grow the array of words
        pointer = realloc(words, (i + 1) * sizeof(*pointer));
        if (pointer == NULL)
            goto cleanup; // Memory Exhausted
        // Now it's safe to overwrite `words'
        words = pointer;

        words[i] = malloc(length);
        if (words[i] == NULL)
            goto cleanup; // Memory Exhausted
        memcpy(words[i], word, length);
        words[i][length - 1] = '\0'; // Replace '\n' with '\0'
        actual_count += 1;
    }

    printf("Input : ");
    for (int i = 0 ; i < actual_count ; ++i)
        printf("%s\t", words[i]);
    printf("\n");

    for (int i = 0; i < actual_count - 1 ; i++)
    {
        for (int j = i + 1 ; j < actual_count ; ++j)
        {
            if (strcmp(words[i], words[j]) <= 0)
                continue;
            temp = words[i];
            words[i] = words[j];
            words[j] = temp;
        }
    }

    printf("Output: ");
    for (int i = 0 ; i < actual_count ; ++i)
        printf("%s\t", words[i]);
    printf("\n");

    result = 0;
cleanup:
    free(word);
    for (int i = 0; i < actual_count ; i++)
        free(words[i]);
    free(words);

    return result;
}
#包括
#包括
#包括
int
主(空)
{
字符**字;
字符*温度;
整数字数;
实际计数;
字符*字;
尺寸与长度;
int结果;
printf(“输入要输入的字数:”);
if(scanf(“%d%*c”,&word\u count)!=1)
return-1;//输入错误
printf(“输入“%d”个字:\n”,字数);
单词=空;
word=NULL;
结果=-1;
实际计数=0;
长度=0;
for(int i=0;i”,i+1);

如果((length=getline(&word,&length,stdin))只是
put(“ERROR!”);
并继续是不正确的错误处理。为什么要强制执行
malloc()
,编译器会抱怨吗?还有,
char-tname[]={0}
看起来不像你想要的。程序编译得很好,但在我经过第一个输入阶段后,程序崩溃了。你有什么解决办法吗?注意:没有
,编译得很好-Wall
可能不是很好。@Joel你必须重新设计你的程序。你需要存储
n
单词。但是你需要sider:1.一个单词的长度不能超过10个字符,这是您定义的限制。-2.您尝试将每个单词复制到同一个变量中(请参阅iharob的答案);这不是您想要的。-3.您还需要学习C中的数组,而不仅仅是指针。只需
put(“ERROR!”);
和continue不是正确的错误处理。为什么要强制转换
malloc()
,编译器会抱怨吗?还有,
char tname[]={0}
看起来不像你想要的。程序编译得很好,但在我经过第一个输入阶段后,程序崩溃了。你有什么解决办法吗?注意:没有
,编译得很好-Wall
可能不是很好。@Joel你必须重新设计你的程序。你需要存储
n
单词。但是你需要一个单词不能超过10个字符