Malloc语句给出分段错误11

对于一个项目我正在学习在c中使用malloc / realloc,但我无法弄清楚为什么这段代码给我一个seg错误! #include #include #include #include typedef struct word_data word_data_t; typedef struct data data_t; typedef struct index_data index_data_t; struct word_data { int docunumber; int freq; }; struct data { char *word; int total_docs; word_data_t *data; }; struct index_data { data_t *index_data_array; }; /* Inside a function called from main */ index_data_t *index_data=NULL; index_data->index_data_array = […]

C双重不按预期工作

我正在写一些我需要将29除以10的地方。但是当我这样做并将其存储在一个double中时,它输出2.0000而不是2.9。 有人可以解释为什么会发生这种情况以及如何解决这个问题? double a = 29/10; output: 2.0000

使用循环将C值添加到链接列表

我有一个链接列表,我正在尝试为其添加值。 但是我必须错误地设置我的指针,或者内存分配有问题。 我想将令牌添加到列表中,但每次有新的循环时数据都会重叠。 例如: 第一次: repl> a a 第二次: repl> b b b 注意a是如何消失的,我想在添加新值时保留先前的值。 到目前为止,这是我的代码: struct node { int val; struct node *next; }; struct node *head = NULL; struct node *cur = NULL; struct node* create_list (int value) { struct node *ptr = (struct node*) malloc(sizeof (struct node)); if (NULL == ptr) return NULL; […]

为什么在这个prog中使用指针指针?

以下程序显示了如何在C程序中构建二叉树。 它使用动态内存分配,指针和递归。 二叉树是一种非常有用的数据结构,因为它允许在排序列表中进行有效的插入,搜索和删除。 因此,树本质上是递归定义的结构,递归编程是处理它的自然而有效的方式。 tree empty node left-branch right-branch left-branch tree right-branch tree 这是代码: #include #include struct tree_el { int val; struct tree_el * right, * left; }; typedef struct tree_el node; void insert(node ** tree, node * item) { if(!(*tree)) { *tree = item; return; } if(item->valval) insert(&(*tree)->left, item); else if(item->val>(*tree)->val) insert(&(*tree)->right, item); } […]

scanfvalidation位于并等待另一个输入。 为什么?

我正在进行这个示例练习,一切都按照我的意愿运行,但有一种我不理解的行为。 提供输入时:如果我连续输入无效条目,一切似乎都很好。 但是如果我在第一个问题的情况下输入不同于1,2,3的数字,或者在第二个问题的情况下输入1,2,则程序只是坐在那里直到给出新的输入。 如果进行了另一个无效输入,则返回错误“无效输入”消息,如果输入了适当的数字,则一切都很顺利。 我不明白为什么停止等待第二次输入…任何人? 多谢你们。 #include static int getInt(const char *prompt) { int value; printf(“%s”,prompt); while (scanf(“%d”, &value) !=1) { printf(“Your entry is invalid.\nGive it another try: %s”, prompt); getchar(); scanf(“%d”, &value); } return value; } int main() { int wood_type, table_size, table_price; printf(“Please enter ” ); wood_type = getInt(“1 for Pine, 2 for […]

允许在VB.net代码中调用C dll

我遇到了一个问题,我正在使用C Dll进入我的VB.net代码。 我有.H文件,它显示了用C语言实现的这个DLL。 此.H文件包含许多结构和联合,其中包含某些结构类型的变量。 有一个主结构,其中包含指向这些结构和联合的指针,最后将主结构的地址传递给dll函数。 我能够弄清楚如何在vb.net中使用编组创建结构和联合,以及如何传递包含其他结构和联合的地址的结构的地址。 请我解决这个问题。 任何帮助,将不胜感激。 谢谢,YOgi

C中的输入限制

我在限制用户输入方面遇到了一些问题。 我只想要整数1,2或3作为输入。 Do .. while循环完成无效整数输入的工作,但是如何忽略字符串/字符输入? 如果输入无效,我还需要重复询问用户。 更新: int problem; printf(“Please select the problem that you want to solve:\n”); printf(“\t 1-Problem 1\n”); printf(“\t 2-Problem 2\n”); printf(“\t 3-Problem 3\n”); while( scanf(“%d”, &problem)==0 && (problem!=1 || problem !=3 || problem !=2)) {int c; while((c=getchar())!=’\n’ && c!=EOF); printf(“Please select the problem that you want to solve:\n”); printf(“\t 1-Problem 1\n”); […]

将标准输出重定向到文件?

我想编写一个函数,它将标准输出重定向到文件名为“name”的文件.log其中“name”是函数的参数 void redirect(char *name) { 我不知道从哪里开始。

function分配,主要无法使用

我正在尝试分配空间并将我的函数中的文件数据放入main中的char数组指针数组中。 运行程序时出现分段错误。 谁能告诉我为什么? 数据字[0]在函数中正确打印。 这是我的function: void database_extract (char **data_words) { FILE *f_data; f_data = fopen(“database.txt”,”r”); struct stat st; stat(“database.txt”, &st); data_words = (char **)malloc(st.st_size * sizeof(char)); if (data_words == NULL) { printf(“No room\n”); exit(EXIT_FAILURE); } data_words[0] = “test”; printf(“%s”,data_words[0]); } 这是我的主要内容: int main () { char **data_words; database_extract (data_words); printf(“%s”,data_words[0]); } 任何帮助将不胜感激。

从基于来源的指数转换为基于目的地的指数

我在一些C代码中使用AVX2指令。 VPERMD指令采用两个8整数向量a和idx并通过基于idx置换a来生成第三个向量dst 。 这似乎相当于dst[i] = a[idx[i]] for i in 0..7 。 我正在调用此源代码,因为该移动是基于源索引的。 但是,我有基于目的地forms的计算索引。 这对于设置数组是很自然的,相当于dst[idx[i]] = a[i] for i in 0..7 。 如何从基于源的表单转换为基于目标的表单? 一个示例测试用例是: {2 1 0 5 3 4 6 7} source-based form. {2 1 0 4 5 3 6 7} destination-based equivalent 对于这种转换,我留在ymm寄存器中,这意味着基于目标的解决方案不起作用。 即使我要单独插入每个,因为它只在常量索引上运行,你不能只设置它们。