Tag: 数组

如何使用MPI_Gatherv从包括主节点在内的不同处理器中收集不同长度的字符串?

我试图在主节点处从所有处理器(包括主节点)收集不同长度的不同字符串到单个字符串(字符数组)。 这是MPI_Gatherv的原型: int MPI_Gatherv(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, const int *recvcounts, const int *displs, MPI_Datatype recvtype, int root, MPI_Comm comm)**. 我无法定义一些参数,如recvbuf , recvcounts和displs 。 任何人都可以在C中提供源代码示例吗?

在C中为数组分配多个值

有没有办法以浓缩forms这样做? GLfloat coordinates[8]; … coordinates[0] = 1.0f; coordinates[1] = 0.0f; coordinates[2] = 1.0f; coordinates[3] = 1.0f; coordinates[4] = 0.0f; coordinates[5] = 1.0f; coordinates[6] = 0.0f; coordinates[7] = 0.0f; return coordinates; 像coordinates = {1.0f, …}; ?

int * array 是指向32个int的数组的指针,还是指向int的32个指针的数组? 有关系吗?

如果我写 int *columns[32]; 我是否用32个指向int的指针定义了一个数组? 或者它是一个指向32个int的数组的指针? 我如何区分这两者? 有区别吗?

方括号数组和指针数组有什么区别?

作为非C / C ++专家,我总是将方括号和指针数组视为相等。 即: char *my_array_star; char my_array_square[]; 但我注意到,当在结构/类中使用时,它们的行为并不相同: typedef struct { char whatever; char *my_array_star; } my_struct_star; typedef struct { char whatever; char my_array_square[]; } my_struct_square; 下面的行显示16, whatever需要1个字节, my_array_pointer需要8个字节。 由于填充,总结构尺寸为16。 printf(“my_struct_star: %li\n”,sizeof(my_struct_star)); 下面的行显示1, whatever需要1个字节, my_array_pointer都不会被考虑在内。 printf(“my_struct_square: %li\n”,sizeof(my_struct_square)); 通过玩耍,我注意到方括号被用作结构中的额外空间 my_struct_square *i=malloc(2); i->whatever=’A’; i->my_array_square[0]=’B’; 线吹显示A: printf(“i[0]=%c\n”,((char*)i)[0]); 线吹显示B: printf(“i[1]=%c\n”,((char*)i)[1]); 所以我不能再说方括号等于指针了。 但我想了解这种行为的原因。 我害怕错过那些语言的关键概念。

是无符号字符 ; 一个 ; 未定义的行为?

来自C标准的未定义行为的示例之一(J.2): – 数组下标超出范围,即使一个对象显然可以使用给定的下标访问(如左边的表达式a [1] [7],给出声明int a [4] [5])(6.5.6) 如果声明从int a[4][5]更改为unsigned char a[4][5] ,访问a[1][7]仍会导致未定义的行为? 我的意见是,它没有,但我从其他人那里听到了不同意见,我想看看其他一些想成为SO专家的想法。 我的推理: 根据6.2.6.1第4段和第6.5段第7段的通常解释,对象a的表示是sizeof (unsigned char [4][5])*CHAR_BIT位,可以作为unsigned char [20]类型的数组访问unsigned char [20]与物体重叠。 a[1]将unsigned char [5]作为左值,但在表达式中使用(作为[]运算符的操作数,或等效地作为*(a[1]+7) +运算符的操作数) ,它衰减到unsigned char *类型的指针。 a[1]值也是指向unsigned char [20]forms的a的“表示”的字节的指针。 以这种方式解释,在a[1]添加7是有效的。

如何获得传递给函数的数组大小?

我正在尝试编写一个打印出数组中元素的函数。 但是,当我使用传递的数组时,我不知道如何迭代数组。 void print_array(int* b) { int sizeof_b = sizeof(b) / sizeof(b[0]); int i; for (i = 0; i < sizeof_b; i++) { printf("%d", b[i]); } } 迭代传递的数组的最佳方法是什么?

指向数组指针

我有一个int指针数组int* arr[MAX]; 我想将其地址存储在另一个变量中。 如何定义指针数组的指针? 即: int* arr[MAX]; int (what here?) val = &arr;

为什么我不能使用`=`复制数组?

我开始通过阅读K&R并完成一些练习来学习C语言。 经过一番挣扎,我终于能够用下面的代码完成练习1-19: /* reverse: reverse the character string s */ void reverse(char s[], int slen) { char tmp[slen]; int i, j; i = 0; j = slen – 2; /* skip ‘\0’ and \n */ tmp[i] = s[j]; while (i <= slen) { ++i; –j; tmp[i] = s[j]; } /* code from copy function p […]

如何存储然后打印2d字符/字符串数组?

假设我有:老虎,狮子,长颈鹿。 如何使用for循环和scanf将其存储在二维char数组中,然后使用for循环逐个打印单词? 就像是 for(i=0;i<W;i++) { scanf("%s",str[i][0]); //to input the string } PS很抱歉提出这样一个基本问题,但我在Google上找不到合适的答案。

可变长度数组的原型

我正在尝试编写一个在c中采用可变大小数组的函数。 void sort(int s, int e, int arr[*]){ … } 它表示对于可变长度数组,它需要在函数声明中有界。 那是什么意思? 我正在使用xcode 4.0,使用LLVM编译器2.0。 谢谢您的帮助。