473,324 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Pointers and structures

vpawizard
Hello,
I am really scrwed up with the following stuff:

struct employee
{
char* employee_name;
};

struct department
{
char* department_name;
struct employee* emp; //Collection of all employees in a department
};

In main(), I wrote
struct department* company;

I want to first enter the name of department and then names of employees in that department. I tried with pointers but I must hv messed up somewhere n getting segmentation faults. Tried using array representation e.g. company[i].emp[j].employee_name . This also failed. I used new operator but it didn't help. Please tell me how do deal such things in pointers.

Thanks!!!
Nov 28 '06 #1
3 1317
horace1
1,510 Expert 1GB
Hello,
I am really scrwed up with the following stuff:

struct employee
{
char* employee_name;
};

struct department
{
char* department_name;
struct employee* emp; //Collection of all employees in a department
};

In main(), I wrote
struct department* company;

I want to first enter the name of department and then names of employees in that department. I tried with pointers but I must hv messed up somewhere n getting segmentation faults. Tried using array representation e.g. company[i].emp[j].employee_name . This also failed. I used new operator but it didn't help. Please tell me how do deal such things in pointers.

Thanks!!!
post your code?
Nov 28 '06 #2
willakawill
1,646 1GB
Hello,
I am really scrwed up with the following stuff:

struct employee
{
char* employee_name;
};

struct department
{
char* department_name;
struct employee* emp; //Collection of all employees in a department
};

In main(), I wrote
struct department* company;

I want to first enter the name of department and then names of employees in that department. I tried with pointers but I must hv messed up somewhere n getting segmentation faults. Tried using array representation e.g. company[i].emp[j].employee_name . This also failed. I used new operator but it didn't help. Please tell me how do deal such things in pointers.

Thanks!!!
Hi you have declared a pointer to a structure. This is not an array of structures.
Expand|Select|Wrap|Line Numbers
  1. struct department
  2. {
  3.    char* department_name;
  4.    employee emp[10];    //Collection of all employees in a department
  5. };
  6.  
  7. int main()
  8. {
  9.    department *company = new department[6];
  10.    strcpy(company[0].department_name, "Accounting");
  11.    strcpy(company[0].emp[0].employee_name, "John Smith");
  12. }
  13.  
Nov 28 '06 #3
I figured out that I was using the pointers without initialization. Initializing them correctly solved the problems.Thanks for quick response.
Nov 28 '06 #4

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

Similar topics

4
by: Konstantin Shemyak | last post by:
I have a big structure tree. All leaves are scalar values (no pointers). Present are arrays, structures and unions. I want to be able to store/read the content of the structure in/from a file, and...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
7
by: Frank M. | last post by:
I'm trying to declare an array of pointers to structures so that I can make the last element a NULL pointer. I figure that it would more easily allow my library routines to know when to stop...
6
by: Sam | last post by:
Hi everyone I'm confused by these declarations: struct hostent *he; server *s = (server*)malloc(sizeof(server)); /* in the code * he = gethostbyname(name);
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
7
by: copx | last post by:
How do you implement an event queue in C? The problem I had is that events needed pointers to the objects they affect and I do not know any way to check if pointers are actually valid in C. The...
12
by: addinall | last post by:
Hi guys and gals, Haven't been in here for a good decade or so! I'm having a braid-dead moment that is lasting for a couple of days. Declaring pointers to functions witrhin structures and...
10
by: arnuld | last post by:
thanks
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.