Hello,
I have a question about pointers.
Could anyone please explain to me the following:
I have a dynamically allocated array of structures like: - struct Employee{......};
-
-
Employee **pEnteredEmployee;
-
-
int main()
-
{
-
....
-
pEnteredEmployee = new Employee *[EntryNumber];
-
for (int i=0; i < EntryNumber; i++)
-
pEnteredEmployee[i] = new Employee;
-
....
-
-
when I call a function say -
-
InputDataFromFile(EntryNumber, pEnteredEmployee);
-
-
if I understand it correctly, the pEnteredEmployee is passing to function the value it holds i.e. the address of the first pointer in the array,
but if the function is for example: - void InputDataFromFile(int EntryNumber, Employee *EnteredEmployee[])
-
{
-
for (int i=0; i < EntryNumber; i++)
-
{ InputFile >> EnteredEmployee[i]->Name;
-
InputFile >> EnteredEmployee[i]->Surname;
-
InputFile >> EnteredEmployee[i]->Grad_Year;
-
InputFile >> EnteredEmployee[i]->Average_Mark;
-
InputFile >> EnteredEmployee[i]->Salary;
-
InputFile >> EnteredEmployee[i]->University;
-
}
-
}
through Employee *EnteredEmployee[] does it receive the address of the pointer, its value or it converts it to the value this pointer points to due to the dereferencing symbol * i.e. the address of the first structure?
Why I cannot write in the function body like this: - InputFile >> *EnteredEmployee[i].Name;
I am just trying to understand how exactly it is working.
Thanks a lot!
5 2204
Why I cannot write in the function body like this: - InputFile >> *EnteredEmployee[i].Name;
I am just trying to understand how exactly it is working.
Thanks a lot!
The reason you cannot do this is because the '.' operator takes precedence over the '*' operator. It is trying to find the Name attribute of a pointer in the above example. You can rewrite it as (*EnteredEmployee[i]).Name, and it will work correctly - however, note that this is the reason the '->' operator was introduced. (*Name).attribute and Name->attribute mean the exact same thing - use whichever one you prefer.
Thank you very much, now I got it!
And what about the first part of the question? Could someone explain that to me or maybe give a good link with theory on that?
Thanks.
And one more thing,
I've tried to do the same program using only one pointer to an array of structures, and it works correctly.
I changed the previous code to: - Employee *pEnteredEmployee;
-
-
void InputDataFromFile(int EntryNumber, Employee EnteredEmployee[])
-
{
-
for (int i=0; i < EntryNumber; i++)
-
{ InputFile >> EnteredEmployee[i].Name;
-
InputFile >> EnteredEmployee[i].Surname;
-
InputFile >> EnteredEmployee[i].Grad_Year;
-
InputFile >> EnteredEmployee[i].Average_Mark;
-
InputFile >> EnteredEmployee[i].Salary;
-
InputFile >> EnteredEmployee[i].University;
-
}
-
};
-
-
int main()
-
{
-
....
-
pEnteredEmployee = new Employee [EntryNumber];
-
-
.....
-
}
What is the point then of the previous one, if we can use one pointer instead of their array and still allocate memory dinamically?
Or I don't understand anything?
The previous method was suggested in the task for the program.
if I understand it correctly, the pEnteredEmployee is passing to function the value it holds i.e. the address of the first pointer in the array,
but if the function is for example: - void InputDataFromFile(int EntryNumber, Employee *EnteredEmployee[])
-
{
-
for (int i=0; i < EntryNumber; i++)
-
{ InputFile >> EnteredEmployee[i]->Name;
-
InputFile >> EnteredEmployee[i]->Surname;
-
InputFile >> EnteredEmployee[i]->Grad_Year;
-
InputFile >> EnteredEmployee[i]->Average_Mark;
-
InputFile >> EnteredEmployee[i]->Salary;
-
InputFile >> EnteredEmployee[i]->University;
-
}
-
}
through Employee *EnteredEmployee[] does it receive the address of the pointer, its value or it converts it to the value this pointer points to due to the dereferencing symbol * i.e. the address of the first structure?
The function is accepting an array of pointers to Employees - that is, it receives a pointer with the memory address of a pointer with the memory address of some Employee object. Thus, you essentially have a pointer to a pointer.
And one more thing,
I've tried to do the same program using only one pointer to an array of structures, and it works correctly.
I changed the previous code to: - Employee *pEnteredEmployee;
-
-
void InputDataFromFile(int EntryNumber, Employee EnteredEmployee[])
-
{
-
for (int i=0; i < EntryNumber; i++)
-
{ InputFile >> EnteredEmployee[i].Name;
-
InputFile >> EnteredEmployee[i].Surname;
-
InputFile >> EnteredEmployee[i].Grad_Year;
-
InputFile >> EnteredEmployee[i].Average_Mark;
-
InputFile >> EnteredEmployee[i].Salary;
-
InputFile >> EnteredEmployee[i].University;
-
}
-
};
-
-
int main()
-
{
-
....
-
pEnteredEmployee = new Employee [EntryNumber];
-
-
.....
-
}
What is the point then of the previous one, if we can use one pointer instead of their array and still allocate memory dinamically?
Or I don't understand anything?
The previous method was suggested in the task for the program.
Using double pointers (Employee** name) is usually used for dynamically allocated 2D arrays. This is because you can allocate any number of Employee* pointers in the original array, and each of these pointers can point to any number of Employee objects. I've never seen double pointers used for anything else, though.
So, I understand if my original task is just to allocate a dynamic array of structures Employee depending on number of entries in the file, the method with only one pointer to the array of structures will do...
Thank you very much for the reply!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Andy Read |
last post by:
Dear all,
I thought I understood passing parameters ByVal and ByRef but I
clearly don't! If I define a simple class
of:
Public Class Person
Public Name as String
Public Age as Integer
End...
|
by: jr |
last post by:
Sorry for this very dumb question, but I've clearly got a long way to go!
Can someone please help me pass an array into a function. Here's a starting
point.
void TheMainFunc()
{
// Body of...
|
by: kalinga1234 |
last post by:
there is a problem regarding passing array of characters to another
function(without using structures,pointer etc,).can anybody help me to
solve the problem.
|
by: keepyourstupidspam |
last post by:
Hi,
I want to pass a function pointer that is a class member.
This is the fn I want to pass the function pointer into:
int Scheduler::Add(const unsigned long timeout, void* pFunction, void*...
|
by: Charles Sullivan |
last post by:
The library function 'qsort' is declared thus:
void qsort(void *base, size_t nmemb, size_t size,
int(*compar)(const void *, const void *));
If in my code I write:
int cmp_fcn(...);
int...
|
by: Christopher Benson-Manica |
last post by:
Does the following program exhibit undefined behavior? Specifically,
does passing a struct by value cause undefined behavior if that struct
has as a member a pointer that has been passed to...
|
by: Mike |
last post by:
Consider the following code:
"""
struct person {
char *name;
int age;
};
typedef struct person* StructType;
|
by: Roman Mashak |
last post by:
Hello,
I belive the reason of problem is simple, but can't figure out.
This is piece of code:
struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
|
by: Ivan Liu |
last post by:
Hi,
I'd like to ask if passing an object as an pointer into a function
evokes the copy constructor.
Ivan
|
by: TS |
last post by:
I was under the assumption that if you pass an object as a param to a method
and inside that method this object is changed, the object will stay changed
when returned from the method because the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |