Passing deference variable to a function [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 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.

Related

Declaring a char pointer [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 10 months ago.
Improve this question
I'm trying to make a login/register project and I have difficulties in declaring the char* tempUsername from this code (SIGSEVG segmentation fault)
char *tempUsername, *tempPassword, *tempPasswordConfirm, *tempSecurityQuestion;
/*
no other declaration for tempUsername here
*/
std::cout<<"Enter your new username:\n";
std::cin>>tempUsername;
//process stops here
if(fileSearch(newFilename(tempUsername))) {
std::cout<<"Username already exists! Choose another username!\n";
}
else {
std::cout<<"Enter your password:\n";
std::cin>>tempPassword;
std::cout<<"Confirm your password:\n";
I'm having a hard time understanding anything about pointers, so any advice is more than helpful!
char *tempUsername
std::cin>>tempUsername;
The problem here is that your pointer is uninitialised. When you try to extract from the input stream into the uninitialised pointer, the behaviour of the program will be undefined. Don't do this.
Your goal seems to be to read a string of user input. A solution that I can recommend is to use the std::string class:
std::string tempUsername;
std::cin >> tempUsername;
No need to use a pointer to achieve this.
I can use a char* as an array of chars, is that true?
It is not true in general. If you have a char* that points to an element of an array of char, then you can use that char* as an iterator to access the elements of the array.

How can make a program typing with 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 1 year ago.
Improve this question
How can make a program typing with c++?
For example with (char) can type a grapheme but i want type word.
char x;
x=='c';
But i want type for example 'equation'.
Someone know how can i do this?
Use a std::string. You can also use char* but i recommend you the first one. Remember that string goes within "" instead of ''.
string x; //You have to define what is 'x'.
x=="equation";
If you use char*, it behaves as an array of chars.
#include <string> // paste this line on top of the file
std::string name = "Bob"; // Use double quotes here.
if(name == "Bob")
{
std::cout << name << std::endl; // works as you expected
}

how to work on stl stacks inside functions 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 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.

Modify a char* 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 8 years ago.
Improve this question
I am trying to modify a c string in C++.
void modify(char* s)
{
s[0] = 'a';
}
If I do this, there will be some undefined behavior and can't run.
Let's assume s[0] is valid. I know char* s is immutable. Is there any possibility that I can modify the s[0] in place, which means, without creating a new string. Do the modification on the original string.
I think you might be misunderstanding some other answers you have seen on the web.
It is only undefined behavior to modify a string constant, not any char*.
As long as you strdup the constant string into a non-constant string, you can make whatever changes you want with it, because it is now in a mutable region of memory.
#include <stdio.h>
#include <string.h>
void modString(char* changeMe) {
changeMe[0] = 'g';
}
int main(){
char* foo = strdup("food");
puts(foo);
modString(foo);
puts(foo);
free(foo);
}

Vector items not being added [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Consider the following source code:
map<string,vector<SectionElement *>> _sections;
...
static SharedData *_shared;
...
static int iniHandler(void* user, const char* section, const char* name,
const char* value)
{
map<string,vector<SectionElement*>> iniFile = *(_shared->sections);
auto& iniSection = iniFile[ section];
auto sectionElement = new SectionElement();
sectionElement->name = name;
sectionElement->value = value;
iniSection.push_back( sectionElement);
return 1;
}
The problem with the code is that if I add an element to iniSection it works, but the vector that is retrieved from iniFile does not seem to be the same that is kept in the map. So every time the function iniHandler is called the count of the vector is zero. I am a bit at a loss here and wondering what obvious thing I am missing...
You modify a local map called iniFile. This has no effect on some other map, *(_shared->sections). Perhaps you wanted to make iniFile a reference?