所以基本上我的程序在我不得不改变之前所做的就是它接受任意值,是采用x量的单词,单词的大小也是任意的。 (两者都是用户输入的)。我通过multiArray做到了这一点。 然后按字母顺序排序。
我只是想把它放在那里,因为我的代码很糟糕,而且我对任意字符串和指针的使用非常不熟悉。 I've
read up on it in the manual
但我认为这个概念需要先下沉一点。无论如何,我得到错误:"中止陷阱: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");
}
答案 0 :(得分:3)
此
strcpy(&tname[i], &name[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
没有空间,它只是一个只有一个元素的数组。
由于您要对字符串进行排序,因此您无需复制它们。只需交换指针。还
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;
}
注意:这会将空字(完全由空格字符组成)视为有效字。