计算矩阵乘法时的分段错误(核心转储)错误

我试图在C中实现矩阵乘法。如果我使用矩阵大小超过3,我会得到分段错误错误。对于2×2矩阵,这个代码它完美地工作。 我试图找出原因。 这是我的代码。 请看看它,让我知道我在哪里做错了。 #include #include #include /* matrix data structure. rs = row start re = row end cs = column start ce = column end a = pointer to array of pointers */ typedef struct _matrix { int rs; int re; int cs; int ce; int **a ; }matrix; matrix random_matrix(int n) { […]

增量和减量运算符

#include int main() { int x = 4, y, z; y = –x; z = x–; printf(“%d %d %d”, x, y, z); } 输出: 2 3 3 有谁能解释一下? i =+ j是什么意思(假设i = 1且j = 2 )?

计算数组的数量

如何在C程序中计算数组的总数? LLVM IR中的数组声明对应于alloca类型的操作。 所以 int a[10]; 对应于 %a = alloca [10 x i32], align 4 在LLVM IR中。 但我也注意到了 int j = 0; 也对应于alloca指令 %j = alloca i32, align 4 那么如何计算仅对应数组的alloca指令数? 编辑: for (Function::iterator i = F.begin(), e = F.end(); i != e; ++i) { for (BasicBlock::iterator ii =(*i).begin(), ii_e = (*i).end(); ii != ii_e; ++ii) […]

如何在c中从引用中为二维数组赋值?

int max(int x,int y){ if(x>=y) return x; else return y; } int calci(int i,int j,int n,int *c){ int k,y,m; k=j; y = (n)/((2^i)*(2^j)); for(y=i;y>=0;y–){ for(k=j;k>=0;k–){ *(*(c+2)+k) = max(y,calci(i+1,k,n,c)+calci(i+2,k,n,c)+calci(i,k+1,n,c)); } for(y=0;y<=j+2;y++){ *(*(c+1)+y)=*(*(c+2)+y); *(*(c+0)+y)=*(*(c+1)+y); } } return *(*(c+2)+0); } int main(){ int i,j,n,z,x,y; printf("Enter the amount\n"); scanf("%d",&n); x=log(n)/log(2); y=log(n)/log(3); int arr[3][y+3]; for(i=0;i<=2;i++) for(j=0;j<=y;j++) arr[i][j+2]=0; z = calci(x,y,n,arr); […]

使用qsort排序字符串以检查它们是否是Anagram

static int myCompare (const void * a, const void * b) { return strcmp (*(const char **) a, *(const char **) b); } void sort1(const char *str1[],int n1) { qsort (str1,n1,sizeof (const char *), myCompare); } void sort2(const char *str2[], int n2) { qsort( str2, n2, sizeof (const char *),myCompare); } int main () { […]

在x64 DLL中强制名称修改

我正在将32位应用程序移植到64位。 该应用程序支持DLL的插件。 不幸的是,每个插件需要具有的强制函数之一称为FreeLibrary ,它当然与同名的kernel32 API冲突。 我的插件API使用FreeLibrary名称的原因是该应用程序源自FreeLibrary不与任何OS API冲突的不同平台。 但是,即使在使用FreeLibrary 32位Windows上也不是问题,因为32位DLL使用名称修改,即该函数存储为_FreeLibrary ,因此不会与kernel32 API冲突。 但是,在64位上,我现在遇到了一个问题,因为64位似乎没有使用名称修改。 在64位编译器创建一个名为FreeLibrary的符号,这当然与同名的kernel32 API冲突并拒绝链接,导致以下错误: Microsoft (R) Incremental Linker Version 9.00.30729.01 Copyright (C) Microsoft Corporation. All rights reserved. kernel32.lib(KERNEL32.dll) : error LNK2005: FreeLibrary already defined in test.o Creating library test.lib and object test.exp test.dll : fatal error LNK1169: one or more multiply defined symbols found 因此,我想知道是否有任何方法强制x64 […]

计算C中CSV文件的平均值

我正在编写一个代码,我在其中读取CSV文本文件,该文件在命令行中作为参数提供。 我必须计算给定文件的实验平均值: 例如,如果文件是 Bob’s experiment,12,33,55,8 Mary’s experiment,99,21,12,0 我必须打印鲍勃的实验(数字的平均值)玛丽的实验(数字的平均值) 这是我的代码: #include #include #include #include int main (int argc, char *argv[]){ FILE* ptr=fopen(argv[1], “rt”); int i=0; double sum=0; double count=0; double ave=0; if (ptr==NULL){ perror(“Error while opening file”); exit(EXIT_FAILURE); } while(!feof(ptr)){ char s=’a’; while(s!=’,’){ s=fgetc(ptr); printf(“%c”, s); } while((char) *ptr)!=’\n’){ fscanf(ptr, “%d”, &i); sum+=i; count++; } ave=sum/count; […]

Swift 3 UnsafeMutablePointer初始化C类型float **

在Swift 3中,我需要将数据发送到接受float **输入的C对象。 在Swift 2中,我曾经声明一个UnsafeMutablePointer< UnsafeMutablePointer> ,构造一个swift数组(仅用于init!),并将其传递给指针,它工作正常: var bufferOut: UnsafeMutablePointer< UnsafeMutablePointer>? arrayOut = Array(repeating: Array(repeating: 0, count: Int(size1), count: Int(size2)) bufferOut = UnsafeMutablePointer< UnsafeMutablePointer>(arrayOut) 现在Swift 3中全部破坏了! 传递C-Style float**并初始化它的最快捷的方法是什么? 在UnsafeMutablePointer< UnsafeMutablePointer>分配值的最佳方法是什么? Documentations说对于T **应该使用AutoreleasingUnsafeMutablePointer但实际上我从未设法构建一个! 请注意,我并不真正关心上面示例中的数组! 如果我可以直接使用已知容量初始化指针,我会的。 注意 : UnsafeRawPointer的预期用例部分描述了有用的情况,例如C数组和C缓冲区,但是为上述结构翻译这些方法并不明显!

如何编译这个C程序?

当我输入gcc gprof-helper.c来编译程序时,我得到以下错误: gprof-helper.c: In function `wooinit’: gprof-helper.c:38: error: `RTLD_NEXT’ undeclared (first use in this function) gprof-helper.c:38: error: (Each undeclared identifier is reported only once gprof-helper.c:38: error: for each function it appears in.) 这是程序文件: #define _GNU_SOURCE #include #include #include #include #include static void * wrapper_routine(void *); /* Original pthread function */ static int (*pthread_create_orig)(pthread_t *__restrict, __const […]

windows上的libcurl静态库

如何将这个库libcurl静态链接到exe? 我试过了 –disable-share –enable-static没有帮助。 我正在使用MingW32 有没有一种简单的方法来静态链接这个库,所以我可以没有更多.dlls与我的应用程序?