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 ?
Related
This question already has answers here:
What does "new int(100)" do?
(4 answers)
Closed 1 year ago.
I came across two similar statements but could not exactly find the diference between them. The statements are:
int *p = new int(75);
int *p = new int[75];
Can anyone help me in knowing the difference between the above two statements.
The first returns a pointer to the integer with value of 75, but the second returns a pointer to the array of 75 integers, which don't have a value.
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.
This question already has answers here:
How to access a local variable from a different function using pointers?
(10 answers)
Closed 6 years ago.
If I understand correctly, variables that are not dynamically allocated are supposed to be deleted at the end of their scope.
However, I wanted to try it out with this code (which I think is not correct as I am supposed to use dynamic allocation) :
int* function()
{
int a = 3;
return &a;
}
int main(int argc, char const *argv[]) {
int* a(function());
std::cout << *a << std::endl; // prints 3
}
Why can I still access the value of the variable a by using the pointer returned by the function when it is supposed not to exist anymore ?
a and hence the return value from function has gone out of scope. You are just lucky.
Just be careful and compile with all the warnings enables - and take heed of those warnings.
The fact that you can access the value is pure luck. The code has undefined behaviour (since the variable is destroyed) and the compiler can generate whatever it wants - but you cannot rely on it.
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Can someone tell me the flow of references in the second printf statement in the given code?
#include<iostream>
using namespace std;
char *c[] = {"ENTNG","NST","AMAZI","FIRBE"};
char **cp[] = {c+3,c+2,c+1,c};
char ***cpp = cp;
int main(){
cout<<(**++cpp);
cout<<(*--*++cpp+3);
cout<<(*cpp[-2]+3);
cout<<(cpp[-1][-1]+1);
return 0;
}
I am trying to understand the concept of pointers, double pointers and triple pointers. Can please somebody tell me how this code works and the concept behind the solution? Thanx in advance.
A single pointer is that data type which can point to a memory location of a particular data type.. In this case, it is char so we assume it is a char pointer and it can point only to a char variable, that is, it can hold address of a character variable only.
A double pointer can hold the address of a single pointer, and a triple pointer can hold the address of a double pointer.
In your code, the first pointer 'c' is a double pointer which holds cstrings in itself. cstrings are single char pointers. then 'cp' and 'cpp' both are triple pointers.
As for the rest of the code,it is pretty messed up and looks like a nightmare. You had better read some resource on pointers and basic C++ programming. I would recomment Programming abstractions in c++ by Sir Eric Roberts from Stanford.
Also see smart pointers.