I have a small code something like this:
#include <stdio.h>
int *ptr1;
int *ptr2;
void some_function(void)
{
int B = 5;
ptr2 = &B;
}
main (){
int C, D;
int A =10;
int *ptr3;
ptr1= &A;
ptr3=ptr2;
some_function();
C = *ptr1 + *ptr2;
printf("Sum of the numbers C= %d\n",C);
some_function();
D = *ptr1 + *ptr3;
printf("Sum of the numbers D= %d\n",D);
}
Why dont I get the result for D but get the result for C? I get the result for the print statement Sum of the numbers C=15 but nothing for the last print statement for D. What is the difference between local and global pointers ( I mean both ptr1 and ptr2 are defined global and ptr3 is define local)? Is the pointer assignment ptr3=ptr2 valid? are there any significant difference with pointer to local variable Vs. pointer to global variable ?
ptr3 = ptr2;
ptr3 is initialized with the value of ptr2 which is NULL. It is NULL because you didn't initialize it and it has static storage duration.
then you do
D = *ptr1 + *ptr3;
You are dereferencing ptr3 which is a null pointer: this is undefined behavior. The value of ptr2 may have changed during your program, but ptr3 is still a null pointer.
EDIT: For the sake of completeness (well, sort of):
void some_function(void)
{
int B = 5;
ptr2 = &B;
}
B object lifetime ends when the functions some_function exits. You cannot access B through ptr2 after some_function has returned. It means:
C = *ptr1 + *ptr2;
also invokes undefined behavior because you are dereferencing ptr2 which is an invalid pointer in this statement.
ptr3=ptr2; coped before the call of function some_function(); so wrong address assigned to ptr3 that is NULL because ptr2 is 0 (by-default value of global variable).
Do like this:
some_function();
ptr3=ptr2;
D = *ptr1 + *ptr3;
printf("Sum of the numbers D= %d\n",D);
Also, following code running correct because ptr2 value is used after call of some_function(); function.
some_function();
C = *ptr1 + *ptr2;
printf("Sum of the numbers C= %d\n",C)
Ooh!, Scope of variable ptr2 points to local to some_function().
void some_function(void)
{
int B = 5;
ptr2 = &B;
}
Do like this:
void some_function(void)
{
int* B = calloc(1,sizeof(int));
*B = 5;
ptr2 = B;
}
Also don't forget to free(ptr2) after print statement;
Related
I created a function to change the value of *a, but it remains unchanged after the function fun has been executed.
Here's my code.
#include<iostream>
using namespace std;
void fun(int *b){
int *c = new int;
*c = 4;
b = c;
}
int main(){
int *a = new int;
*a = 2;
fun(a);
cout << *a << endl; // the output is 2 but I want it be 4
return 0;
}
I want *a be 4 in function fun
There's two ways, first the C++ way is to have a mutable (non-const) reference:
void fun(int& b) {
b = 4;
}
// Calling
int x = 0;
fun(x);
Then the classic C way is to use a regular pointer:
void fun(int* b) {
*b = 4;
}
// Calling
int x = 0;
fun(&x);
The reference approach is better because there's less syntax chaff both in calling the function, and in the function's implementation. There's also no risk you might get nullptr, a reference will be defined.
Note: Your use of new here has unintended consequences, like leaking memory. When you allocate with new you are responsible for releasing that memory.
b = c;
b is pointer, this operation makes b points to the address c represents. you do not change the value located on the address of 'a'
*b = *c;
This is the right
In your function instead of b=c you should write
*b = *c to change the value stored at the address pointed by b.
That is the reason your function does not work as desired.
In case of any query please feel free to clarify.
Here, two variables a=1,b=2 and three pointer variables *p2a = &a , *q2p = p2a , *r2b = &b .
When I change *p2a to point to *r2b, then pointer *q2p which is pointing to *p2a should also change right? . But *q2p still points to old value pointed by *p2q and not the new value of newly pointed *p2q which is the value of b pointed by *r2b.
#include <iostream>
using namespace std;
int main() {
int a = 1;
int b = 2;
int* p2a = &a;
int* q2p = p2a;
int* r2b = &b;
p2a = r2b;
cout << *q2p;
return 0;
}
Output:
1
My understanding of pointers is not that good. So If my question is really dumb I'm sry for it.
If you feel hard to get the problem description, then simply my question is, Why *q2p value is not changed when *p2q is changed to point to *r2b, but still *q2p pointing to old value of *p2q.
No, q2p is not a an int** (pointing at the pointer p2a), so
int* p2a = &a;
int* q2p = p2a; // q2p is pointing at "a"
// ...
int* r2b = &b;
p2a = r2b; // q2p is still pointing at "a"
I ran into this example online and I can't figure out how it works for the life of me.
#include <iostream>
int main()
{
int const a = 1;
int* pa = (int*) &a;
*pa = 2;
printf("%p %d %p %d", (void*) &a, a, (void*) pa, *pa);
return 0;
}
When I compile it on my machine, I get:
0xffffcc04 1 0xffffcc04 2
Which means that the memory location 0xffffcc04 has value 1 and 2 simultaneously??? Is the compiler optimizing away a, or am I missing something?
P.S. when I run this in debug, a and *pa are 2...
Changing a const-value yields undefined behaviour, and the "mysterious" output is just such undefined behaviour.
It is actually needless to investigate on why a behaviour in the world of undefined behaviour is as it is then. But in your case it is probably not using a as you declare it as const, so the compiler "knows" the value and may decide not to access the variable.
Just for showing something curious, try:
int main()
{
volatile int const a = 1;
int* pa = (int*) &a;
*pa = 2;
printf("%p %d %p %d", (void*) &a, a, (void*) pa, *pa);
return 0;
}
I have a problem with the pointers. I know what this does:
*name
I understand that this is a pointer.
I've been searching but I do neither understand what this one does nor I've found helpful information
**name
The context is int **name, not multiplication
Could someone help me?
NOTE: Without the proper context, the usage of *name and **name is ambiguous. it may portrait (a). dereference operator (b) multiplication operator
Considering you're talking about a scenario like
char * name;
char **name;
in the code,
*name
name is a pointer to a char.
**name
name is a pointer, to the pointer to a char.
Please don't get confused with "double-pointer", which is sometimes used to denote pointer to a pointer but actually supposed to mean a pointer to a double data type variable.
A visual below
As above, we can say
char value = `c`;
char *p2 = &value; // &value is 8000, so p2 == 8000, &p2 == 5000
char **p1 = &p2; // &p2 == 5000, p1 == 5000
So, p1 here, is a pointer-to-pointer. Hope this make things clear now.
It's actually very simple, consider this:
int a; // a is an int
int* b; // b is a pointer to an int
int** c; // c is a pointer to a pointer to an int
If you see every level as just another variable type (so, see *int as a type), it's easier to understand.
Another example:
typedef int* IntPointer;
IntPointer a; // a is an IntPointer
IntPointer* b; // b is a pointer to an IntPointer!
Hope that helps!
pointer stores address of variable, pointer to pointer stores address of another pointer.
int var
int *ptr;
int **ptr2;
ptr = &var;
ptr2 = &ptr;
cout << "var : " << var;
cout << "*ptr : " << *ptr;
cout << "**ptr2 : " << **ptr2;
You can look here
int a = 5;// a is int, a = 5.
int *p1 = &a; // p1 is pointer, p1 point to physical address of a;
int **p2 = &p1; // p2 is pointer of pointer, p2 point to physical adress of p1;
cout<< "a = "<<a << " *p1 = "<<*p1<<" *(*p2) = " << *(*p2)<<endl;
**name in this case. Would be a pointer to a pointer.
Is it possible to make a pointer variable hold the address of another pointer variable? eg:int a;
int *ptr,*ptr1;
ptr=&a;
ptr1=&ptr;
Sure, a pointer to a pointer.
int i;
int *pi = &i;
int **ppi = π
There is nothing particularly unusual about a pointer to a pointer. It's a variable like any other, and it contains the address of a variable like any other. It's just a matter of setting the correct type so that the compiler knows what to do with them.
Yes, but it needs to have the right type. In your example int *ptr,*ptr1; both ptr and ptr1 have type "pointer to int", which can only point to an int, not a pointer. If you declare int *ptr, **ptr1;, then ptr1 has type "pointer to int *" and thus can point to ptr.
Here's a sample showing what happens
int a = 13;
int *ptr,
int **ptr1; // ** means pointer to pointer
ptr = &a;
ptr1 = &ptr;
cout << a; //value of a
cout << *ptr; //ptr points to a, *ptr is value of a
cout << **ptr1; //same as above
cout << &ptr; //prints out the address of ptr
cout << *ptr1; //same as above
It works the same for int ***ptr, int ****ptr.
Pointer to pointer is possible (and very common), but int* may not be large enough to contain the address of another int*. use int**. (or void*, for generally pointer)
There's two answers here and they're both yes.
Two pointers can point to the same location
int b, *p1=&b, *p2=&b;
*p1 = 123;
*p2; // equals 123
You can also have a pointer to a pointer:
int x=2, y=3, *p=&x, **q=&p;
Note the extra asterisk.
**q; // equals 2
*q = &y;
**q; // equals 3
**q = 4;
y; // equals 4
Yes,
Pls see the following code. I hope it will serve your purpose
int a = 4;
int *ptr = &a;
int *ptr1 = (int*)&ptr;
cout << **(int**)ptr1;
Here ptr1 is single pointer but behaves as double pointer