Passing the address of an array of structures to a pointer variable
I'm having trouble understanding the logic of passing the address of an array of structures to a function for my assignment. How do I pass the address of an array of structures to a pointer variable? This is my skeleton code:
const int SIZE = 20;
struct StuStruct
{
int id, testOne, testTwo, testThree;
float average;
};
typedef StuStruct StuArr[SIZE];
void SetArray(StuArr *);
int main()
{
StuArr Students;
SetArray(&Students);
}
void SetArray(StuArr *students)
{
int *ptr = students;
}
My goal is to send Students to SetArray and use pointer arithmetic to read some data from a file, but I get the following errors in SetArray when I try to initialize the pointer variable:
int *ptr = students;
E0144: a value of type "StuArr *" cannot be used to initialize an
entity of type "int *"
C244: 'initializing': cannot convert from 'StuArr (*)' to 'int *
The way I understand arrays is that the array name itself is just a reference to its address, so when I pass Students to SetArray, I shouldn't need the reference operator, but omitting the operator gives me the error:
E0167: argument of type "StuStruct *" is incompatible with parameter
of type "StuArr *"
I've created a little dummy script to test passing array logic without the structure without issue, so I my confusion is with how I do this with passing an array of structures.
int main()
{
int Students[20];
SetArray(Students);
}
void SetArray(int *students)
{
int *ptr = students;
}
c++ arrays function pointers structure
|
show 3 more comments
I'm having trouble understanding the logic of passing the address of an array of structures to a function for my assignment. How do I pass the address of an array of structures to a pointer variable? This is my skeleton code:
const int SIZE = 20;
struct StuStruct
{
int id, testOne, testTwo, testThree;
float average;
};
typedef StuStruct StuArr[SIZE];
void SetArray(StuArr *);
int main()
{
StuArr Students;
SetArray(&Students);
}
void SetArray(StuArr *students)
{
int *ptr = students;
}
My goal is to send Students to SetArray and use pointer arithmetic to read some data from a file, but I get the following errors in SetArray when I try to initialize the pointer variable:
int *ptr = students;
E0144: a value of type "StuArr *" cannot be used to initialize an
entity of type "int *"
C244: 'initializing': cannot convert from 'StuArr (*)' to 'int *
The way I understand arrays is that the array name itself is just a reference to its address, so when I pass Students to SetArray, I shouldn't need the reference operator, but omitting the operator gives me the error:
E0167: argument of type "StuStruct *" is incompatible with parameter
of type "StuArr *"
I've created a little dummy script to test passing array logic without the structure without issue, so I my confusion is with how I do this with passing an array of structures.
int main()
{
int Students[20];
SetArray(Students);
}
void SetArray(int *students)
{
int *ptr = students;
}
c++ arrays function pointers structure
2
Well, aStuArr
is not anint
, so you can't assign a pointer to the former to a pointer to the latter. What good do you believe such an assignment would achieve, anyway? In what way do you hope it will advance you towards your ultimate goal?
– Igor Tandetnik
Nov 25 '18 at 16:26
In the example that works, replaceint
withStuStruct
everywhere (well, except the return type ofmain
). It will continue to work.
– Igor Tandetnik
Nov 25 '18 at 16:27
1
Also note that the variableStudents
in themain
function is an array ofStuStruct
structures, which means that&Students
is a pointer to the array, of typeStruStruct (*)[SIZE]
. That's sematically very different from what theSetArray
function expects. Don't use type-aliases (created withtypedef
) for pointer or array types, that makes it much harder to understand the code.
– Some programmer dude
Nov 25 '18 at 16:28
Hi Igor, from my notes and lectures I was under the impression that a pointer variable always needed to be some type of int. So I misunderstood something there as switching the pointer variable to type StuArr worked. I agree that using typedef for arrays is overly confusing, as its given me trouble in this class in the past. But for some reason, this professor insists on us using them. I appreciate all the input!
– Matt
Nov 25 '18 at 16:32
1
@matt all pointers are a type of int, but in stongly typed languages like C++ a pointer variable has a type assosiated with it, like int or somestruct and that type information is a denotion of how much space that type requires in memory. So anint * ptr;
would point to an int, and asomestruct * ptr;
would point to a somestruct.
– johnathan
Nov 25 '18 at 16:38
|
show 3 more comments
I'm having trouble understanding the logic of passing the address of an array of structures to a function for my assignment. How do I pass the address of an array of structures to a pointer variable? This is my skeleton code:
const int SIZE = 20;
struct StuStruct
{
int id, testOne, testTwo, testThree;
float average;
};
typedef StuStruct StuArr[SIZE];
void SetArray(StuArr *);
int main()
{
StuArr Students;
SetArray(&Students);
}
void SetArray(StuArr *students)
{
int *ptr = students;
}
My goal is to send Students to SetArray and use pointer arithmetic to read some data from a file, but I get the following errors in SetArray when I try to initialize the pointer variable:
int *ptr = students;
E0144: a value of type "StuArr *" cannot be used to initialize an
entity of type "int *"
C244: 'initializing': cannot convert from 'StuArr (*)' to 'int *
The way I understand arrays is that the array name itself is just a reference to its address, so when I pass Students to SetArray, I shouldn't need the reference operator, but omitting the operator gives me the error:
E0167: argument of type "StuStruct *" is incompatible with parameter
of type "StuArr *"
I've created a little dummy script to test passing array logic without the structure without issue, so I my confusion is with how I do this with passing an array of structures.
int main()
{
int Students[20];
SetArray(Students);
}
void SetArray(int *students)
{
int *ptr = students;
}
c++ arrays function pointers structure
I'm having trouble understanding the logic of passing the address of an array of structures to a function for my assignment. How do I pass the address of an array of structures to a pointer variable? This is my skeleton code:
const int SIZE = 20;
struct StuStruct
{
int id, testOne, testTwo, testThree;
float average;
};
typedef StuStruct StuArr[SIZE];
void SetArray(StuArr *);
int main()
{
StuArr Students;
SetArray(&Students);
}
void SetArray(StuArr *students)
{
int *ptr = students;
}
My goal is to send Students to SetArray and use pointer arithmetic to read some data from a file, but I get the following errors in SetArray when I try to initialize the pointer variable:
int *ptr = students;
E0144: a value of type "StuArr *" cannot be used to initialize an
entity of type "int *"
C244: 'initializing': cannot convert from 'StuArr (*)' to 'int *
The way I understand arrays is that the array name itself is just a reference to its address, so when I pass Students to SetArray, I shouldn't need the reference operator, but omitting the operator gives me the error:
E0167: argument of type "StuStruct *" is incompatible with parameter
of type "StuArr *"
I've created a little dummy script to test passing array logic without the structure without issue, so I my confusion is with how I do this with passing an array of structures.
int main()
{
int Students[20];
SetArray(Students);
}
void SetArray(int *students)
{
int *ptr = students;
}
c++ arrays function pointers structure
c++ arrays function pointers structure
edited Nov 25 '18 at 16:53
Matt
asked Nov 25 '18 at 16:23
MattMatt
256
256
2
Well, aStuArr
is not anint
, so you can't assign a pointer to the former to a pointer to the latter. What good do you believe such an assignment would achieve, anyway? In what way do you hope it will advance you towards your ultimate goal?
– Igor Tandetnik
Nov 25 '18 at 16:26
In the example that works, replaceint
withStuStruct
everywhere (well, except the return type ofmain
). It will continue to work.
– Igor Tandetnik
Nov 25 '18 at 16:27
1
Also note that the variableStudents
in themain
function is an array ofStuStruct
structures, which means that&Students
is a pointer to the array, of typeStruStruct (*)[SIZE]
. That's sematically very different from what theSetArray
function expects. Don't use type-aliases (created withtypedef
) for pointer or array types, that makes it much harder to understand the code.
– Some programmer dude
Nov 25 '18 at 16:28
Hi Igor, from my notes and lectures I was under the impression that a pointer variable always needed to be some type of int. So I misunderstood something there as switching the pointer variable to type StuArr worked. I agree that using typedef for arrays is overly confusing, as its given me trouble in this class in the past. But for some reason, this professor insists on us using them. I appreciate all the input!
– Matt
Nov 25 '18 at 16:32
1
@matt all pointers are a type of int, but in stongly typed languages like C++ a pointer variable has a type assosiated with it, like int or somestruct and that type information is a denotion of how much space that type requires in memory. So anint * ptr;
would point to an int, and asomestruct * ptr;
would point to a somestruct.
– johnathan
Nov 25 '18 at 16:38
|
show 3 more comments
2
Well, aStuArr
is not anint
, so you can't assign a pointer to the former to a pointer to the latter. What good do you believe such an assignment would achieve, anyway? In what way do you hope it will advance you towards your ultimate goal?
– Igor Tandetnik
Nov 25 '18 at 16:26
In the example that works, replaceint
withStuStruct
everywhere (well, except the return type ofmain
). It will continue to work.
– Igor Tandetnik
Nov 25 '18 at 16:27
1
Also note that the variableStudents
in themain
function is an array ofStuStruct
structures, which means that&Students
is a pointer to the array, of typeStruStruct (*)[SIZE]
. That's sematically very different from what theSetArray
function expects. Don't use type-aliases (created withtypedef
) for pointer or array types, that makes it much harder to understand the code.
– Some programmer dude
Nov 25 '18 at 16:28
Hi Igor, from my notes and lectures I was under the impression that a pointer variable always needed to be some type of int. So I misunderstood something there as switching the pointer variable to type StuArr worked. I agree that using typedef for arrays is overly confusing, as its given me trouble in this class in the past. But for some reason, this professor insists on us using them. I appreciate all the input!
– Matt
Nov 25 '18 at 16:32
1
@matt all pointers are a type of int, but in stongly typed languages like C++ a pointer variable has a type assosiated with it, like int or somestruct and that type information is a denotion of how much space that type requires in memory. So anint * ptr;
would point to an int, and asomestruct * ptr;
would point to a somestruct.
– johnathan
Nov 25 '18 at 16:38
2
2
Well, a
StuArr
is not an int
, so you can't assign a pointer to the former to a pointer to the latter. What good do you believe such an assignment would achieve, anyway? In what way do you hope it will advance you towards your ultimate goal?– Igor Tandetnik
Nov 25 '18 at 16:26
Well, a
StuArr
is not an int
, so you can't assign a pointer to the former to a pointer to the latter. What good do you believe such an assignment would achieve, anyway? In what way do you hope it will advance you towards your ultimate goal?– Igor Tandetnik
Nov 25 '18 at 16:26
In the example that works, replace
int
with StuStruct
everywhere (well, except the return type of main
). It will continue to work.– Igor Tandetnik
Nov 25 '18 at 16:27
In the example that works, replace
int
with StuStruct
everywhere (well, except the return type of main
). It will continue to work.– Igor Tandetnik
Nov 25 '18 at 16:27
1
1
Also note that the variable
Students
in the main
function is an array of StuStruct
structures, which means that &Students
is a pointer to the array, of type StruStruct (*)[SIZE]
. That's sematically very different from what the SetArray
function expects. Don't use type-aliases (created with typedef
) for pointer or array types, that makes it much harder to understand the code.– Some programmer dude
Nov 25 '18 at 16:28
Also note that the variable
Students
in the main
function is an array of StuStruct
structures, which means that &Students
is a pointer to the array, of type StruStruct (*)[SIZE]
. That's sematically very different from what the SetArray
function expects. Don't use type-aliases (created with typedef
) for pointer or array types, that makes it much harder to understand the code.– Some programmer dude
Nov 25 '18 at 16:28
Hi Igor, from my notes and lectures I was under the impression that a pointer variable always needed to be some type of int. So I misunderstood something there as switching the pointer variable to type StuArr worked. I agree that using typedef for arrays is overly confusing, as its given me trouble in this class in the past. But for some reason, this professor insists on us using them. I appreciate all the input!
– Matt
Nov 25 '18 at 16:32
Hi Igor, from my notes and lectures I was under the impression that a pointer variable always needed to be some type of int. So I misunderstood something there as switching the pointer variable to type StuArr worked. I agree that using typedef for arrays is overly confusing, as its given me trouble in this class in the past. But for some reason, this professor insists on us using them. I appreciate all the input!
– Matt
Nov 25 '18 at 16:32
1
1
@matt all pointers are a type of int, but in stongly typed languages like C++ a pointer variable has a type assosiated with it, like int or somestruct and that type information is a denotion of how much space that type requires in memory. So an
int * ptr;
would point to an int, and a somestruct * ptr;
would point to a somestruct.– johnathan
Nov 25 '18 at 16:38
@matt all pointers are a type of int, but in stongly typed languages like C++ a pointer variable has a type assosiated with it, like int or somestruct and that type information is a denotion of how much space that type requires in memory. So an
int * ptr;
would point to an int, and a somestruct * ptr;
would point to a somestruct.– johnathan
Nov 25 '18 at 16:38
|
show 3 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469478%2fpassing-the-address-of-an-array-of-structures-to-a-pointer-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53469478%2fpassing-the-address-of-an-array-of-structures-to-a-pointer-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Well, a
StuArr
is not anint
, so you can't assign a pointer to the former to a pointer to the latter. What good do you believe such an assignment would achieve, anyway? In what way do you hope it will advance you towards your ultimate goal?– Igor Tandetnik
Nov 25 '18 at 16:26
In the example that works, replace
int
withStuStruct
everywhere (well, except the return type ofmain
). It will continue to work.– Igor Tandetnik
Nov 25 '18 at 16:27
1
Also note that the variable
Students
in themain
function is an array ofStuStruct
structures, which means that&Students
is a pointer to the array, of typeStruStruct (*)[SIZE]
. That's sematically very different from what theSetArray
function expects. Don't use type-aliases (created withtypedef
) for pointer or array types, that makes it much harder to understand the code.– Some programmer dude
Nov 25 '18 at 16:28
Hi Igor, from my notes and lectures I was under the impression that a pointer variable always needed to be some type of int. So I misunderstood something there as switching the pointer variable to type StuArr worked. I agree that using typedef for arrays is overly confusing, as its given me trouble in this class in the past. But for some reason, this professor insists on us using them. I appreciate all the input!
– Matt
Nov 25 '18 at 16:32
1
@matt all pointers are a type of int, but in stongly typed languages like C++ a pointer variable has a type assosiated with it, like int or somestruct and that type information is a denotion of how much space that type requires in memory. So an
int * ptr;
would point to an int, and asomestruct * ptr;
would point to a somestruct.– johnathan
Nov 25 '18 at 16:38