Passing a LPDWORD as out parameter [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am new to C++ and I need to create a function with this structure:
BOOL myfunc(LPDWORD myLpdword){ // myLpdword must be an out parameter
DWORD myDword = 1;
myLpdword = &myDword;
return true;
}
int main(){
DWORD outDword = 20;
myfunc(&outDword);
cout << outDword << end1;
return 0;
}
I expected that the value of outDword would be 1 (changed by myfunc), but the value is not changed by myfunc.
Please, can you give me a hint to solve this problem?

Like this
BOOL myfunc(LPDWORD myLpdword){ // myLpdword must be an out parameter
*myLpdword = 1;
return true;
}
Out parameter is not something that means anything in C++. MS use it but only because they are using a consistent terminology across different languages.
In C++ terms what you did is pass a pointer to the variable you want to modify to myfunc. What the above code does is take that pointer and dereference with the * operator it to modify the variable you wanted modified.
I like that you're writing small test programs to check your understanding of C++. But as others said there's no real substitute for a decent book. Any C++ book is going to cover this.

You passed in a pointer to outDword.
myLpdword is now a pointer to outDword.
You then changed myLpdword to be a pointer to myDword.
You never did anything with the VALUE of outDword.

You assigned the pointer of a variable that will not exist after exiting the function body (read on scopes in C/C++.
To solve your problem, assign the value of the variable to the dereferenced pointer, like so: *myLpdword = myDword;. It would also be wise to check that the value of the pointer is not null before dereferencing it like this: if (myLpdword == 0) { return; } . This check doesn't guarantee that the pointer is safe to assign to, but Atleast guards you against null pointer access.

In C++ this is called pass-by-reference. You denote it with an ampersand in the function signature:
bool myfunc(DWORD &myDword) {…
The ampersands you are using now are actually getting the address of the variables, so you should remove those.

Related

Initialize a variable in c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 months ago.
Improve this question
So I want to initialize a variable with a value like this:
ADM_job_t job{42};
The structure of ADM_job_t is this one:
typedef struct adm_job* ADM_job_t;
And adm_job looks like this:
struct adm_job {
uint64_t id;
};
So my idea was to intialize my variable with an int, because is the "final" type, but I'm getting an error saying that I can not initialize an adm_job with an int. I can not change the structure that has been provided. How can I do it?
Thank you!
As mentioned in the comments you do actually have to create a adm_job object.
Here is one way
adm_job obj{42};
ADM_job_t job = &obj;
Even if you can't change the adm_job structure, you can probably still ignore ADM_job_t for object creation, and use smart pointers instead. I recommend starting with something like this:
#include <memory>
struct adm_job {
uint64_t id;
};
typedef struct adm_job* ADM_job_t;
void foo(ADM_job_t p) {
}
int main () {
//auto job = std::make_unique<adm_job>(42); // C++20
auto job = std::unique_ptr<adm_job>{new adm_job{42}}; // C++11
foo(job.get());
}
job's type is std::unique_ptr<adm_job>. As you can see, you can still pass the result of job.get() to any old C function that makes non-owning use of ADM_job_t.
You needn't worry about deleting the object like this.
Read about unique_ptr. There are some other types of smart pointer depending on who else is going to "own" the pointed two object during its lifetime.

coping an array from one object to another object [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
set &set::operator=(set const &s) {
elems = new int[s.num_elems];
num_elems = *(new size_t);
for (size_t i = 0; i < s.num_elems; i++) {//i am getting error in this line on "=" saying " a value type int* cannot be assigned tpo an entity of type int".
elems[i] = &(s.elems[i]);
}
num_elems = s.num_elems;
return *this;
};
i am trying to copy an object to another object they each has two private size_t num_elems and int *elems.i have tried changing the pointer symbols and copying the array directly but it gives me error everytime
This line is bad: num_elems = *(new size_t);
It just leaks a few bytes and does nothing useful. Delete it.
Then there is a problem where you are assigning the address of s.elems[i] into elems[i]. Which is not right because elems[i] is an integer, and the address of s.elems[i] is... well, an address.
So you need to change this line: elems[i] = &(s.elems[i]); to this: elems[i] = s.elems[i];
A few tips:
& this is called the "address of" operator. It gets the address of where something is in memory.
* this is called the "pointer dereference" operator. It helps you access what the pointer is pointing at.
When you access an array with square brackets, like this: elems[i] you're de-referencing it. Which means you're using the value that the pointer is pointing at. It's the same as doing this: *(elems + i)
Study your pointers :D They're a little tricky at first but they get easier with time and practice.

How to dereference an array element (C++) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I wanted to create an array of a specific size using a variable, but allegedly the only way to do that is to use pointers.
int size = 5;
string* myArray = new string[size];
I now want to assign an element from this array to another variable, which I believe will only work through dereferencing. The following line of code doesn't work. How do I fix it?
string line;
myArray[0] = "Hello";
line = *myArray[0];
Edit:
I just want to clarify something: Using the normal "myArray[0]" code doesn't work either. It compiles, but causes a crash. Here's some more specific code regarding what I want to do.
void MyClass::setLine(string line)
{
myLine = line; /*This is a private variable in MyClass*/
}
///////////////
MyClass* elements = new MyClass[size];
elements[0].setLine(myArray[0]);
I want to assign the array element to a private variable from a class, but the program crashes when I try to assign the value to the private variable.
If you know the size at compile time, you can use a normal array. If you only know the size at runtime, you could still use a std::vector, which is far easier to use than manual allocation.
Anyway, if you really want to learn about pointers for array managing, keep in mind that the index operator is equivalent to addition and dereference, i.e. ar[i] is the same as *(ar + i). In other words, indexing is just dereferencing at an offset.
As such, no extra dereference is needed. Just drop the asterisk in the failing line.
Valid code will look like
string line;
myArray[0] = "Hello";
line = myArray[0];
By the way you could use class std::vector instead of the array if you are going to add or remove elements from the collection.
For example
std::vector<std::string> myArray;
myArrray.reserve( 5 );
myArray.push_back( "Hello" );
line = myArray[0];

c++: Coping to an stl container that is passed by reference [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Using the following code:
class MyClass
{
public:
void GetStrings(list<std::string>& strs)
{
strs = m_strings;
}
list<std::string> m_strings;
}
Are all m_strings's elements being copied when strs = m_strings; is performed?
In other words:
Is this equal to list<std::string>& strs = m_strings; or to list<std::string> strs = m_strings; (with respect to elements copy)
yes.
Now, if you wanted to copy just a reference to the list (like you have in languages such as C# or Java) then pass a shared_ptr instead, then you will have a single list with 2 references to it.
All variable assignment in C/C++ is done by value, even if the variable being passed is a pointer (eg a 4 byte variable). Other languages are the same, its just that they cover this up with their language constructs.

C++ Pointers in function parameters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
If I declare a function:
vector<int> & call(string *target)
How should I use target in the function for a comparison with another string? For example:
string str;
if(str == //string in target){
...
}
As &target, or simply target? Also, how should the return look? I'm assuming it should be:
return &some_vector;
since that is the type in the function declaration. Finally, what about the opposite? That is:
vector<int> & call(string &target)
When in the function and wanting to use the string, is it as simple as:
*target
On your first question it's
if(str == *target){
On your second question my advice would simply be, don't. It looks like you are trying to return a reference to a local variable. That is a well known newbie mistake that will simply crash your program.
On your third question it's
if(str == target){
You need to dereference the pointer to get the object. That is, use *:
if (str == *target) {
Also, how should the return look?
You're returning a reference, not a pointer. That means you simply need to return the name of the object:
return some_vector;
Finally, what about the opposite?
If target is a reference (NOT a pointer) than you simply use the name of the object. There's no dereference involved with this.