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 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: - 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 *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: - InputFile >> *EnteredEmployee[i].Name;
I am just trying to understand how exactly it is working.
Thanks a lot!
5 2221 Ganon11 3,652
Recognized Expert Specialist
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 (*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.
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.
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: - 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 *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: - 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 Class
|
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);
|
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*
pParam)
|
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:
| |
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;
};
|
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 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
{
|
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...
| |
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,...
|
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,...
|
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...
|
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...
|
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();...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |