how to work on stl stacks inside functions c++? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am new to stl in c++. I would like to know how can we work on an stl stack inside a function such that the globally declared stack which i pass to the function is modified as we modify the local stack inside the function .Is there a way to achieve this?
example- i want to do modification inside stack 'a' inside fill , will s1 be affected this way?..if not what to do?
#include<iostream>
#include<stack>
using namespace std;
stack<int> s1;
stack<int> s2;
void fill(stack<int> a,int cap){
.........
}
int main()
{
int n;
fill(s1,n);
return 0;
}

Your fill function takes the parameter a by value, which means a copy is made. No changes to a inside the function will affect the variable you passed to the function.
I suggest you make the function pass the object by reference:
void fill(stack<int>& a, int cap)
Now changes to a will update the object your passed. The fact that you may be passing a global variable to the function doesn't matter here.

Related

Variable declare & Assign value in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is it possible to declare a variable without assigning value in C++?
int a; int a=15; Which one will be Correct?
If i assign value to a variable 3 times or more which one will count at the end in C++??
int a=15;
int a=10;
int a=5;
Which value will be execute for a at the end?
int a; // declared but not assigned
a = 1; // assigning a value
a = 2; // assigning a different value
a = 3; // assigning another value
std::cout << a << "\n"; // will print 3 since only the last assignment matters
int x; declares a variable x without assigning a value.
If you assign to a variable three times then which ever assignment executed last will be the variables final value. Very important idea, C++ statements execute in a specific order, and which order they execute in is essential to understanding what a program does.

how to find the address of a function in a c++ program [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
i need to find the address of func in the stack so that if i provided a 32 long "A" string and then the function address in the stack i would get the Access granted
i remember i did it using objdump but i can't seem to figure it out
#include <iostream>
using namespace std;
void func()
{
cout << "Access Granted \n";
}
int main()
{
char buff[20];
cin >> buff;
cout << buff;
return 0;
}
i tried immunity debugger but i was not successful
how to find the address of a function in a c++ program
You can use the addressof operator to get a function pointer to the function. This pointer stores the address. Note that this cannot be done for non-static member functions because you get a member function pointer rather than a function pointer.
To print the address, you can convert the function pointer to void*. Note however that not all systems support this conversion.

Declaring variable within a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
int function(int a, int b){
a = a*b;
return(a);
}
This is a really stupid questions but, if i pass in 4 and 2.
Why doesnt the a need to be declared?
int function(int a, int b){ // two variables, a and b, are declared and set equal to whatever you passed in when you called the function.
a = a*b; // now you are using the two already-declared variables
return(a); // return the value of 'a' which was declared in the first line and then modified
}
Note that 'a' and 'b' in the above example are now destroyed. They only live inside of the function and are re-created every time you call function() with new values passed in. You can't call the 'a' and 'b' you were using in this function anywhere outside of this function, because they doesn't exist outside of this function. This means that you can declare another 'int a' and 'int b' outside of this function.
If I call this function in main:
int oldA = 5; // declare an int called oldA and set it to 5
int oldB = 10; // declare an int called oldB and set it to 10
cout << function(oldA, oldB); // makes a copy of oldA and oldB and then uses them inside function()
// note that oldA and oldB are still 5 and 10. A copy of them was made and then
// used inside of function.

Passing deference variable to a function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
This may be a trivial question for most but I am new to c++. My question is, how would I pass a pointer which is deference to a function to operate on the pointed value?
char first_name[] = "hello";
int myFunc(const char *source){
innerFunc(char *source){/*append world*/}
}
This doesnt seem to work.
One example:
char first_name[] = "hello";
int inner_func(const char* source) { /* do something, read-only */ }
int my_func(const char* source) {
inner_func(source);
}
So, you merely need to pass the name, that's all.
However, note that you have passed the pointer as const, which means that you cannot change it. Appending world does not work in that instance. In fact, if you would like to operate on your char string in a changing manner, you would need to create a second char* dynamically with the extended size. You cannot change source.
Also, inner functions like that cannot be defined in C++. Just define it outside of myFunc. You can create inner functions with lambdas, but this would be another answer.
Luckily, in C++, manipulating strings is far easier and deeply to recommend:
#include <string>
std::string first_name = "hello";
int inner_func(std::string& source) {
source += " world";
}
int my_func(std::string& source) {
inner_func(source);
}
Now, when you pass a string like first_name to my_func, it will be passed to inner_func where some string is appended.
Note, though, that hello world is quite a strange name, especially as a first name. It might not be what you want.

Passing pointer on pointers into function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Couldn't find answer to this. When I have pointer on pointers
char **buffer;
and I want to pass it to a function
void some_func(char **buffer) {}
so after this function call buffer will contain data from this function how should I call this function please ?
in your example it is correct but the fact is that you are passing this pointer to pointer by value so pass it by pointer:
// initialize it
char** buffer;
void some_func(char ***buffer) {} // by reference
and in function call:
some_funct(&buffer);
some_func(&buffer);
That's the call you need.
And the function should be...
void some_func(char ***buffer) {}
To manipulate buffer...
*buffer = something;