473,497 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Segfault help

1 New Member
I'm getting a segmentation fault in one of my functions, which basically adds an employee structure to an array of employee pointers. Here's a condensed version of my C program:

Expand|Select|Wrap|Line Numbers
  1. typedef struct
  2.  {
  3.   char *firstName;
  4.   ......
  5.   ......
  6.  } Employee;
  7.  
  8. void hireEmployee(Employee *array[])
  9.  {
  10.   .....
  11.   char *first;
  12.   printf("Enter New Employee's First Name: ");
  13.   scanf("%s", &first);
  14.   strcpy(array[0]->firstName, first);
  15.   .....
  16.   .....
  17.  }
  18.  
  19. main()
  20.  {
  21.   Employee *employeeArray[5];
  22.   for(int index = 0; index < 5; index++)
  23.    {
  24.     employeeArray[index] = (Employee *)malloc(sizeof(Employee));
  25.     employeeArray[index] = NULL;
  26.    }
  27.  
  28.    hireEmployee(employeeArray);
  29.    .......
  30.  }
The segfault occurs when line 14 is executed. I obviously don't have a good understanding of what a segfault is, so any help from the experts on this board would be appreciated.
Mar 1 '08 #1
1 1184
JosAH
11,448 Recognized Expert MVP
If you have a pointer to T that pointer doesn't necessarily point to a T (you need
space for a T to point to). See the firstname; it's a pointer to a char (or more
of them consecutively). Where are those chars? You didn't allocate room for
the first name; the firstname pointer points into la-la-land, hence the segfault.

kind regards,

Jos
Mar 1 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

12
3132
by: Nathaniel Echols | last post by:
I've written a function in C to perform protein sequence alignment. This works fine in a standalone C program. I've added the necessary packaging to use it in Python; it returns three strings and...
6
1965
by: Stefan Behnel | last post by:
Hi! In Python 2.4b3, the deque is causing a segfault on two different machines I tested on. With deque, my program runs fine for a while (at least some tens of seconds up to minutes) and then...
0
1809
by: dale | last post by:
Python newbie disclaimer on I am running an app with Tkinter screen in one thread and command-line input in another thread using raw_input(). First question - is this legal, should it run...
10
2875
by: Arthur J. O'Dwyer | last post by:
I'm seeing a bug at the moment that I can't track down. It's part of a moderately large program, but here is a small program that exhibits the bug on gcc. (The program code follows at the bottom...
4
1870
by: Jim Strathmeyer | last post by:
Under what circumstances would closing a istream object (such as 'in.close()') SEGFAULT?
9
29656
by: Bin Lu | last post by:
I keep getting this malloc problem when my program tries to allocate memory for some pointer. The statement is like: rsv_cache = (rsvcache *) malloc (sizeof(rsvcache)); It goes through the...
6
2464
by: Code Raptor | last post by:
Folks, I am hitting a segfault while free()ing allocated memory - to make it short, I have a linked list, which I try to free node-by-node. While free()ing the 28th node (of total 40), I hit a...
162
6553
by: Richard Heathfield | last post by:
I found something interesting on the Web today, purely by chance. It would be funny if it weren't so sad. Or sad if it weren't so funny. I'm not sure which. ...
10
533
by: danielesalatti | last post by:
Hello!! I'm studying c++ and I'm trying to get a little piece of code working, but I'm getting a segfault with strlen here: void tabhash::set (url *U) { uint hash = U->hashCode(); char* url =...
0
7121
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6993
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7162
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6881
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.