Re: Pointers and nested structures




Computer Wizard (surely some mistake - if you ask these questions
you're no Wizard) wrote:
Hello,
I am really scrwed up with the following stuff:

struct employee
{
char* employee_name;
};

OK - so employee_name points to some storage. How did you assign the
storage?

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

Please don't use "//" comments in usenet postings - they wrap and mess
things up.
They are also not supported in the C standards most commonly used.

In your department structure, you have a pointer to an employee
structure - where do you assign the space for the employee structure?
(Alternatively you have a point to the first of an array of employee
structures - where do you assign the space for them?)

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

So company is a pointer to a department structure (is that really what
you meant?). Where do you assign the space for the department
structure. (Or again, where do you assign the space for the
array of department structures?)

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.

"hv" ? "n"? Please type in English. You are clearly capable of doing
so.

Tried using
array representation e.g. company[i].emp[j].employee_name . This also
failed.

If you haven't assigned space, then whether you express your operations
directly as pointer operations or as array operations, they will still
fail.

I used new operator but it didn't help.

There is no "new" operator in C. If you want to ask about C++, go to an
appropriate newsgroup. If you want to dynamically allocate space in C,
look at using the malloc() family of functions.

Please tell me how to deal such thing in pointers.

There's a lot to do here and much of it could be better done by reading
some books or online tutorials.

However if you really want us to help you, post a complete program
showing what you have tried and describe what happened.

Thanks!!!

.



Relevant Pages

  • Re: Pointer to an array
    ... I have an structure called employee that will put all of the employee ... Or are you trying to store 10 10-character strings to a single array? ... I then store the pointer of this structure off into another structure ... first valid eid from the original structure) and that goes on and does ...
    (comp.lang.c)
  • Re: Pointer to an array
    ... I have an structure called employee that will put all of the employee ... Just for clarification the employee array contains the correct values ... I then store the pointer of this structure off into another structure ... If you want to store a pointer then store a pointer, not a pointer converted to an integer! ...
    (comp.lang.c)
  • Re: Problem with small piece of C code
    ... 'employee' includes the forty bytes of its 'name' field, ... dynamically allocate this struct. ... a pointer - probably not what you want. ... Why copy to a temporary buffer? ...
    (comp.lang.c)
  • Re: const ref vs. pointer
    ... Employee* GetEmployeeconst; ... void SetSalary; ... Passing a pointer and passing a reference-to-const are two totally different ...
    (microsoft.public.vc.language)
  • Re: Programmer knowledge
    ... All the objects have to be of the same Employee() type, ... > What we want, therefore, is a way of making sure that our array ... A "Money" class would hold an amount that represents a financial value. ... Templates I would say are also not a "special chisel" but things to be used ...
    (comp.programming)