同时使用int和string填充数组 #包括 #包括 #包括 #包括 int main() { srand(时间(空)); 字符十六进制[]=“0123456789ABCDEF”; char seferNo[]=“”; char numaraDeposu[100][6]; int j=0; 对于(j=0;j
,如注释中所述,同时使用int和string填充数组 #包括 #包括 #包括 #包括 int main() { srand(时间(空)); 字符十六进制[]=“0123456789ABCDEF”; char seferNo[]=“”; char numaraDeposu[100][6]; int j=0; 对于(j=0;j,c,C,,如注释中所述,char seferNo[]=”;是一个大小为1的数组,它只能容纳空字符串('\0'),并且仍然被视为字符串。对strncpy()的调用调用了未定义的行为,因为没有足够的空间来写入'\0'字符 您可以使用2D数组执行您正在尝试的操作,并且不需要包含string.h。您可以使用简单的字符操作和stdio.hsprintf(),例如 #include <stdio.h> #include <stdlib.h> #include <string.h>
char seferNo[]=”;
是一个大小为1的数组,它只能容纳空字符串('\0'
),并且仍然被视为字符串。对strncpy()
的调用调用了未定义的行为,因为没有足够的空间来写入'\0'
字符
您可以使用2D数组执行您正在尝试的操作,并且不需要包含string.h
。您可以使用简单的字符操作和stdio.h
sprintf()
,例如
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main()
{
srand(time(NULL));
char hex[]="0123456789ABCDEF";
char seferNo[]="";
char numaraDeposu[100][6];
int j=0;
for (j=0;j<10;j++)
{
char seferNo[]="";
for (int i=0;i<4;i++)
{
int a;
a=(rand() % 15);
strncat(seferNo,&hex[a],1);
}
strcpy(numaraDeposu[j],seferNo);
printf("%s\n",numaraDeposu[j]);
}
}
$ ./bin/numardeposu_struct
A c190
7 c329
D c955
7 c761
6 c968
D c781
8 c985
9 c449
7 c708
F c893
E c799
E c168
需要包含不同类型的对象时,请使用结构 当您需要将不同类型作为单个对象进行协调时,您应该考虑
struct
。在这里,由于您将2D数组的每个字符串元素中的第一个字符限制为单个char
,因此并不完全必要。但是,它将为您提供更多的灵活性,让您能够访问第一个character和剩余的代码
您可以使用简单的结构,如:
$ ./bin/numardeposu2D
Fc598
7c569
Ec247
1c451
Fc172
0c558
Bc501
0c462
Ec318
Ac396
2c742
0c304
4c146
...
选择无符号字符
只是为了使类型不同,但您可以对这两种类型使用字符
。然后,您将声明一个结构数组,并为数组中的每个结构填充每个成员,例如
typedef struct {
unsigned char hexnum;
char code[7];
} deposu_type;
虽然使用struct
和struct
数组将是更简单的解决方案(如果您的示例变得更复杂),但这两种解决方案大致相当
仔细检查一下,如果你还有其他问题,请告诉我
根据需要为尽可能多的numaraDeposu动态分配存储 为了回应您关于如何为无限多的对象分配存储的评论(我认为这意味着一个未知的数字,因为您没有无限的可用内存),您将使用
malloc()
、calloc()
或realloc()
动态分配存储以容纳所有numaraDeposu
动态分配存储并不困难。首先,为一些合理的数字结构分配存储,并将内存块的地址分配给指针变量。例如,在这里,让我们为2
structdeposu type
分配存储,并将结果分配给指针努马拉德波苏
,例如
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main()
{
srand(time(NULL));
char hex[]="0123456789ABCDEF";
char seferNo[]="";
char numaraDeposu[100][6];
int j=0;
for (j=0;j<10;j++)
{
char seferNo[]="";
for (int i=0;i<4;i++)
{
int a;
a=(rand() % 15);
strncat(seferNo,&hex[a],1);
}
strcpy(numaraDeposu[j],seferNo);
printf("%s\n",numaraDeposu[j]);
}
}
$ ./bin/numardeposu_struct
A c190
7 c329
D c955
7 c761
6 c968
D c781
8 c985
9 c449
7 c708
F c893
E c799
E c168
(注意:您必须始终检查返回以验证分配成功或失败,并处理任何失败)
您总是为分配保留两个计数器变量。可用
计数器保存当前可放入分配内存块的numaraDeposu
,并且used
跟踪您已填充(使用)的数字。当(used==available)
您可以重新分配更大的内存块,并更新可用的数量。一种常见的增长模式是,每次需要更多的内存时,将可用的数量增加一倍,但每次您可以随意添加任意数量的内存
为numaraDeposu
分配存储的简单示例可以编写如下。通过这种方法,您可以根据需要分配任意多的numaraDeposu
,直到耗尽物理内存:
size_t available = 2, /* no. of struct available */
used = 0; /* no. of struct used */
/* allocate memory for initial (2) struct available */
deposu_type *numaraDeposu = malloc (available * sizeof *numaraDeposu);
if (numaraDeposu == NULL) { /* validate EVERY allocation */
perror ("malloc-numaraDeposu");
return 1;
}
始终确认已释放所有已分配的内存,并且没有内存错误
如果您有进一步的问题,请告诉我。声明一个包含int
和char[]
的struct
,然后声明它们的数组。char seferNo[]=”;
是一个大小为1
的数组,当strncat(seferNo,&hex[a],1)时会导致未定义的行为;
被调用。请参见..,并且有两个不同的char seferNo[]=”;
变量。循环中的一个变量与循环中看不到的较早的变量相阴影。删除内部变量,并将较早的变量更改为,例如,char seferNo[1000]=“”
请花一点时间澄清您想要什么,编辑您的问题。您是想在代码中同时存储一个数字,还是只想在随机代码中输出数字?此外,正如@DavidC.Rankin所指出的,您完全处于UB状态。请为该数组提供一个大小。“这很好”。我认为这样做不好:char seferNo[]=”;
这是一堆char
,只有一个char
的空间,初始化为包含\0
char。谢谢你,这很有帮助。我可以问一些问题。我想声明无限数量的(结构)我的代码的总线和每个总线都有一个hex num queue num ect。但是我不知道如何声明无限多的结构(用户可以添加总线)希望你能理解我的蹩脚英语。你声明任何对象的方法是声明一个该类型的指针,然后使用malloc()
,calloc()
或realloc()为当时需要的数量分配存储空间
然后,当需要增加或减少存储量时,可以使用realloc()
调整内存块的大小。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
unsigned char hexnum;
char code[7];
} deposu_type;
int main (void) {
const char hex[]="0123456789ABCDEF";
size_t available = 2, /* no. of struct available */
used = 0; /* no. of struct used */
srand(time(NULL));
/* allocate memory for initial (2) struct available */
deposu_type *numaraDeposu = malloc (available * sizeof *numaraDeposu);
if (numaraDeposu == NULL) { /* validate EVERY allocation */
perror ("malloc-numaraDeposu");
return 1;
}
for (; used < 100; used++) {
if (used == available) { /* reallocate more when (used == available) */
/* always reallocate using temporary pointer */
void *tmp = realloc (numaraDeposu, 2 * available * sizeof *numaraDeposu);
if (!tmp) { /* valdate EVERY reallocation */
perror ("realloc-numaraDeposu");
break;
}
numaraDeposu = tmp; /* assign reallocated block to pointer */
available *= 2; /* updaate no. of struct available */
}
numaraDeposu[used].hexnum = hex[rand() % 16];
sprintf (numaraDeposu[used].code, "c%03d", rand()%1000);
printf ("%c %s\n", numaraDeposu[used].hexnum, numaraDeposu[used].code);
}
free (numaraDeposu); /* free all allocated memory */
}
$ valgrind ./bin/numardeposu_struct_dyn
==13231== Memcheck, a memory error detector
==13231== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==13231== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==13231== Command: ./bin/numardeposu_struct_dyn
==13231==
C c582
A c524
4 c742
1 c697
5 c890
...
6 c509
A c079
2 c511
0 c770
==13231==
==13231== HEAP SUMMARY:
==13231== in use at exit: 0 bytes in 0 blocks
==13231== total heap usage: 8 allocs, 8 frees, 3,056 bytes allocated
==13231==
==13231== All heap blocks were freed -- no leaks are possible
==13231==
==13231== For counts of detected and suppressed errors, rerun with: -v
==13231== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)