poiner to structure problem
Hi everybody!
To make a long story short....
say I have a structure
Code:
struct employee {
int emp_index; //To assign a value to each employee for a later "pointing"
char name[30];
char address[100];
int age;
float salary;
} emp_ptr[N];
I fill an array with this structure. Assume that emp_num is fed by user input earlier
Code:
printf("Populating staff......... \n\n");
for (d = 1; d <= emp_num; d++)
emp_ptr[emp_num];
say I declare a pointer like this
Code:
void *ptr = &emp_ptr[d]; // here ptr points at the address of the array of employees
ptr = malloc(sizeof (struct employee) * d); // memory allocation for the previous line - is this right?
If at a later stage in my code I want to recall a particular employee on the array....
can I do it like this?
Code:
//THE POINTER PART HERE IS PROBABLY TOTALLY WRONG
printf("Enter empoyee you want to inspect: \n");
// this bit has to point to the chosen employee in the employee array. Make use of the index
scanf("%d", &emp_ptr[d].emp_index); // scans the index value desired
for (d = 1; d <= emp_num; d++) {//this is the ammount of employees to output on the screen.
ptr = &emp_ptr[d];//This is the pointer decared above. ptr points now to the employee of index d, that is, emp_ptr[d]
}
printf("Employee summary: %p \n", ptr);//Prints desired employee
As it is right now it is not working.... Can anyone help?
Pointer to structure problem
Quote:
Originally Posted by
MK27
You've got the right idea, but remember arrays are indexed starting from 0:
Code:
for (d = 1; d <= emp_num; d++) {
And so the last index is one less than the total number (eg, 10 elements are indexed 0-9).
Hi MK,
Having changed initial value in the for loop to d=0, now I get a memory location as an outptut for the pointer
Code:
printf("Employee summary: %p \n", ptr);//Prints desired employee
the output of this is now '004050F0' and not the desired employee, the one I choose on at
Code:
scanf("%d", &emp_ptr[d].emp_index);
What can possibly be wrong?
Thank you
pointer to structure problem
Hi Mk,
Thank you for the explanation!!
Aside of the bad name for the variabe, which I have changed as you suggested...
The reason for the int emp_index; variable within the structure is because I thought it would be helpful for pointer purposes.
Code:
struct employee {
int emp_index; //To assign a value to each employee for a later "pointing"
char name[30];
char address[100];
int age;
float salary;
} emp_struct[N];
My objective is to make an list of employees with the use of an array and to use dynamic memory allocation for it, I thought of being able to enter each employee with a different int emp_index by accepting input from the user with scanf in that way I could setup an array of employees in a dissorganize manner. For example, with int emp_index =7, int emp_index = 6, int emp_index = 2, int emp_index = 15... and so on and then being able to call them back. The reason why entering them in a disorganized way is to provide for the oportunity to test sorting in this program. Sorting by employee number that is, alghough I thought to expand the functionality to make it possible to sort according to different criteria such as name, age etc..
In addition to this I thought about the int emp_index variable because I want to be able to recall the whole employee not just a value within an employee with the help of pointers. In other words I thought that having a pointer pointing to the int emp_index would return the employee I wanted...
Lastly with the malloc what I was trying to do is to reserve memory for the array..
Do you think this approach would work?
Thnx,
Bluetxxth
pointer to structure problem
HI there MK,
I have been trying to implement what you suggested yesterday but I still have problems.
Based on this structure:
Code:
struct employee {
int emp_index; //To assign a value to each employee for a later "pointing"
char name[30];
char address[100];
int age;
float salary;
} emp_struct[N];
First I made two functions one to make take input from the user to decide the size of the staff
Code:
// Takes imput that establishes the size of the staff
void staff_size() {
printf("What is the size of your staff?: ");
scanf("%d", &emp_num);
}
Second I fill the staff like this (I think this will be the array of structures):
Code:
//Fills staff with desired size of elements - This is the array of structures (emp_struct)
void staff_create() {
printf("Populating staff......... \n\n");
for (d = 1; d <= emp_num; d++)
emp_struct[emp_num];
}
Then I thought to make another function for the array of pointers like this:
Code:
// This is an array of pointers. As an index it will have the index value of the array of structures
void fill_emp_struct(void *emp_ptrs, struct employee emp_struct) {
for (d = 0; d <= emp_num; d++)
emp_ptrs[emp_struct[d].emp_index] = &emp_struct[d];
}
This above, however, gives me an error which says;
'main.cpp:52: error: no match for 'operator[]' in 'emp_struct[d]''
I don't even think its necessary but I thought that perhaps I could make the function return an employee and call it within the main function instead of coding there... what do you think?
Below within the main function I have:
Code:
struct employee *ptr = &emp_struct[d]; // Used for an array of structures. Here ptr points at the address of emp_struct
struct employee * emp_ptrs[N]; //Declares a pointer of type employee which will be used for the array of pointers.
emp_ptrs[emp_struct[d].emp_index] = &emp_struct[d]; //Initializes the pointer above, emp_ptrs will have as index, emp_index
// emp_ptrs will be pointing to &emp_stuct[d]
I think this time I decared and initialized the pointers correctly...
Then, I think this is my second probem, I believe I did not understand how to printf, or 'extract' if you will, the contents of the pointer that points to the &emp_struct[d].emp_index which is theoretically the employee I have scanf'ed and the one I want to view.
Code:
printf("Enter empoyee you want to inspect: \n");
// this bit has to point to the chosen employee in the employee array. Make use of the index
scanf("%d", &emp_struct[d].emp_index); // scans the index value desired
for (d = 0; d <= emp_num; d++) {//this is the ammount of employees to output on the screen.
}
printf("Employee summary: %d\n", ptr -> &emp_struct[d].emp_index);//Prints desired employee << HERE IS THE PROBLEM
for the code above I get this error: main.cpp:177: error: expected unqualified-id before '&' token
What is I do wrong? Can u give me some more ideas?
problem with pointer to structure
Hi MK,
Thank you for the explanation it was very thorough.
I think my problems biggest problems are the pointers and the malloc; specially the latter.
I will give your suggestions a try and then probably post the program. I donīt mind sharing, it was just an experiment I made for learning purposes and it has no use beyond that. I sent you a private message but I donīt think it went through.
Cheers,
Bluetxxth
Ps. The Queens you live in is the same as the one I used to live in ...at the end of the N train in Ditmars Blv.