473,395 Members | 1,956 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,395 software developers and data experts.

Passing pointer to a function

62
Hello,
I have a question about pointers.
Could anyone please explain to me the following:
I have a dynamically allocated array of structures like:

Expand|Select|Wrap|Line Numbers
  1. struct Employee{......};
  2.  
  3. Employee **pEnteredEmployee;
  4.  
  5. int main()
  6. {
  7. ....
  8. pEnteredEmployee = new Employee *[EntryNumber];
  9.     for (int i=0; i < EntryNumber; i++)
  10.         pEnteredEmployee[i] = new Employee;
  11. ....
  12.  
  13.  
when I call a function say
Expand|Select|Wrap|Line Numbers
  1.  
  2.    InputDataFromFile(EntryNumber, pEnteredEmployee);
  3.  
  4.  
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:

Expand|Select|Wrap|Line Numbers
  1.   void InputDataFromFile(int EntryNumber, Employee *EnteredEmployee[])
  2. {
  3.       for (int i=0; i < EntryNumber; i++)
  4.       {    InputFile >> EnteredEmployee[i]->Name;
  5.            InputFile >> EnteredEmployee[i]->Surname;
  6.            InputFile >> EnteredEmployee[i]->Grad_Year;
  7.            InputFile >> EnteredEmployee[i]->Average_Mark;
  8.            InputFile >> EnteredEmployee[i]->Salary;
  9.            InputFile >> EnteredEmployee[i]->University;
  10.        }
  11. }
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:

Expand|Select|Wrap|Line Numbers
  1.        InputFile >> *EnteredEmployee[i].Name;
I am just trying to understand how exactly it is working.

Thanks a lot!
Feb 10 '07 #1
5 2204
Ganon11
3,652 Expert 2GB
Why I cannot write in the function body like this:

Expand|Select|Wrap|Line Numbers
  1.        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.
Feb 10 '07 #2
jewel87
62
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.
Feb 10 '07 #3
jewel87
62
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:

Expand|Select|Wrap|Line Numbers
  1. Employee *pEnteredEmployee;
  2.  
  3. void InputDataFromFile(int EntryNumber, Employee EnteredEmployee[])
  4. {
  5.     for (int i=0; i < EntryNumber; i++)
  6.     {           InputFile >> EnteredEmployee[i].Name;
  7.     InputFile >> EnteredEmployee[i].Surname;
  8.     InputFile >> EnteredEmployee[i].Grad_Year;
  9.     InputFile >> EnteredEmployee[i].Average_Mark;
  10.     InputFile >> EnteredEmployee[i].Salary;
  11.     InputFile >> EnteredEmployee[i].University;
  12.    }
  13. };
  14.  
  15. int main()
  16. {
  17. ....
  18. pEnteredEmployee = new Employee [EntryNumber];    
  19.  
  20. .....
  21. }

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.
Feb 10 '07 #4
Ganon11
3,652 Expert 2GB
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:

Expand|Select|Wrap|Line Numbers
  1.   void InputDataFromFile(int EntryNumber, Employee *EnteredEmployee[])
  2. {
  3.       for (int i=0; i < EntryNumber; i++)
  4.       {    InputFile >> EnteredEmployee[i]->Name;
  5.            InputFile >> EnteredEmployee[i]->Surname;
  6.            InputFile >> EnteredEmployee[i]->Grad_Year;
  7.            InputFile >> EnteredEmployee[i]->Average_Mark;
  8.            InputFile >> EnteredEmployee[i]->Salary;
  9.            InputFile >> EnteredEmployee[i]->University;
  10.        }
  11. }
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:

Expand|Select|Wrap|Line Numbers
  1. Employee *pEnteredEmployee;
  2.  
  3. void InputDataFromFile(int EntryNumber, Employee EnteredEmployee[])
  4. {
  5.     for (int i=0; i < EntryNumber; i++)
  6.     {           InputFile >> EnteredEmployee[i].Name;
  7.     InputFile >> EnteredEmployee[i].Surname;
  8.     InputFile >> EnteredEmployee[i].Grad_Year;
  9.     InputFile >> EnteredEmployee[i].Average_Mark;
  10.     InputFile >> EnteredEmployee[i].Salary;
  11.     InputFile >> EnteredEmployee[i].University;
  12.    }
  13. };
  14.  
  15. int main()
  16. {
  17. ....
  18. pEnteredEmployee = new Employee [EntryNumber];    
  19.  
  20. .....
  21. }

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.
Feb 10 '07 #5
jewel87
62
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!
Feb 10 '07 #6

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

Similar topics

3
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...
58
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...
8
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.
6
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*...
17
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...
17
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...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
6
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 */ };
8
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
7
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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
jinu1996
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...
0
tracyyun
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...

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.