Tag: 数组

值得用mod替换if语句为圆形索引

我需要一个变量来指向数组索引,并且像圆圈一样,当它到达数组的末尾时返回0。 我知道我可以使用if语句判断,但我不确定是否会更快或者不使用mod来实现相同的function,有人能给我一些建议吗? int p=0; int arr[10]; void add_index(){ if(p==9) p=0; else p++; } 要么 int p=0; int arr[10]; void add_index(){ p=(p+1)%10; }

更新struct数组值后,值保持不变

我有一个包含2^32键/值对的文件; 32410806935257552 7355088504912337885 32422108164434515 8864339902215816897 32476145725020530 3183682744179377405 32554129507725755 7487866067392975840 32556703862039701 6580190515895793022 32576110112978588 468697310917255961 32589559935917707 757063057981860288 32724197203660231 4397507527199428746 32740607750878547 497049298362389181 32804063187658851 690408619066859126 …. 我需要读取该文件,并在每次需要时获取1000 lines 。 我正在使用下面的function; void setChunk(pair* pairs, int setNumber, FILE *file){ int start = setNumber * 1000; int finish = start + 1000; int count = 0; int i = 0; char line […]

struct声明中需要typedef

我正在尝试创建一个struct元素数组,如下所示: #include #include struct termstr{ double coeff; double exp; }; int main(){ termstr* lptr = malloc(sizeof(termstr)*5); return 0; } 当我编译它时,我得到如下错误: term.c: In function ‘main’: term.c:11:1: error: unknown type name ‘termstr’ term.c:11:31: error: ‘termstr’ undeclared (first use in this function) 但是,当我将我的代码更改为以下内容时,它会照常编译: #include #include typedef struct termstr{ double coeff; double exp; }term; int main(){ term* lptr = […]

初始化结构数组内的结构数组

我在结构数组中初始化结构数组时遇到了一个问题。 例如,如果我有如下代码: #include int main() { typedef struct { int a; int b; int c; } FIRST_T; typedef struct { int x; int y; int z; FIRST_T *p; } SECOND_T; FIRST_T p1[]={{1,2,3},{3,4,5},{6,7,8}}; FIRST_T p2[]={{4,5,6},{7,8,9}}; SECOND_T my_second[]= { {1,2,3,p1}, {4,5,6,p2} }; } 如果我必须在第二个数组初始化部分本身初始化我的第一个数组,那么我将如何编写我的typedef SECOND_T? 喜欢 SECOND_T my_second[]= { {1,2,3,{{1,2,3},{4,5,6},{7,8,9}}}, {4,5,6,{{1,1,1},{2,2,2}}} }; 那我的SECOND_T怎么样? 我确信我无法将其定义为: typedef struct { […]

用C语言在字符串中查找字符的堆栈

#include #include main() { int i; int *b, *z; char name[30]; char vowel[5] = {‘A’, ‘E’, ‘I’, ‘O’, ‘U’}; char consonants[23] = {‘B’,’C’,’D’,’F’,’G’,’H’,’J’,’K’,’L’,’M’,’N’,’P’,’Q’,’R’,’S’,’T’,’V’,’W’,’X’,’Y’,’Z’}; printf (“input the string: “); scanf (“%s”, name); printf (“The string is %s\n”, name); for (i=0; name[i]!=’\0′; i++){ if (b=strchr(vowel, name[i]) != NULL) { printf (“The vowels are: %s\n”, b); } else […]

为什么’c ‘编译而’c ‘没有?

此示例编译时没有警告/错误(gcc 4.8.2 -Wall): #include int main() { char c; int i; printf(“%p %02x\n”,&i,c[&i]); printf(“%p %02x\n”,&c,c[&c]); // important to note that this line DOESN’T compile: //printf(“%p %02x\n”,&i,c[i]); // which makes sense as c is NOT an array, it is a char. return 1; } 为什么语法c [&i]编译? 是故意还是意外? 语法c [&i]在语义上是否有效? (它有什么有用的意义吗?) 我的输出示例:(指针每次都改变) 0xbfb2577c b7718cea 0xbfb2577b 08 这个问题起源于这里有一个奇怪的代码’ch2 […]

从文件中读取行并创建按字母顺序排序的数组

我正在学习C,我想做这个特定的任务。 我知道有许多类似的问题和答案,但仍然……我会尝试更具体。 可以说,我有一个包含以下行的文件: program01 programs aprogram 1program prog 5program 我现在想要一个数组: 1program 5program aprogram prog program01 programs 因此,字符串中只有拉丁文小写字母和数字,没有空格。 我知道如何执行一些单独的步骤,但想要获得并感受整个(和正确的)概念,所以说。 可能它首先从文件中读取时可以做出一些排序决定? 对于我的特定情况,手动排序是首选,只是为了更好的学习和可能的优化。 可以说,一行的最大长度为256,最大行数为256.在此先感谢。

在C编程中将元素插入动态char数组

在C编程中尝试将元素添加到动态char数组时遇到了一些问题。 这是预期的输出: How many characters do you want to input: 5 Input the string:datas The string is: datas Do you want to 1-insert or 2-remove or 3-quit?: 1 What is the character you want to insert: a Resulting string: adata 我已经在main函数中执行了那些用户输入部分,这里是main中的代码,我在其中输入字符串输入,大小并将它们传递给insert(): printf(“How many characters do you want to input: “); scanf(“%d”, &n); str = malloc(n […]

在C语言中进行类型转换后复制数组的方法更快?

我有一个二维整数数组InArray[2][60]携带2个LS字节的short数据和2个MS字节的位字段数据。 请建议一个更快的方法来提取short数据并将其复制到一个short OutArray[60] ,这是memcpy()的行。 我认为迭代每个项目不是最佳的方法。 TIA 编辑:添加代码段 int InArray[2][60]; short OutArray[60]; for (int i=0; i < 60;i++) { OutArray[i] = (short)(InArray[0][i] & 0xffff); } 有没有更好,更快的方法来做到这一点

如何连接指针数组?

可能重复: 在C中连接char数组 如何连接: char *a=”abcd”; char *b=”1234″; 使用strcat()? 当用户输入* a和* b时,回答相同的问题? EDIT错过了这个:没有使用另一个arrays。