Tag: 数组

字符数组和指针之间的区别

我在两个代码中做同样的事情。 在代码1中:我使用了char *并在main使用malloc分配空间。 在代码2中:我使用了char数组用于相同的目的。 但为什么输出会有所不同? 代码1: struct node2 { int data; char p[10]; }a,b; main() { a.data = 1; strcpy(ap,”stack”); b = a; printf(“%d %s\n”,b.data,bp); // output 1 stack strcpy(bp,”overflow”); printf(“%d %s\n”,b.data,bp); // output 1 overflow printf(“%d %s\n”,a.data,ap); // output 1 stack } 代码2: struct node1 { int data; char *p; }a,b; main() { a.data […]

如何在C中声明运行时数组的大小?

我基本上想要相当于这个的C(嗯,只是数组的部分,我不需要类和字符串解析和所有这些): public class Example { static int[] foo; public static void main(String[] args) { int size = Integer.parseInt(args[0]); foo = new int[size]; // This part } } 原谅我的C无知。 我被java损坏了;)

C和C ++:数组元素访问指针vs int

如果您执行myarray[ i ]或将myarray[ i ]的地址存储在指针中,是否存在性能差异? 编辑:指针都在我的程序中一个不重要的步骤中计算,其中性能没有标准。 在关键部分期间,指针保持静态并且不被修改。 现在问题是这些静态指针是否比使用myarray[ i ]更快。

数组中的负索引

可能重复: C中的负数组索引? 我可以在数组中搜索负数索引吗? #include int main(void) { char a[] = “pascual”; char *p = a; p += 3; printf(“%c\n”, p[-1]); /* -1 is valid here? */ return 0; }

在OpenMP中为每个内部循环启动一个线程

我是OpenMP的新手,我正在尝试启动一个单独的线程来处理2D数组中的每个项目。 基本上,这个: for (i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { a[i][j] = b[i][j] + c[i][j]; 我正在做的是这样的: #pragma omp parallel for shared(a,b,c) private(i,j) reduction(+:diff) schedule(dynamic) for (i = 0; i < dimension; i++) { for (int j = 0; j < dimension; j++) { a[i][j] […]

减去数组

什么是实现数组减法的最快方法? 例如: array a1 = [1, 3, 4, 5, 8]; array a2 = [2, 4, 5]; array a3 = a1 – a2; /* [1, 3, 8] */ 这里的array是我的程序用来表示一个用作容器的结构的类型。 其余部分是伪代码,当然我不会创建像这样的数组也不会减去。 我能想到的最简单的解决方案涉及嵌套循环: /* a1 – a2 */ for (i = 0; i < a1.size; ++i) { int is_the_same = 0; for (j = 0; i < a2.size; […]

C中的const变量的数组大小

我发现了一个有趣的事实,我不明白它是如何运作的。 以下代码完美无缺。 #include int main(){ const int size = 10; int sampleArray[size]; typedef char String [size]; return 0; } 然后,我尝试只使用具有全局范围的常量变量,并且它仍然很好。 #include const int size = 10; int main(){ int sampleArray[size]; typedef char String [size]; return 0; } 但是,如果我将数组的范围也改为全局,我得到以下结果: 错误:在文件范围内修改了’sampleArray’ #include const int size = 10; int sampleArray[size]; typedef char String [size]; int main(){ return 0; […]

C字符串的比较运算符

我在查找比较C字符串的信息时遇到了一些困难。 我明白,与C ++不同,C不支持运算符重载,所以我想知道是否有办法检查一个字符串是否大于/小于另一个(例如str1> str2)? 提前感谢您的回复。 老实说,这是我实际上不得不问一个问题的第一次,因为我找不到相关的post。

指向结构的动态数组指针

我必须使用以下代码块进行学校作业,严格禁止任何修改。 typedef struct { char* firstName; char* lastName; int id; float mark; }* pStudentRecord; pStudentRecord* g_ppRecords; int g_numRecords =0; 这里g_ppRecords应该是一个指向结构的指针数组。 我完全没有理解的是, pStudentRecords *g_ppRecords;如何声明pStudentRecords *g_ppRecords; 意味着g_ppRecords是一个数组,因为数组应该被定义为 type arrayname[size]; 我尝试动态地为g_ppRecords分配内存,但这没有帮助。 g_ppRecords = (pStudentRecord*) malloc(sizeof(pStudentRecord*)*(g_numRecords+1));

关于C中的数组

我写了以下代码(参见问题的代码注释), #include int main() { int size; scanf(“%d”,&size); int arr[size]; /*is it a valid statement?*/ for(int i=1;i<=size;i++) { scanf("%d",&arr[i]); printf("%d",arr[i]); } return 0; }