Dynamic array of pointers to structs- how to pass to funcs?
I am trying to figure out how should I get some code done so I will send a dynamic array of pointers to structs to functions and will be able to free all memory at the end of the program.
Void main()
{
Some_struct **array;
array = (Some_struct**)malloc(10 * sizeof(struct));
}
Now should my function be like:
Void My_func(Some_struct** array)
{ //Code}
Or should it be like:
Void My_func(Some_struct*** array)
{ //Code}
I would like some explanation pls :)
c pointers struct
add a comment |
I am trying to figure out how should I get some code done so I will send a dynamic array of pointers to structs to functions and will be able to free all memory at the end of the program.
Void main()
{
Some_struct **array;
array = (Some_struct**)malloc(10 * sizeof(struct));
}
Now should my function be like:
Void My_func(Some_struct** array)
{ //Code}
Or should it be like:
Void My_func(Some_struct*** array)
{ //Code}
I would like some explanation pls :)
c pointers struct
1
Please include the actual code you are using that reproduces whatever problem you are having (e.g.,1-3 lines of actual use instead of// Code
+ the calling code and definitions of any required variables). (In this case I suspect that if you do that, the compiler errors when you test that code before posting it here, or lack thereof, will show you which is the correct way.)
– Arkku
Dec 31 '18 at 23:32
sizeof(struct)
is invalid C code. Suggestarray = malloc(sizeof *array * 10);
Easier to code right, review and maintain.
– chux
Jan 1 at 1:54
regarding:Void
there is no such modifier in C. Perhaps you meant:void
(all lower case) Regarding:Void main()
There are only two valid signatures formain()
they are:int main( int argc, char *argv )
andint main( void )
Notice that both signatures use a return type ofint
– user3629249
Jan 2 at 1:15
please post a Minimal, Complete, and Verifiable example so we can reproduce the problem and help you debug it.
– user3629249
Jan 2 at 1:20
add a comment |
I am trying to figure out how should I get some code done so I will send a dynamic array of pointers to structs to functions and will be able to free all memory at the end of the program.
Void main()
{
Some_struct **array;
array = (Some_struct**)malloc(10 * sizeof(struct));
}
Now should my function be like:
Void My_func(Some_struct** array)
{ //Code}
Or should it be like:
Void My_func(Some_struct*** array)
{ //Code}
I would like some explanation pls :)
c pointers struct
I am trying to figure out how should I get some code done so I will send a dynamic array of pointers to structs to functions and will be able to free all memory at the end of the program.
Void main()
{
Some_struct **array;
array = (Some_struct**)malloc(10 * sizeof(struct));
}
Now should my function be like:
Void My_func(Some_struct** array)
{ //Code}
Or should it be like:
Void My_func(Some_struct*** array)
{ //Code}
I would like some explanation pls :)
c pointers struct
c pointers struct
asked Dec 31 '18 at 23:16
Idan C.Idan C.
11
11
1
Please include the actual code you are using that reproduces whatever problem you are having (e.g.,1-3 lines of actual use instead of// Code
+ the calling code and definitions of any required variables). (In this case I suspect that if you do that, the compiler errors when you test that code before posting it here, or lack thereof, will show you which is the correct way.)
– Arkku
Dec 31 '18 at 23:32
sizeof(struct)
is invalid C code. Suggestarray = malloc(sizeof *array * 10);
Easier to code right, review and maintain.
– chux
Jan 1 at 1:54
regarding:Void
there is no such modifier in C. Perhaps you meant:void
(all lower case) Regarding:Void main()
There are only two valid signatures formain()
they are:int main( int argc, char *argv )
andint main( void )
Notice that both signatures use a return type ofint
– user3629249
Jan 2 at 1:15
please post a Minimal, Complete, and Verifiable example so we can reproduce the problem and help you debug it.
– user3629249
Jan 2 at 1:20
add a comment |
1
Please include the actual code you are using that reproduces whatever problem you are having (e.g.,1-3 lines of actual use instead of// Code
+ the calling code and definitions of any required variables). (In this case I suspect that if you do that, the compiler errors when you test that code before posting it here, or lack thereof, will show you which is the correct way.)
– Arkku
Dec 31 '18 at 23:32
sizeof(struct)
is invalid C code. Suggestarray = malloc(sizeof *array * 10);
Easier to code right, review and maintain.
– chux
Jan 1 at 1:54
regarding:Void
there is no such modifier in C. Perhaps you meant:void
(all lower case) Regarding:Void main()
There are only two valid signatures formain()
they are:int main( int argc, char *argv )
andint main( void )
Notice that both signatures use a return type ofint
– user3629249
Jan 2 at 1:15
please post a Minimal, Complete, and Verifiable example so we can reproduce the problem and help you debug it.
– user3629249
Jan 2 at 1:20
1
1
Please include the actual code you are using that reproduces whatever problem you are having (e.g.,1-3 lines of actual use instead of
// Code
+ the calling code and definitions of any required variables). (In this case I suspect that if you do that, the compiler errors when you test that code before posting it here, or lack thereof, will show you which is the correct way.)– Arkku
Dec 31 '18 at 23:32
Please include the actual code you are using that reproduces whatever problem you are having (e.g.,1-3 lines of actual use instead of
// Code
+ the calling code and definitions of any required variables). (In this case I suspect that if you do that, the compiler errors when you test that code before posting it here, or lack thereof, will show you which is the correct way.)– Arkku
Dec 31 '18 at 23:32
sizeof(struct)
is invalid C code. Suggest array = malloc(sizeof *array * 10);
Easier to code right, review and maintain.– chux
Jan 1 at 1:54
sizeof(struct)
is invalid C code. Suggest array = malloc(sizeof *array * 10);
Easier to code right, review and maintain.– chux
Jan 1 at 1:54
regarding:
Void
there is no such modifier in C. Perhaps you meant: void
(all lower case) Regarding: Void main()
There are only two valid signatures for main()
they are: int main( int argc, char *argv )
and int main( void )
Notice that both signatures use a return type of int
– user3629249
Jan 2 at 1:15
regarding:
Void
there is no such modifier in C. Perhaps you meant: void
(all lower case) Regarding: Void main()
There are only two valid signatures for main()
they are: int main( int argc, char *argv )
and int main( void )
Notice that both signatures use a return type of int
– user3629249
Jan 2 at 1:15
please post a Minimal, Complete, and Verifiable example so we can reproduce the problem and help you debug it.
– user3629249
Jan 2 at 1:20
please post a Minimal, Complete, and Verifiable example so we can reproduce the problem and help you debug it.
– user3629249
Jan 2 at 1:20
add a comment |
1 Answer
1
active
oldest
votes
It depends on what you intend to do with array
within the function. If you intend to, for example, reallocate it, then define your function's parameter with an extra level of pointer (***
).
Yes I want to reallocate my array, then it seems that the second option is correct. And I should send the array to the function like this: My_func(&array)
– Idan C.
Jan 1 at 6:52
Please read: three star programmer
– user3629249
Jan 2 at 1:18
add a comment |
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%2f53992071%2fdynamic-array-of-pointers-to-structs-how-to-pass-to-funcs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
It depends on what you intend to do with array
within the function. If you intend to, for example, reallocate it, then define your function's parameter with an extra level of pointer (***
).
Yes I want to reallocate my array, then it seems that the second option is correct. And I should send the array to the function like this: My_func(&array)
– Idan C.
Jan 1 at 6:52
Please read: three star programmer
– user3629249
Jan 2 at 1:18
add a comment |
It depends on what you intend to do with array
within the function. If you intend to, for example, reallocate it, then define your function's parameter with an extra level of pointer (***
).
Yes I want to reallocate my array, then it seems that the second option is correct. And I should send the array to the function like this: My_func(&array)
– Idan C.
Jan 1 at 6:52
Please read: three star programmer
– user3629249
Jan 2 at 1:18
add a comment |
It depends on what you intend to do with array
within the function. If you intend to, for example, reallocate it, then define your function's parameter with an extra level of pointer (***
).
It depends on what you intend to do with array
within the function. If you intend to, for example, reallocate it, then define your function's parameter with an extra level of pointer (***
).
answered Jan 1 at 0:19
James R.James R.
284
284
Yes I want to reallocate my array, then it seems that the second option is correct. And I should send the array to the function like this: My_func(&array)
– Idan C.
Jan 1 at 6:52
Please read: three star programmer
– user3629249
Jan 2 at 1:18
add a comment |
Yes I want to reallocate my array, then it seems that the second option is correct. And I should send the array to the function like this: My_func(&array)
– Idan C.
Jan 1 at 6:52
Please read: three star programmer
– user3629249
Jan 2 at 1:18
Yes I want to reallocate my array, then it seems that the second option is correct. And I should send the array to the function like this: My_func(&array)
– Idan C.
Jan 1 at 6:52
Yes I want to reallocate my array, then it seems that the second option is correct. And I should send the array to the function like this: My_func(&array)
– Idan C.
Jan 1 at 6:52
Please read: three star programmer
– user3629249
Jan 2 at 1:18
Please read: three star programmer
– user3629249
Jan 2 at 1:18
add a comment |
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%2f53992071%2fdynamic-array-of-pointers-to-structs-how-to-pass-to-funcs%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
1
Please include the actual code you are using that reproduces whatever problem you are having (e.g.,1-3 lines of actual use instead of
// Code
+ the calling code and definitions of any required variables). (In this case I suspect that if you do that, the compiler errors when you test that code before posting it here, or lack thereof, will show you which is the correct way.)– Arkku
Dec 31 '18 at 23:32
sizeof(struct)
is invalid C code. Suggestarray = malloc(sizeof *array * 10);
Easier to code right, review and maintain.– chux
Jan 1 at 1:54
regarding:
Void
there is no such modifier in C. Perhaps you meant:void
(all lower case) Regarding:Void main()
There are only two valid signatures formain()
they are:int main( int argc, char *argv )
andint main( void )
Notice that both signatures use a return type ofint
– user3629249
Jan 2 at 1:15
please post a Minimal, Complete, and Verifiable example so we can reproduce the problem and help you debug it.
– user3629249
Jan 2 at 1:20