Passing the address of an array of structures to a pointer variable












0















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;

}









share|improve this question




















  • 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











  • 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





    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






  • 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


















0















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;

}









share|improve this question




















  • 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











  • 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





    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






  • 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
















0












0








0








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;

}









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 16:53







Matt

















asked Nov 25 '18 at 16:23









MattMatt

256




256








  • 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











  • 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





    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






  • 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
















  • 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











  • 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





    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






  • 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










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














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
});


}
});














draft saved

draft discarded


















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
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)