What's going on at tolower? [duplicate] - c++

This question already has answers here:
what is the meaning of (*(int (*)())a)()?
(3 answers)
Closed 4 years ago.
I was reading this answer: https://stackoverflow.com/a/5539302/588867
What is going on with this part: (int (*)(int))tolower in this line of code:
transform(s.begin(),s.end(),s.begin(),(int (*)(int))tolower );
If I make this function and the code works:
int myToLower(int c){
return tolower(c);
}
transform(s.begin(),s.end(),s.begin(), myToLower);
What's plain English about this part: (int (*)(int)).

You can see this answered in my answer, that's a function pointer. You can read more about them here: http://en.cppreference.com/w/cpp/language/pointer#Pointers_to_functions
Fundamentally this is a pointer to a function that takes in an int argument and returns an int.
The reason the transform works when using myToLower and not with an uncast tolower, is that in code is that the tolower function is overloaded in the std namespace by both the locale library's tolower and the ctype library's tolower. When only the function name is used as an uncast pointer no overload resolution is performed, and you'll get an error. When you cast the function pointer you're telling the compiler which overload you want.

Related

What are the differences between char** and char*& in CPP [duplicate]

This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 4 months ago.
For an assignment I came across this question.
What is the result of the statement following the definitions given below?
char c='a';
char *pc=&c;
char *&rc=pc ;
(*rc)++;
after printing all the different variables it shows that now variable c stores 'b'.
I didn't understand why.
Can anyone please explain?
Both char** (pointer-to-pointer) and char*& (reference-to-pointer) are second level abstractions of a value. (There's no pointer-to-reference type btw.)
The differences are really the same as they are for regular pointers vs. references: nullable vs. non-nullable etc. See here for more: https://www.geeksforgeeks.org/pointers-vs-references-cpp/
In your example, incrementing *rc does the same as would with using *pc.
Note that you don't need a symbol for accessing a references value as you would with * when using a pointer.
Just for fun: Try char** ppc = &pc. Can you tell what this does ?

How to Call function with default parameter? [duplicate]

This question already has answers here:
Skip some arguments in a C++ function?
(4 answers)
how to skip Default Arguments C++?
(4 answers)
C++ Skipping first default parameter in call
(4 answers)
Closed 2 years ago.
I have a function define in header file as this:
int myfunction(VpTR*& viewporttable, wchar* vpname=L"*Active", OpenMode f=fR);
how i can call this function but ignore second argument?
I tried calling it with the following code but an error message appeared: expected an expression
myfunction(myviewporttable,, fR);
can i omit this argument but it still understands L "* Active" as the called value?
The syntax doesn't allow to omit parameter values at arbitrary positions, only those at the very end of the list.
You have to overload 2 versions:
int myfunction(VpTR*& viewporttable, OpenMode f=fR) {
return myfunction(viewporttable,L"*Active",L"*Active",f);
}
int myfunction(VpTR*& viewporttable, wchar* vpname=L"*Active", OpenMode f=fR);
Then you can call
myfunction(myviewporttable, fR);

How can i call below C++ function with size_t* in Objective c ios [duplicate]

This question already has an answer here:
Calling C++ method from Objective C
(1 answer)
Closed 6 years ago.
I want to call this function void getResult(char, size_t*) in objective c.How can i do this please help me.
It give me below error show in image.
It seems your getResult function expects to get second parameter by reference. You can't pass function result by reference. Assign it to actual variable first and then pass reference to that.
unsigned long sizeOfC = sizeof(c);
c->getResult(array1, &sizeOfC);

How does the ifstream function "get" alters its character argument? [duplicate]

This question already has answers here:
What is a reference variable in C++?
(12 answers)
Closed 7 years ago.
The function get of an ifstream reads the next character and stores it in the argument you pass to the function. Example program:
#include <iostream>
#include <fstream>
int main () {
std::ifstream is("input.txt"); // open file
char c;
while (is.get(c)) // loop getting single characters
std::cout << c;
is.close(); // close file
return 0;
}
This works great but I am puzzled by how c can be changed by the function get as it is not passed by its pointer. I was told, some time ago, that modifying a variable within a function cannot change its value outside the function. And that's the whole purpose of pointers, right -- manipulating an object created outside the function. So how can c be changed here?
I guess there is something obvious that I don't understand here?
The member function std::istream::get() uses a reference:
istream& get (char& c);
This means that the function accesses directly to the variable passed as a parameter.
If you're not familiar with references, here you can learn more.
"So how can c be changed here?"
As from the reference documentation std::ifstream::get() uses a parameter passed by reference for the char variable
basic_istream& get( char_type& ch );
so it alters it by using this reference.

Using & operator with char data type [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is address of char data not displayed?
I was experimenting with ampersand operator and got stuck at this program :
#include<iostream>
using namespace std;
int main() {
char i='a';
cout<<&i;
return 1;
}
I was expecting the address of variable i as the output but instead the output came as the value of variable i itself.
Can anybody explain what just happened? Thanx in advance.
That's because cout::operator<< has an overload for const char*. You'll need an explicit cast to print the address:
cout<<static_cast<void*>(&i);
This will call the overload with void* as parameter, which is the one used to print addresses.
Also note that your code runs into undefined behavior. You only have a single char there, and the overload expects a null-terminated C-string.