what is the meaning of **ptr in c [duplicate] - c++

This question already has answers here:
In C, what does a variable declaration with two asterisks (**) mean?
(5 answers)
Closed 7 years ago.
I know *ptr is a pointer variable called ptr
Does **ptr mean its a pointer to a pointer?
If that is true, what is the meaning of a pointer to a pointer?
Any pointers (pun intended) are very much appreciated.

It depends on the context.
In a declaration, X **ptr declares an object that is a pointer to a pointer to X.
Outside of a declaration, **ptr dereferences both pointers, yielding the value.
Remember that a pointer is just an object that holds the address of another object. So a pointer to pointer is just that: an object that holds the address of another object, which happens to be a pointer.

Related

What are the difference between * and & for objects in c++ [duplicate]

This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
Closed 2 years ago.
First of all
I know the differences between & and * for variables but not for the objects of a class
consider I have a class let's call it a "Math"
Math math; // a simple object called math
Math* mathPtr = &math; // this will holds the address of math object
Math& mathRef = math ; // this one is exactly equivalent to the previous one
What are the differences ?!
A very big difference between pointers(*) and references(&) in nearly every situation is that a pointer is an indepedent variable and can be assigned NEW address values; whereas a reference, once assigned, can never refer to any new object until the variable goes out of scope.
Moreover a pointer can be of a null value, and a reference dosen't.

what does it do *((T**)m_ptr)? [duplicate]

This question already has answers here:
Why cast a pointer to a float into a pointer to a long, then dereference?
(5 answers)
In C, if I cast & dereference a pointer, does it matter which one I do first?
(6 answers)
Closed 4 years ago.
I see this in the code and can't understand what is going here:
T * ptr; // we have some pointer and it has proper adress
...
// Later I see this and I can't understand what is going here
ptr = *((T **)ptr);
Also, later in the code I see *((T**)ptr) = m_address;
What for this construction is used ?
*((T**)ptr)
Thanks!
It means that the author wished they'd written T** ptr instead, and are hacking around the fact that they didn't, by casting the pointer to a different type than that which it was declared with, before dereferencing it. It pretends that ptr points to T** instead of a T*.
There are some occasions on which this type punning is okay (e.g. it's commonly used with the struct sockaddr type to sort of implement polymorphism), but type punning T* to T** is very strange.
In fact, unless T is char, or T has a T* as its first member and there's no padding, it's also a code smell (and, as far as I'm aware, UB).
Avoid.

pointer getting reference to a return of a function [duplicate]

This question already has answers here:
What is a dangling pointer?
(7 answers)
Closed 5 years ago.
Imagine we have a scope where a variable is defined and a pointer with larger scope gets this variable's reference. What happens when we leave the scope knowing that the pointer is defined in the outer scope, a class member for example.
{
int a = 6;
pointa = &a; //defined out of this scope
}
//what happens here, pointa is still defined in this scope
EDIT: My question was concerning a more specific case which I cannot find an answer to (it feels like the answer lies within the dangling pointer explanation though).
Suppose foo() is a function that returns a double.
I tried this:
int* p;
p = &foo();
But then p becomes empty.
Can you please explain how this relates to a dangling pointer?
What happens when we leave the scope knowing that the pointer is defined in the outer scope, a class member for example.
The pointer becomes a dangling pointer.
Dereferencing it to access the object's members, member variables as well as member functions, will lead to undefined behavior.

Does sizeof(pointer) depend on the object type? [duplicate]

This question already has answers here:
Do all pointers have the same size in C++?
(10 answers)
The Definitive C++ Book Guide and List
(1 answer)
Is the sizeof(some pointer) always equal to four? [duplicate]
(17 answers)
Closed 8 years ago.
I'm trying to understand what a pointer to an object means. I'm wondering if A is an incomplete type, why is a pointer to A a complete type. Consider the following program:
#include <stdio.h>
class B; //B is incomplete type here
int main()
{
printf("%d\n",sizeof(B*));//4
}
Please explain why does sizeof(B*) return 4? What exactly does a pointer to an object represent in memory?
Please explain why sizeof(B*) return 4?
It returns 4 because size of a pointer variable is 4 in your system.
What exactly pointer to object represents in memory?
Objects reside in memory, and pointer to an object contains the starting memory address of that object. For example, if your B object has size 100 Byte, and it is placed in 1024-1123(100 Bytes) memory location, then a pointer to that object will hold the value `024 (starting address).
Is pointer to sizeof depends on the object type?
I guess you meant does pointer size depends on object type?. No, since pointers contain an address, it's size depends on address space of your system, not type of object it points to.
A pointer to an object represents the address of the object in memory. If in your case it is 4 bytes, it means that you have a 32 bit address space (either the OS, or your program is built in 32bits). To maintain the address of an object you don't really need any knowledge of the object, so a pointer to an incomplete type is fine.
Note that none of this applies to pointers to members, which are a completely different beast.

Difference between pass-by-reference & and *? [duplicate]

This question already has answers here:
Passing a modifiable parameter to c++ function
(12 answers)
Closed 1 year ago.
What is the difference between passing-by-reference and using the C pointer notation?
void some_function(some_type& param)
and
void some_function(some_type *param)
Thanks
When you pass a pointer to a variable in a subroutine call, the address of that variable is passed to the subroutine. To access the variable in the subroutine, the pointer has to be dereferenced.
When you pass a reference to a variable, the compiler takes care of obtaining the address of the variable when the variable is passed to the subroutine and dereferencing the variable in the subroutine.
You can't get a NULL reference: this alone gives you lots of safety
You can treat your reference as if it was an object: you can dereference it or whatever you need.
Basically you handle a safe pointer as if it was your own object.