【发布时间】:2010-09-15 03:12:51
【问题描述】:
编辑:算了。它是代码的另一部分(实际上问题是次要元素。另一个指针但没有用 malloc 分配,只是声明了,所以我认为内存是在另一个地方分配的)。该示例现在可以编译。
谢谢大家。我很抱歉我的错别字,但英语不是我的母语(不是一个好的escuse,但我会更加努力)。
嗨。我想将一些元素(在结构中)传递给函数,但我无法以任何方式读取元素(段错误)
#define _FILE_OFFSET_BITS 64
#define _LARGEFILE_SOURCE
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include "pcre.h"
#include <arpa/inet.h>
#define BUFFER 1512
typedef struct OCR {
unsigned long int ocr;
struct OCR * prev;
struct OCR * next;
} ip_ocr;
int sending (ip_ocr * tmp) {
printf("%p\n",tmp); //this outpuut
printf("%lu",tmp->ocr); // at this point i get a seg fault
return 0;
}
int main () {
ip_ocr * list;
list=malloc(sizeof(ip_ocr));
list->ocr=1;
list->next=NULL;
list->prev=NULL;
sending(list);
}
【问题讨论】:
-
这不可能是你的代码:它缺少几个分号和
#include行。另外,由于某种原因,尝试添加类似printf("tmp: %p\n", tmp); at the start ofsending: I think you might be getting aNULL`指针的东西。当我编译您的代码时(在修复包含和语法错误之后),我没有收到段错误。 -
刚刚他们关闭了虚拟服务器。但我检查了指针并没有为空。我会重新检查我的代码(我只是在这里发布了它的关键部分)我会在他们再次打开机器时再次检查。
-
不要解释代码;它使其他人很难确定实际代码中存在哪些错误以及通过重新输入代码引入了哪些错误。但是,如果您必须解释,您应该发布可以重现问题的最小示例。
-
谢谢,但实际代码有几行。再次感谢:D
标签: c pointers data-structures