Struct with Pointers in C++
I haven't been brushed up on my Data Structures and I am encountering a problem using structures.
I want to create a structure that will be pointers to values from an array I take from an input file.
Say for example I created a structure here:
struct complexnums {
float * real; //A ptr to real list
float * imag; //A ptr to imag list
};
int main()
{
//Lets say this is an array I have taken from file input
float real = {1.0, 2.0, 3.0, 4.0};
float imag = {0.5, 1.0, 1.5, 2.0};
//How can I assign the structure ptr's to these arrays?
//Do I do it like this?
complexnums complex = {&real[0],&imag[0]};
}
Given the example, above is that the correct way to assign the values to it? Will the struct actually get the pointers to those values above?
Also I was looking at a sample of how a struct look like and this person did this.
typedef struct {
int sample;
int *test1;
}
struct1, *struct2;
What is the difference between struct1 and struct2?
Sorry and let me know if this is understandable. If not I'll try to edit it the best I can.
c++ data-structures struct
|
show 5 more comments
I haven't been brushed up on my Data Structures and I am encountering a problem using structures.
I want to create a structure that will be pointers to values from an array I take from an input file.
Say for example I created a structure here:
struct complexnums {
float * real; //A ptr to real list
float * imag; //A ptr to imag list
};
int main()
{
//Lets say this is an array I have taken from file input
float real = {1.0, 2.0, 3.0, 4.0};
float imag = {0.5, 1.0, 1.5, 2.0};
//How can I assign the structure ptr's to these arrays?
//Do I do it like this?
complexnums complex = {&real[0],&imag[0]};
}
Given the example, above is that the correct way to assign the values to it? Will the struct actually get the pointers to those values above?
Also I was looking at a sample of how a struct look like and this person did this.
typedef struct {
int sample;
int *test1;
}
struct1, *struct2;
What is the difference between struct1 and struct2?
Sorry and let me know if this is understandable. If not I'll try to edit it the best I can.
c++ data-structures struct
7
Is there a reason for you to use C-style array ? Is there a reason to use raw pointers ?
– Pierre Antoine Guillaume
Nov 21 '18 at 21:25
@PierreAntoineGuillaume is there a reason not to?
– van dench
Nov 21 '18 at 21:26
1
Also, it's hard to answer this kind of question because you actually have 3/4 questions embedded into 1.
– MPops
Nov 21 '18 at 21:30
2
@vandench I guess so. I think handeling references and stl objects makes a better learning for c++. Memory leaks are definitely an issue in numerous programs because it is a complex problem, linked to hard to understand problems like error handeling. So I'd advice std::vectors and smart pointers instead.
– Pierre Antoine Guillaume
Nov 21 '18 at 21:30
1
Your edit just broke your question. It was fine before then. I was about to comment that yes: it is (was) fine, except overly complicated because you could have just used= { real, imag }since array types are elided to a pointer.
– paddy
Nov 21 '18 at 21:39
|
show 5 more comments
I haven't been brushed up on my Data Structures and I am encountering a problem using structures.
I want to create a structure that will be pointers to values from an array I take from an input file.
Say for example I created a structure here:
struct complexnums {
float * real; //A ptr to real list
float * imag; //A ptr to imag list
};
int main()
{
//Lets say this is an array I have taken from file input
float real = {1.0, 2.0, 3.0, 4.0};
float imag = {0.5, 1.0, 1.5, 2.0};
//How can I assign the structure ptr's to these arrays?
//Do I do it like this?
complexnums complex = {&real[0],&imag[0]};
}
Given the example, above is that the correct way to assign the values to it? Will the struct actually get the pointers to those values above?
Also I was looking at a sample of how a struct look like and this person did this.
typedef struct {
int sample;
int *test1;
}
struct1, *struct2;
What is the difference between struct1 and struct2?
Sorry and let me know if this is understandable. If not I'll try to edit it the best I can.
c++ data-structures struct
I haven't been brushed up on my Data Structures and I am encountering a problem using structures.
I want to create a structure that will be pointers to values from an array I take from an input file.
Say for example I created a structure here:
struct complexnums {
float * real; //A ptr to real list
float * imag; //A ptr to imag list
};
int main()
{
//Lets say this is an array I have taken from file input
float real = {1.0, 2.0, 3.0, 4.0};
float imag = {0.5, 1.0, 1.5, 2.0};
//How can I assign the structure ptr's to these arrays?
//Do I do it like this?
complexnums complex = {&real[0],&imag[0]};
}
Given the example, above is that the correct way to assign the values to it? Will the struct actually get the pointers to those values above?
Also I was looking at a sample of how a struct look like and this person did this.
typedef struct {
int sample;
int *test1;
}
struct1, *struct2;
What is the difference between struct1 and struct2?
Sorry and let me know if this is understandable. If not I'll try to edit it the best I can.
c++ data-structures struct
c++ data-structures struct
edited Nov 21 '18 at 21:44
NoobestPros
asked Nov 21 '18 at 21:21
NoobestProsNoobestPros
307
307
7
Is there a reason for you to use C-style array ? Is there a reason to use raw pointers ?
– Pierre Antoine Guillaume
Nov 21 '18 at 21:25
@PierreAntoineGuillaume is there a reason not to?
– van dench
Nov 21 '18 at 21:26
1
Also, it's hard to answer this kind of question because you actually have 3/4 questions embedded into 1.
– MPops
Nov 21 '18 at 21:30
2
@vandench I guess so. I think handeling references and stl objects makes a better learning for c++. Memory leaks are definitely an issue in numerous programs because it is a complex problem, linked to hard to understand problems like error handeling. So I'd advice std::vectors and smart pointers instead.
– Pierre Antoine Guillaume
Nov 21 '18 at 21:30
1
Your edit just broke your question. It was fine before then. I was about to comment that yes: it is (was) fine, except overly complicated because you could have just used= { real, imag }since array types are elided to a pointer.
– paddy
Nov 21 '18 at 21:39
|
show 5 more comments
7
Is there a reason for you to use C-style array ? Is there a reason to use raw pointers ?
– Pierre Antoine Guillaume
Nov 21 '18 at 21:25
@PierreAntoineGuillaume is there a reason not to?
– van dench
Nov 21 '18 at 21:26
1
Also, it's hard to answer this kind of question because you actually have 3/4 questions embedded into 1.
– MPops
Nov 21 '18 at 21:30
2
@vandench I guess so. I think handeling references and stl objects makes a better learning for c++. Memory leaks are definitely an issue in numerous programs because it is a complex problem, linked to hard to understand problems like error handeling. So I'd advice std::vectors and smart pointers instead.
– Pierre Antoine Guillaume
Nov 21 '18 at 21:30
1
Your edit just broke your question. It was fine before then. I was about to comment that yes: it is (was) fine, except overly complicated because you could have just used= { real, imag }since array types are elided to a pointer.
– paddy
Nov 21 '18 at 21:39
7
7
Is there a reason for you to use C-style array ? Is there a reason to use raw pointers ?
– Pierre Antoine Guillaume
Nov 21 '18 at 21:25
Is there a reason for you to use C-style array ? Is there a reason to use raw pointers ?
– Pierre Antoine Guillaume
Nov 21 '18 at 21:25
@PierreAntoineGuillaume is there a reason not to?
– van dench
Nov 21 '18 at 21:26
@PierreAntoineGuillaume is there a reason not to?
– van dench
Nov 21 '18 at 21:26
1
1
Also, it's hard to answer this kind of question because you actually have 3/4 questions embedded into 1.
– MPops
Nov 21 '18 at 21:30
Also, it's hard to answer this kind of question because you actually have 3/4 questions embedded into 1.
– MPops
Nov 21 '18 at 21:30
2
2
@vandench I guess so. I think handeling references and stl objects makes a better learning for c++. Memory leaks are definitely an issue in numerous programs because it is a complex problem, linked to hard to understand problems like error handeling. So I'd advice std::vectors and smart pointers instead.
– Pierre Antoine Guillaume
Nov 21 '18 at 21:30
@vandench I guess so. I think handeling references and stl objects makes a better learning for c++. Memory leaks are definitely an issue in numerous programs because it is a complex problem, linked to hard to understand problems like error handeling. So I'd advice std::vectors and smart pointers instead.
– Pierre Antoine Guillaume
Nov 21 '18 at 21:30
1
1
Your edit just broke your question. It was fine before then. I was about to comment that yes: it is (was) fine, except overly complicated because you could have just used
= { real, imag } since array types are elided to a pointer.– paddy
Nov 21 '18 at 21:39
Your edit just broke your question. It was fine before then. I was about to comment that yes: it is (was) fine, except overly complicated because you could have just used
= { real, imag } since array types are elided to a pointer.– paddy
Nov 21 '18 at 21:39
|
show 5 more comments
2 Answers
2
active
oldest
votes
To answer the second part of your question, in the statement:
typedef struct myStruct {
//...
} structType, *ptrToStructType;
The resulting type definitions are structType, being shorthand for struct myStruct, and ptrToStructType being short for structType*. Struct identifiers are optional (myStruct in this example, ommited in yours).
See:
Confusion with typedef and pointers in C
add a comment |
To answer the first question, yes you can assign the pointers that way and is correct. But anybody reviewing the code could be confused. Thus renaming members and variables it can be a lot clearer.
In assigning complex by first defining it and then assigning the individual members it is clear that don't become mixed as it is easy with direct initialization to assign reals to imaginaries and vice versa.
struct complexnums {
float * reals; // A ptr to array of reals
float * imags; // A ptr to array of imaginarys
};
int main()
{
float realArray = {1.0, 2.0, 3.0, 4.0};
float imagArray = {0.5, 1.0, 1.5, 2.0};
complexnums complex;
complex.reals = realArray;
complex.imags = imagArray;
}
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%2f53420631%2fstruct-with-pointers-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
To answer the second part of your question, in the statement:
typedef struct myStruct {
//...
} structType, *ptrToStructType;
The resulting type definitions are structType, being shorthand for struct myStruct, and ptrToStructType being short for structType*. Struct identifiers are optional (myStruct in this example, ommited in yours).
See:
Confusion with typedef and pointers in C
add a comment |
To answer the second part of your question, in the statement:
typedef struct myStruct {
//...
} structType, *ptrToStructType;
The resulting type definitions are structType, being shorthand for struct myStruct, and ptrToStructType being short for structType*. Struct identifiers are optional (myStruct in this example, ommited in yours).
See:
Confusion with typedef and pointers in C
add a comment |
To answer the second part of your question, in the statement:
typedef struct myStruct {
//...
} structType, *ptrToStructType;
The resulting type definitions are structType, being shorthand for struct myStruct, and ptrToStructType being short for structType*. Struct identifiers are optional (myStruct in this example, ommited in yours).
See:
Confusion with typedef and pointers in C
To answer the second part of your question, in the statement:
typedef struct myStruct {
//...
} structType, *ptrToStructType;
The resulting type definitions are structType, being shorthand for struct myStruct, and ptrToStructType being short for structType*. Struct identifiers are optional (myStruct in this example, ommited in yours).
See:
Confusion with typedef and pointers in C
answered Nov 21 '18 at 21:44
Hal JarrettHal Jarrett
431411
431411
add a comment |
add a comment |
To answer the first question, yes you can assign the pointers that way and is correct. But anybody reviewing the code could be confused. Thus renaming members and variables it can be a lot clearer.
In assigning complex by first defining it and then assigning the individual members it is clear that don't become mixed as it is easy with direct initialization to assign reals to imaginaries and vice versa.
struct complexnums {
float * reals; // A ptr to array of reals
float * imags; // A ptr to array of imaginarys
};
int main()
{
float realArray = {1.0, 2.0, 3.0, 4.0};
float imagArray = {0.5, 1.0, 1.5, 2.0};
complexnums complex;
complex.reals = realArray;
complex.imags = imagArray;
}
add a comment |
To answer the first question, yes you can assign the pointers that way and is correct. But anybody reviewing the code could be confused. Thus renaming members and variables it can be a lot clearer.
In assigning complex by first defining it and then assigning the individual members it is clear that don't become mixed as it is easy with direct initialization to assign reals to imaginaries and vice versa.
struct complexnums {
float * reals; // A ptr to array of reals
float * imags; // A ptr to array of imaginarys
};
int main()
{
float realArray = {1.0, 2.0, 3.0, 4.0};
float imagArray = {0.5, 1.0, 1.5, 2.0};
complexnums complex;
complex.reals = realArray;
complex.imags = imagArray;
}
add a comment |
To answer the first question, yes you can assign the pointers that way and is correct. But anybody reviewing the code could be confused. Thus renaming members and variables it can be a lot clearer.
In assigning complex by first defining it and then assigning the individual members it is clear that don't become mixed as it is easy with direct initialization to assign reals to imaginaries and vice versa.
struct complexnums {
float * reals; // A ptr to array of reals
float * imags; // A ptr to array of imaginarys
};
int main()
{
float realArray = {1.0, 2.0, 3.0, 4.0};
float imagArray = {0.5, 1.0, 1.5, 2.0};
complexnums complex;
complex.reals = realArray;
complex.imags = imagArray;
}
To answer the first question, yes you can assign the pointers that way and is correct. But anybody reviewing the code could be confused. Thus renaming members and variables it can be a lot clearer.
In assigning complex by first defining it and then assigning the individual members it is clear that don't become mixed as it is easy with direct initialization to assign reals to imaginaries and vice versa.
struct complexnums {
float * reals; // A ptr to array of reals
float * imags; // A ptr to array of imaginarys
};
int main()
{
float realArray = {1.0, 2.0, 3.0, 4.0};
float imagArray = {0.5, 1.0, 1.5, 2.0};
complexnums complex;
complex.reals = realArray;
complex.imags = imagArray;
}
answered Nov 21 '18 at 22:23
Bo RBo R
928111
928111
add a comment |
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%2f53420631%2fstruct-with-pointers-in-c%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

7
Is there a reason for you to use C-style array ? Is there a reason to use raw pointers ?
– Pierre Antoine Guillaume
Nov 21 '18 at 21:25
@PierreAntoineGuillaume is there a reason not to?
– van dench
Nov 21 '18 at 21:26
1
Also, it's hard to answer this kind of question because you actually have 3/4 questions embedded into 1.
– MPops
Nov 21 '18 at 21:30
2
@vandench I guess so. I think handeling references and stl objects makes a better learning for c++. Memory leaks are definitely an issue in numerous programs because it is a complex problem, linked to hard to understand problems like error handeling. So I'd advice std::vectors and smart pointers instead.
– Pierre Antoine Guillaume
Nov 21 '18 at 21:30
1
Your edit just broke your question. It was fine before then. I was about to comment that yes: it is (was) fine, except overly complicated because you could have just used
= { real, imag }since array types are elided to a pointer.– paddy
Nov 21 '18 at 21:39