pointer
Variable is used to store value
Pointer is used to store addresss of value
pointer
Variable is used to store value
Pointer is used to store addresss of value
pointer
/*
**use of pointers is that here even if we change the value of c since the adderss is
* of c is assigned to pc it *pc will also get modified*/
int* pc, c;
c = 5;
pc = &c;
*pc = 1;
printf("%d", *pc); // Ouptut: 1
printf("%d", c); // Output: 1
poiner in c
#include <stdio.h>
int main() {
int c = 5;
int *p = &c;
printf("%d", *p); // 5
return 0;
}
poiner in c
int* pc, c, d;
c = 5;
d = -15;
pc = &c; printf("%d", *pc); // Output: 5
pc = &d; printf("%d", *pc); // Ouptut: -15
c++ pointers
#include <iostream>
using namespace std;
// isualize this on http://pythontutor.com/cpp.html#mode=edit
int main()
{
double* account_pointer = new double;
*account_pointer = 1000;
cout << "Allocated one new variable containing " << *account_pointer
<< endl;
cout << endl;
int n = 10;
double* account_array = new double[n];
for (int i = 0; i < n; i++)
{
account_array[i] = 1000 * i;
}
cout << "Allocated an array of size " << n << endl;
for (int i = 0; i < n; i++)
{
cout << i << ": " << account_array[i] << endl;
}
cout << endl;
// Doubling the array capacity
double* bigger_array = new double[2 * n];
for (int i = 0; i < n; i++)
{
bigger_array[i] = account_array[i];
}
delete[] account_array; // Deleting smaller array
account_array = bigger_array;
n = 2 * n;
cout << "Now there is room for an additional element:" << endl;
account_array[10] = 10000;
cout << 10 << ": " << account_array[10] << endl;
delete account_pointer;
delete[] account_array; // Deleting larger array
return 0;
}
poiner in c
int c, *pc;
// pc is address but c is not
pc = c; // Error
// &c is address but *pc is not
*pc = &c; // Error
// both &c and pc are addresses
pc = &c;
// both c and *pc values
*pc = c;
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us