Tag: 数组

2 和6 是什么意思?

#include int main(void) { char c[]=”GATECSIT2017″; char *p=c; printf(“%s”, c+2[p]-6[p]-1); return 0; } 2[p]和6[p]是什么意思? 请提供详细说明。 产量: 17

从文件读取到数组 – C

int main() { FILE* infile1; int stockCode[15]; char stockName[100]; int stockQuantity[15]; int stockReorder[15]; int unitPrice[15]; int i; infile1 = fopen(“NUSTOCK.TXT”, “r”); while(fscanf(infile1, “%d %s %d %d %f”, &stockCode, stockName, &stockQuantity, &stockReorder, &unitPrice) != EOF) { printf(” %3d %-18s %3d %3d %6.2f \n”, stockCode, stockName, stockQuantity, stockReorder, unitPrice); } fclose(infile1); } 我要做的是从文件中获取信息并将其存储到5个单独的数组中。 但是,在打印时,它只会正确打印出名称。 1394854864 Prune-Basket 1394854688 […]

用字符串的一部分填充二维数组

我想要将我打破的字符串部分输入到二维数组中,例如:String:“one day”数组中的结果:Col1:一个Col2:day 问题是,如何用第1列的第二个变量result2和第2列的result填充数组? 这是我的代码到目前为止(你可以看到我有一个单独的历史数组和一个单独的数组用于保存用户输入的部分): #include #include #include int main (int argc, char *argv[]) { int i=0; int j=0; int k=0; char inputString[100]; char *result=NULL; char *result2=NULL; char delims[] = ” “; char historyArray[100][100] = {0}; char historyKey[] = “history”; char *tokenArray[100][100] = {0} ; //char exitString[] = “exit”; do { printf(“hshell>”); gets(inputString); strcpy (historyArray[k], inputString); […]

运行时检查失败变量周围的堆栈已损坏

#include main() { int num[9], i = 0, count = 0; while (i<10) { scanf("%d", &num[i]); if (num[i] % 2 == 0) { count++; } i++; } printf("we have %d double numbers\n", count); } 运行时检查失败#2 – 变量周围的堆栈已损坏 我该怎么办?

数组成员的值变化不合逻辑

当我声明一个数组时,我注意到了 int arr[10]; 过了一会儿,虽然在一段时间内没有任何影响,但数组成员的值会发生变化。 然后我用“新”动态分配,问题解决了。 我认为一切都应该动态声明。 但这不应该是真的。 什么是合乎逻辑的原因?

使用qsort和printf时出现分段错误

我正在尝试通过搜索md5哈希文件并使用bsearch在rockou数据库中找到它们来制作破解密码的程序。 我的问题是我遇到了由我的qsort或我的printf引起的分段错误(我运行Valgrind并且它说printf,但是操纵qsort会改变错误输出)。 我似乎无法在线找到解决方案,虽然我已经尝试刷新stdout以及在qsort函数中调整数组大小的不同方法。 char **dict = read_dict( argv[2] ); read_dict,我没有放在这里,因为它是一大堆代码,接受字典文件,将其拆分为字符串数组,将其格式化为hash:password,并为其设置mallocs空间。 然后它返回包含每个字符串的指针数组的指针。 int qcompare( const void *a, const void *b) { return strncmp( *((char **)a), *((char **)b), HASH_LEN); } qsort(dict, (sizeof(dict) / sizeof(dict[0])), sizeof(char *), qcompare); for (int i = 0; dict[i] != NULL; i++) { printf(“%s\n”, dict[i]); } 这里显示的printf不是我正在使用的那个,它只是我试图用来调试我的代码的一个更简单的。 这是我第一次发帖,所以希望我没有做任何格式化这个问题的错误。 提前感谢您提供的任何帮助。 read_dict按要求 char **read_dict(char *filename) […]

当我访问数组的超出边界元素时,为什么不会出现运行时错误?

在下面的代码中,我尝试访问数组的’-1’元素,我没有得到任何运行时错误。 #include int A[10] = {0}; int main(){ A[-1] += 12; printf(“%d”,A[-1]); return 0; } 当我运行代码时,它输出12表示它正在向不存在的A [-1]添加12。 直到今天每当我试图访问越界元素时,我都遇到了运行时错误。 我以前从未尝试过简单的代码。 任何人都可以解释为什么我的代码成功运行? 我在我的计算机上运行它,也在ideone上运行它,在它成功运行的两种情况下。

将数据存储在程序中而不是外部文件中

我有以下C代码的一部分,它使用来自文件名WMM.COF的数据,并使用存储在文件中的数据来计算地球的磁场。 该程序工作正常,但我不能让程序访问外部文件; 我想将所有数据都存储在程序中。 我尝试使用结构数组来复制数据,然后将数组放入一个字符串,但这会导致程序出错并且不会产生正确的结果。 这是我正在尝试修改的程序的代码。 static void E0000(int IENTRY, int *maxdeg, double alt, double glat, double glon, double time, double *dec, double *dip, double *ti, double *gv) { static int maxord,i,icomp,n,m,j,D1,D2,D3,D4; static double c[13][13],cd[13][13],tc[13][13],dp[13][13],snorm[169], sp[13],cp[13],fn[13],fm[13],pp[13],k[13][13],pi,dtr,a,b,re, a2,b2,c2,a4,b4,c4,epoch,gnm,hnm,dgnm,dhnm,flnmj,otime,oalt, olat,olon,dt,rlon,rlat,srlon,srlat,crlon,crlat,srlat2, crlat2,q,q1,q2,ct,st,r2,r,d,ca,sa,aor,ar,br,bt,bp,bpp, par,temp1,temp2,parp,bx,by,bz,bh; static char model[20], c_str[81], c_new[5]; static double *p = snorm; char answer; FILE *wmmdat; wmmdat = […]

如何创建一个快速和巨大的联合数组而不浪费C中的内存?

我想使用union将不同的数据类型存储在同一个内存中。 该arrays具有固定长度,应快速访问,并尽可能少浪费内存。 我将定义存储相同数据类型的区域。 所以我这样做: #include #include #include #include #define RESERVED_BYTES 1073741824 //#define RESERVED_BYTES 2147483648 typedef union { char c[RESERVED_BYTES]; //1Byte uint8_t u8[RESERVED_BYTES]; //1Byte uint16_t u16[RESERVED_BYTES / 2]; //2Byte } array_type; int main(void) { array_type *array; array = calloc(1, sizeof(array_type)); getchar(); return 0; } 这段代码工作并分配1GB内存,该数组的元素可以与array[0].u8[3]例如我知道我必须处理索引,因为它们依赖于字节大小。 遗憾的是,如果我增加内存大小(参见#define RESERVED_BYTES 2147483648 )并且在64位计算机上使用MSVS 2013,则代码无法编译我收到error C2148: total size of array must […]

C:将字符串文件读入char * s数组

我有类似的东西(没有间距问题),我正在读取一个文件,其中的字符串由#符号分隔,并尝试将每个字符串读取为char * s数组。 但是,每个包含字符串的char*都是字符串的确切长度。 虽然这是一个小马车。 char * fragments[1000]; char temp[20]; while (i < numFrags) { if (fscanf(fp, "#%15[^#]#", temp) == 1) { char entry[strlen(temp)]; entry = strcpy(entry, temp); fragments[i++] = entry; printf("%d\n", strlen(temp)); } } 思考?