473,790 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing pointer to a function

62 New Member
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 pEnteredEmploye e 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 *EnteredEmploye e[] 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 2221
Ganon11
3,652 Recognized Expert Specialist
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 (*EnteredEmploy ee[i]).Name, and it will work correctly - however, note that this is the reason the '->' operator was introduced. (*Name).attribu te and Name->attribute mean the exact same thing - use whichever one you prefer.
Feb 10 '07 #2
jewel87
62 New Member
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 New Member
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 Recognized Expert Specialist
if I understand it correctly, the pEnteredEmploye e 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 *EnteredEmploye e[] 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 New Member
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
16845
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 Class
58
10182
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 code... TCHAR myArray; DoStuff(myArray);
8
4117
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
8831
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* pParam)
17
3605
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 (*fcmp)() = &cmp_fcn; qsort(..., fcmp); then everything works. But if instead I code qsort as:
17
2813
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 free()? #include <stdlib.h> struct stype { int *foo; };
12
5405
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
6
3031
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
2101
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
3307
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 object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10145
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9986
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4094
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.