gpt4 book ai didi

C scanf 字符串数组

转载 作者:行者123 更新时间:2023-11-30 20:11:57 26 4
gpt4 key购买 nike

我正在尝试编写一个小而短的程序来读取文本文件并将其内容放入数组中进行练习。我想编写这个程序以将任何文本文件作为字符串读取并将其存储到数组中。这样,您可以读取任何文件,并且无论字符串长度如何,它都会动态构建一个数组并用文件填充它。我将此作为练习 C 的练习,并希望将其推广到其他类型和结构。

但是,由于某种原因,我的第一个条目不匹配,导致意外行为,但至少它不会遇到任何段错误。我知道使用 C 语言,您基本上需要微观管理所有内存,并且使用代码,我尝试为每个条目分配内存,但这是正确的方法吗?我在脑子里运行了代码,从 0 个条目开始在逻辑上是有意义的,但我不明白为什么第一个条目失败而其余条目工作。

代码:

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

int main(int argc, char *argv[]){

//Initialize variables and pointers
//Create an array of chars to use when reading in
//Create an array of strings to store
//i : use to keep track of the number of strings in array
//j : loop variable
//size: size of string
char *s = (char *) malloc(sizeof(char));
int i=0,j=0;
int size = 0;
char **a = (char **) malloc(sizeof(char *));

//Read in string, assign string to an address at a[]
while( scanf("%79s",s) == 1){

//Get the size of the input string
size = (unsigned) strlen(s);

//Print some notes here
printf("\nString is \"%-14s\"\tSize is %-3d, i is currently %d\n",s,size,i);
printf("Using MALLOC with %d bytes\n",size+1);

//Allocate memory to copy string
//
//For some reason, the commented code works
//a[i] = (char *) (malloc(sizeof(char)*(size+1)) + 'a');
a[i] = (char *) (malloc(sizeof(char)*(size+1)) );

//Go and allocate memory for each character in string to store
for(j=0; j<(size+1); j++) a[i][j] = (char) (malloc(sizeof(char)));

//Print some more notes here
printf("Size: a[%2d] is %3d bytes, *a[%2d] is %3d bytes, Length of a[%2d] is %d\n",i,(int) sizeof(a[i]),i,(int) sizeof(*a[i]),i,(unsigned) strlen(a[i]));

//Copy over string and set last char to be end
for(j=0; j<size; j++) a[i][j] = (char) s[j];
a[i][size] = '\0';

//Print it out and increase i
printf("a[%3d] is now %s\n",i,a[i]);

i++;
}
printf("I is now %d\n\n\n",i);
a[i] = NULL;

//print out array
for(j=0; j<i; j++) printf("%3d. %-40s\n",j,a[j]);


return 0;
}

测试文本文件(numbers.txt):

1
22
333
4444
55555
666666
7777777
88888888
9999999
0000000000
11111111111
222222222

命令:

./a.out

结果:

String is "1             "      Size is 1  , i is currently 0
Using MALLOC with 2 bytes
Size: a[ 0] is 8 bytes, *a[ 0] is 1 bytes, Length of a[ 0] is 2
a[ 0] is now 1

String is "22 " Size is 2 , i is currently 1
Using MALLOC with 3 bytes
Size: a[ 1] is 8 bytes, *a[ 1] is 1 bytes, Length of a[ 1] is 3
a[ 1] is now 22

String is "333 " Size is 3 , i is currently 2
Using MALLOC with 4 bytes
Size: a[ 2] is 8 bytes, *a[ 2] is 1 bytes, Length of a[ 2] is 4
a[ 2] is now 333

String is "4444 " Size is 4 , i is currently 3
Using MALLOC with 5 bytes
Size: a[ 3] is 8 bytes, *a[ 3] is 1 bytes, Length of a[ 3] is 5
a[ 3] is now 4444

String is "55555 " Size is 5 , i is currently 4
Using MALLOC with 6 bytes
Size: a[ 4] is 8 bytes, *a[ 4] is 1 bytes, Length of a[ 4] is 6
a[ 4] is now 55555

String is "666666 " Size is 6 , i is currently 5
Using MALLOC with 7 bytes
Size: a[ 5] is 8 bytes, *a[ 5] is 1 bytes, Length of a[ 5] is 7
a[ 5] is now 666666

String is "7777777 " Size is 7 , i is currently 6
Using MALLOC with 8 bytes
Size: a[ 6] is 8 bytes, *a[ 6] is 1 bytes, Length of a[ 6] is 8
a[ 6] is now 7777777

String is "88888888 " Size is 8 , i is currently 7
Using MALLOC with 9 bytes
Size: a[ 7] is 8 bytes, *a[ 7] is 1 bytes, Length of a[ 7] is 9
a[ 7] is now 88888888

String is "9999999 " Size is 7 , i is currently 8
Using MALLOC with 8 bytes
Size: a[ 8] is 8 bytes, *a[ 8] is 1 bytes, Length of a[ 8] is 8
a[ 8] is now 9999999

String is "0000000000 " Size is 10 , i is currently 9
Using MALLOC with 11 bytes
Size: a[ 9] is 8 bytes, *a[ 9] is 1 bytes, Length of a[ 9] is 11
a[ 9] is now 0000000000

String is "11111111111 " Size is 11 , i is currently 10
Using MALLOC with 12 bytes
Size: a[10] is 8 bytes, *a[10] is 1 bytes, Length of a[10] is 12
a[ 10] is now 11111111111

String is "222222222 " Size is 9 , i is currently 11
Using MALLOC with 10 bytes
Size: a[11] is 8 bytes, *a[11] is 1 bytes, Length of a[11] is 10
a[ 11] is now 222222222
I is now 12


0. ▒"▒
1. 22
2. 333
3. 4444
4. 55555
5. 666666
6. 7777777
7. 88888888
8. 9999999
9. 0000000000
10. 11111111111
11. 222222222

最佳答案

对于初学者:您的声明 char **a; 声明了一个指针;类型正确,但没有指向任何地方。

您当前的代码正在写入内存的某个未定义区域,有时有效,有时会转储;但这样做的效果是不确定的,而且大多是不可预测的。

在C中,你需要处理你自己的内存。因此,要么声明您希望 this 指针指向右侧的内存: char a[255][10]; (每个字符串中有 255 个字符,其中 10 个字符),或者您在堆上分配它(使用 malloc)并将其分配给指针:a = (char * *) malloc(sizeof(char *) * 10); (再次获取 10),然后对于每个:a[i] = (char *) malloc(sizeof(char) * 255);

如果需要,后者允许您使用大小变量,并且还允许比第一种方式更大的内存块。

理解这一点后,请将其应用到您想要使用的其他变量。请记住,您声明的每个指针只是一个指针。您的工作是让它指向有用的东西,并在该位置提供内存。

关于C scanf 字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38275759/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com