C指针问题......分段错误! [英] C pointer issues... Segmentation fault!

查看:120
本文介绍了C指针问题......分段错误!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!


我很困惑C中所有这个指针malarky并且它导致了一个令人讨厌的分段错误,它不会消失!


基本上我正在构建的程序有一个Get()函数,它从一个文件中读取1个数据记录,另一个函数将这个记录(或结构)添加到数据结构中,然后Get函数读取并返回下一条记录。


记录由姓氏,地址(带空格)和邮政编码(带空格)组成,但文件格式为:

第1行。姓氏,名字(虽然我只需要姓氏)

第2行。地址

第3行。邮编等等。


我的代码成功读取了前2条记录(因为我将其打印到屏幕上进行检查),然后在转储状态<可能已损坏的堆栈>时出现错误 ;


代码如下:

展开 | 选择 | 换行 | 行号

解决方案

展开 | 选择 | Wrap | 行号


我使用过:


char postcodebuf [MAXLINESIZE];


嗨。


只需一个事情:你的结构测试在哪里?我在你的示例代码中找不到它。


根据我学习链表的个人经验,使用指向struct testClass的指针会是更好的选择,例如: />

展开 | 选择 | Wrap | 行号


Howdy!

I''m mega confused with all this pointer malarky in C and it''s resulting in an ever annoying segmentation fault which just won''t go away!

Essentially the program I am building has a Get() function which reads 1 record of data from a file, another function adds this record (or struct) to a data structure, then the Get function reads and returns the next record.

A record consists of a surname, address (with spaces), and postcode (with spaces), however the file format is:
line 1. surname, firstname (although I only need surname)
line 2. address
line 3. postcode and so on.

My code succesfully reads the first 2 records (as I make it print it to screen to check), and then I am presented with a "error while dumping state <probably corrupted stack>"

The code is as follows:

Expand|Select|Wrap|Line Numbers

解决方案

Expand|Select|Wrap|Line Numbers


I used:

char postcodebuf[MAXLINESIZE];


Hi.

Just one thing: where is your struct test ? I couldn''t find it inside your example code.

From my personal experience in studying linked list, use a pointer to struct testClass would be a better choice, such like:

Expand|Select|Wrap|Line Numbers


这篇关于C指针问题......分段错误!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆