Tag: 数组

非常基本,尝试从带有数字的文本文件中读取并将它们存储在数组中

当我运行该程序时,我得到的数字是完全荒谬的,例如-39389014,当文本文件中的数字是20.这是我的文本文件的样子: 20 20 40 30 80 40 90 20 60 10 18.0 和守则: #include #define SIZE 5 int main(void){ FILE *in = fopen(“pfile1.txt”, “r”); int x[5], y[5], i; double h; for (i=0;i<SIZE;++i){ fscanf(in, "%d %d", &x[i], &y[i]); } for (i=0;i<SIZE;++i){ printf("%4d %10d\n", x[i], y[i]); } fscanf(in, "%lf", &h); printf("%lf\n", h); fclose(in); return(0); }

我如何在函数内malloc结构数组? 代码另有工作

我正在尝试创建一个创建可变大小的2D funct数组的函数。 我正在使用以下代码,它似乎可以自行工作: typedef struct { //Starter Properties int TypeB; int TypeF; int TypeW; //Randomized Properties int RandB; int RandF; int RandW; //Derived Properties int Speed; } MapTileData; MapTileData **Map; int i, x=5, y=5; //Allocate Initial Space Map = (MapTileData**)calloc(x, sizeof(MapTileData)); for(i = 0; i < x; i++) { Map[i] = (MapTileData*)calloc(y, sizeof(MapTileData)); } 所以上面的代码创建了一个2D结构数组。 […]

在函数中交换两个数组的指针

我正在编写一个没有复制部分的mergesort到一个额外的临时数组。 出于这个原因,我创建了一个辅助arraysaux int * aux aux = (int *) calloc(n, sizeof(int)); 其中n是数组的大小。 在函数合并中,我想通过使用指针继续我的算法来在最后交换两个数组。 但是,如果我交换指针,并将它们打印到控制台,我得到了奇怪的东西: 在方法本身内,指针被交换。 但是,如果我检查我的方法,回到我的主,指针不再交换。 调用我的合并方法: merge(a, lo, mid, hi, aux); 其中a是我的主arrays,辅助和lo , mid和hi是整数。 原型: void merge(int *a, int lo, int mid, int hi, int *aux); 我试图像这样交换它们: int temp; temp = a; a = aux; aux = temp; 你能帮我解决这个问题吗? 提前致谢!

数组中的唯一数字

与我之前的问题类似,这次我试图打印不会出现两次或两次以上的数字。 这是我的代码: #include int main() { int i; int a[10]={2,2,2,4,6,6,9,10,10,11}; for(i=0; i 0 && a[i] != a[i-1]) printf(“%d “,a[i]); } if(i==9 && a[i]!=a[i-1]) printf(“%d”, a[i]); } printf(“\n”); return 0; } 输出结果是正确的,即4,9,11,但我需要分别考虑i的最终值。 有没有更好的办法?

Fortran / C混合:如何在Fortran中访问动态分配的C数组?

我目前遇到内存问题:我有一个用Fortran编码的主程序,它调用C / C ++子程序来执行某些任务并将数据存储在动态分配的数组中。 问题是我需要在返回Fortran主程序时访问这些数据。 我试图在fortran中声明一个C指针(TYPE(C_PTR))指向数组,但它似乎不起作用。 该数组存在于C子例程中,但是当我回到Fortran主程序时尝试访问它时会出现段错误。 我在这里提供我的代码,任何想法? 谢谢你的帮忙 !! Fortran语言: PROGRAM FORT_C use iso_c_binding IMPLICIT NONE interface subroutine call_fc(pX,s) bind(C,name=’call_fc_’) import integer(c_int) :: s type(c_ptr), pointer :: pX end subroutine end interface integer(c_int) :: i integer(c_int) :: s integer(c_int), pointer :: X(:) type(C_ptr), pointer :: pX s=100 call call_fc(pX,s) call c_f_pointer(pX,X,(/s/)) ! This here […]

为什么这个数组大小“解决方法”给了我一个警告?

众所周知,当在C中作为参数传递时,数组会衰减为指针。但是数组数组只会衰减到数组的指针; 因此,如果我们将原始数组本身封装在一个数组中,我们可以在将封闭数组传递给函数后检索原始数组长度。 这是一个测试这个想法的程序: /* array_workaround.c */ #include void printsize(char array_pointer[][10]); int main(void) { char a[10]; for(char i = 0; i < 10; i++) a[i] = i; char a_p[1][10] = {a}; printsize(a_p); return 0; } void printsize(char array_pointer[][10]) { printf("%lu\n", sizeof(array_pointer[0])); } 运行时,它会输出正确的值( 10 ),但编译器会发出以下警告: array_workaround.c:12:24: warning: incompatible pointer to integer conversion initializing ‘char’ with an […]

在C中传递和更改函数内部的数组

我有这个简单的C程序,它将函数中的任何数组元素更改为2。 虽然它有效但令我困惑的是,我不应该将数组地址传递给函数,而不是数组本身? 它不是那样工作的…… void function(int *val, int element){ *(val+element) = 2; } int main(int argc, char *argv[]) { int value[2]; value[0] = 10; value[1] = 5; int element = 0; function(value, element); return 0; }

如何在MPI中发送具有每个处理器变体的2Darrays

我试图在根0上采用随机生成的数组,稍微随机地改变它,并将每个变体发送到另一个处理器。 这是我到目前为止的代码: #include “stdio.h” #include “stdlib.h” #include “mpi.h” #include “math.h” int main(int argc, char **argv) { int N = 32; int dim = 3; float a = 10.0; int size, rank, i, j, k, q; float **C; float rijx, rijy, rijz, rij, Vij, E=0; float stepsize = 0.05; double Start_time, End_time, Elapse_time; MPI_Status status; MPI_Init(&argc, […]

不兼容的C型错误?

uint32 InterruptLatency; uint8 measurements[32]; char buf[256]; int kernelinterrupt time() { fscanf(fp,”%lu”, InterruptLatency); // I am reading the data from kernel which is not shown here measurements[17] = InterrupLatency; buf = &measurements; // I am getting error here as below // after storing it in buffer I am sending the data from but to another layer […]

释放二维arrays – 检测到堆腐蚀

编辑:对不起伙计们,我忘了提到这是在VS2013中编码的。 我有一个全局声明的结构: typedef struct data //Struct for storing search & sort run-time statistics. { int **a_collision; } data; data data1; 然后我分配我的记忆: data1.a_collision = (int**)malloc(sizeof(int)*2); //Declaring outer array size – value/key index. for (int i = 0; i < HASH_TABLE_SIZE; i++) data1.a_collision[i] = (int*)malloc(sizeof(int)*HASH_TABLE_SIZE); //Declaring inner array size. 然后我初始化所有元素: //Initializing 2D collision data array. for (int […]