Tag: 数组

数组K&R 1.6字符?

在第23页的K&R第二版中,有一些代码 if (c >= ‘0’ && c <= '9') ++ndigit[c-'0']; 它继续。 我的问题是,为什么整数都有’在每一边? 什么是c-‘0’?

指针和数组 – 艰难地学习C语言

这个问题来自Zed Shaw的Learn C the Hard Way。 它是关于指针和数组的。 我们在这里给出了一些代码: #include int main(int argc, char *argv[]) { // create two arrays we care about int ages[] = {23, 43, 12, 89, 2}; char *names[] = { “Alan”, “Frank”, “Mary”, “John”, “Lisa” }; // safely get the size of ages int count = sizeof(ages) / sizeof(int); int i […]

如何在C中向二维数组添加字符串

我开始学习C并且我遇到了将字符串输入添加到2D数组的问题,我能够正确地获取字符串输入但是当我尝试将字符串元素添加到数组时它不能按预期工作当打印数组时(这是我测试程序的方式),它会为数组中的每个索引分配一个字符而不是整个字符串。 这是我的观看代码,非常感谢您提前感谢任何发布的帮助。 #include main() { char c_array[3][3]; int x; int y; int z=10; int i_user_input; char c_name[256]; for(x=0;x<=+2;x++) { for(y=0;y<=2;y++) { printf("\nPlease enter a name to the system:"); scanf(" %s",&c_array[x][y]); printf("The string is %s\n",c_name); printf("Please press '1' if you would like to keep entering names\n"); printf("Please press '2' if you would like to print the […]

将单个字符串与C中的字符串数组进行比较

我的程序正在接受用户输入,然后输入输入的第一个单词并将其与接受的命令数组进行比较。 将输入的第一个单词(在被标记化之后)与字符串数组进行比较的最佳方法是什么? 例: 将字符串”pwd”与包含{“wait”, “pwd”, “cd”, “exit”}的数组进行比较 在此先感谢您的帮助!

使用rand的函数初始化矩阵总是以相同的方式

我有一个函数,应该以一个随机的方式初始化矩阵的值,给定一个概率(在这种情况下p(0)= 1/3,p(1)= 2/3)问题是矩阵总是出来是一样的: void init_matrix(short unsigned* m,int* n_gen) { *n_gen=0; int i; for(i=0;i<N_X*N_Y;i++) { if( ( ( rand() % 100 ) % 3) == 0 ) m[i]=0; else m[i]=1; } } 是因为rand()函数的实现? 还是我错误地使用它? 谢谢!

value既不是数组也不是指针

我有这个代码: int ID [STUDENTS]; int Scores [STUDENTS][GRADES]; int Hi [GRADES]; int Lo [GRADES]; double Avg [GRADES]; int numStudents; /*getData function called*/ int getData(ID, Scores, numStudents) { int student_count; int quiz_count; FILE* spIn; spIn = fopen(“myfile.dat”, “r”); student_count=0; while (fscanf (spIn, “%d”, ID[student_count]) != EOF) { for (quiz_count = 0; quiz_count < GRADES; quiz_count++) { fscanf […]

用于频繁随机访问的数组或链表?

我经常使用列表和数组,我想知道什么是更快,数组或列表? 假设我们有整数数组和链表,两者都有相同的值。 int array_data[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; typedef struct node_data{ int data; struct node_data * next; } list_data; ____ ____ ____ ____ ____ ____ ____ ____ ____ ____ list_data *d = -> | 1| -> | 2| -> | 3| -> | 4| -> | 5| -> […]

C MPIarrays搜索(MPI_Scatter分段故障)

我的MPI代码一直在运行所有非主任务的分段错误。 #include “mpi.h” #include #include int main(int argc, char* argv[]) { int list_size = 1000 int threads; int th_nums; int slice; int index; char* the_array[list_size]; char* temp_array[list_size]; char str_to_search[10]; FILE *in = fopen(“inputfile”, “r”); char parse[10]; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &threads); MPI_Comm_size(MPI_COMM_WORLD, &th_nums); if (threads == 0) { // if master task fgets(parse, 10, in); slice […]

C:数组结构中的结构数组

我有这些结构: struct menu_item{ int id; char *text; }; struct menu_tab{ char *label; unsigned char item_count; struct menu_item *items; }; struct menu_page{ char *label; unsigned char tab_count; struct menu_tab *tabs; }; struct Tmenu{ unsigned char page_count; struct menu_page *pages; }; 我想定义整个菜单系统: struct Tmenu menu_test = { 2, { “F1”, 2, { { “File”, 8, { {1, […]

在C中为char数组赋一个char指针

我开始研究C,我已经遇到了一些问题。 我想解析一个文件并将每行的结果存储在一个结构中。 我的结构看起来像: struct record { char x[100]; } 然后,每当我使用strtok解析某些file.txt中的一行时, struct record R; … char *token; token = strtok(line, “\t”); token返回一个指向字符串的指针,每当我打印它时,它都是正确的字符串。 我想将标记分配给x,例如Rx = token ,但是我收到错误,“char x [100]不可分配”。 是否可以将此指针标记转换为实际的char数组,或者将结果存储到结构中的最佳方法是什么?