![]() |
|
#1
|
|||
|
|||
Need a help with C code-Segmentation FaultHi guys,
Just started learning C a few days back.Working on following code with structures.But when i execute i get segmentation fault.Could some one pls help with this ? Right now only choice "1"(Add Record) is being coded. CPP / C++ / C Code:
Last edited by dsmith : 16-Jul-2004 at 16:33.
Reason: Please use [c] & [/c] for syntax highlighting
|
|
#2
|
||||
|
||||
|
Hi nkhambal. Welcome to GIDForums.
If you are new to C, I would try to avoid using pointers until you are a bit more comfortable with C. For example, instead of: CPP / C++ / C Code:
Make this an array of structures like: CPP / C++ / C Code:
The reason that you are getting a segfault is that you are trying to use unallocated memory. When you use, "emp* records", you are simply defining a memory address that will eventually point to a type of structure emp*, but you are not allocating any memory for this. So unless you want to use malloc & a list, I would recommend using an array. (Especially for something this small.) In addition, when you define your structure, use arrays for now to get used to C and then go back and use pointers: CPP / C++ / C Code:
Now to add data, you can use something more like: CPP / C++ / C Code:
Also, you may want to use fgets instead of scanf. It allows you to limit your input, like so: CPP / C++ / C Code:
Good luck! __________________
The best damn Sports Blog period. |
|
#3
|
|||
|
|||
|
Thanks dsmith,
That was quick. I made the changes in code.It works now.One small problem in fuction add_record below. CPP / C++ / C Code:
when i try to run the program and try adding record program does not wait on Date of birth printf it goes to next printf for date of joining.Here is my output. *Employee records systems* 1. Add a record 2. View a record 3. Modify a record 4. Delete a record 5. Report 6. Exit Enter selecttion: 1 Add record for Employee no: 3 Employee Name: Johny English Date Of Birth(mm/dd/yy): Date of Joining(mm/dd/yy): Any suggetions ? Thanks, |
|
#4
|
||||
|
||||
|
Hi nkhambal. I am glad that things are working.
I don't use scanf alot, especially in the situations of accepting user input as in the program you have. Scanf is better suited for structured input and reading files IMO. Instead, I would recommend looking at fgets. I gave you the wrong format last time. CPP / C++ / C Code:
I would even get your floating point using a temporary string and then using atof to convert it to a floating point, ala: CPP / C++ / C Code:
See if fgets solves the problem. It is much more suitable for accepting user input. __________________
The best damn Sports Blog period. |
|
#5
|
|||
|
|||
|
Hi dsmith,
Tried with fgets but still program does not hault at Printf plus i get complier error saying "incompatible types in assignment".Following are some changes I did, CPP / C++ / C Code:
It gives complier error for assignment statement.Where am i going wrong? Thanks, |
|
#6
|
||||
|
||||
|
Quote:
You can't assign c string (char arrays or char pointers) with the assignment operator. You need to use the strcpy command to copy the string from one memory location to another. CPP / C++ / C Code:
Also, why are you using the seperate name variable? Is there a reason that you can't just use: CPP / C++ / C Code:
Is that giving you an error? Also, can you post all of your data entry routine again? I am not sure why it is not stopping to allow for input if you have removed all of the scanfs... __________________
The best damn Sports Blog period. |
|
#7
|
|||
|
|||
|
Hi dsmith,
Now the compilation error is gone.I am using records[i].variable in fgets.However the problem with printf is still there.Programs does not wait a printf for adding name.Following is my add record function CPP / C++ / C Code:
Thanks, |
|
#8
|
||||
|
||||
|
Quote:
I apologize, but I don't use scanf often because of some of its weirdness and here is another one that I just found. If I use a fgets right after a scanf, the fgets will see the enter character from the previous scanf and think that the user has pressed enter. Work-arounds:
Sorry, I mainly use fgets so I have never encountered this before. Hope this does the trick. __________________
The best damn Sports Blog period. |
|
#9
|
||||
|
||||
|
CPP / C++ / C Code:
CPP / C++ / C Code:
After you enter 1 person's data is that when you have the "printf" problem? If so, it's because you still have a scanf() in your code. That call leaves the '\n' in the input buffer. switch to: CPP / C++ / C Code:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#10
|
|||
|
|||
|
Hi dsmith,
Clearing buffer did the job...Problem is solved now.Heres what i did.. CPP / C++ / C Code:
Thanks for your help.. |
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Re: Formatting C / C++ code | WaltP | C Programming Language | 1 | 07-Jan-2008 00:59 |
| very difficult code - program | gaurav_sting | C++ Forum | 1 | 16-Jun-2004 01:59 |
| Explain code in MS STL's binary_search | rom | C++ Forum | 11 | 07-Mar-2004 21:11 |
| child pid xxx exit signal Segmentation fault (11) | bezak | Apache Web Server Forum | 1 | 24-Nov-2003 10:18 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The