使用 getline 的任意字符串
Arbitrary-strings using getline
所以基本上我的程序在我必须更改它以接受任意值之前所做的是采用 x 数量的单词,并且单词的大小也是任意的。 (两者都是用户输入的)。我通过 multiArray 做到了这一点。
然后按照字母顺序排序。
我只是想把它放在那里,因为我的代码很烂,而且我对任意字符串和指针的用法非常不熟悉。 I've
read up on it in the manual
但我相信这个概念需要先深入了解一下。无论如何,我收到错误:"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");
}
这个
strcpy(&tname[i], &name[i]);
是完全错误的,如果你只是想复制所有的字符,那么就
strcpy(tname, name);
相当于
for (size_t i = 0 ; name[i] != '[=12=]' ; ++i)
tname[i] = name[i];
使用 strcpy(&tname[i], &name[i])
是错误的,因为它会从 name
复制所有字节,直到找到 '[=18=]'
,在从第 i 个字符开始的每个循环中。
但这会再次失败,因为 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] = '[=15=]'; // Replace '\n' with '[=15=]'
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;
}
注意:这会将空词(完全由白色 space 字符组成)视为有效词。
所以基本上我的程序在我必须更改它以接受任意值之前所做的是采用 x 数量的单词,并且单词的大小也是任意的。 (两者都是用户输入的)。我通过 multiArray 做到了这一点。 然后按照字母顺序排序。
我只是想把它放在那里,因为我的代码很烂,而且我对任意字符串和指针的用法非常不熟悉。 I've
read up on it in the manual
但我相信这个概念需要先深入了解一下。无论如何,我收到错误:"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");
}
这个
strcpy(&tname[i], &name[i]);
是完全错误的,如果你只是想复制所有的字符,那么就
strcpy(tname, name);
相当于
for (size_t i = 0 ; name[i] != '[=12=]' ; ++i)
tname[i] = name[i];
使用 strcpy(&tname[i], &name[i])
是错误的,因为它会从 name
复制所有字节,直到找到 '[=18=]'
,在从第 i 个字符开始的每个循环中。
但这会再次失败,因为 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] = '[=15=]'; // Replace '\n' with '[=15=]'
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;
}
注意:这会将空词(完全由白色 space 字符组成)视为有效词。