pointers in cpp
C++
#include <iostream>
using namespace std;
int main(){
//Pointer declaration
int *p, var=101;
//Assignment
p = &var;
cout<<"Address of var: "<<&var<<endl;
cout<<"Address of var: "<<p<<endl;
cout<<"Address of p: "<<&p<<endl;
cout<<"Value of var: "<<*p;
return 0;
}#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;
}#include <iostream>
using std::cout;
int main() {
/*
Some things to keep in mind:
-you shouldn't circumvent the type system if you are creating raw ptrs
and don't need to "type pun" or cast (don't use void ptrs)
-ptr types only reference memory (which are integers), not actual data, thus
they should not be treated as data types
char* is just 1 byte of mem, int* is just 4 bytes of mem, etc
- '*' means that you are creating a pointer which "points" to the mem address
of a variable
- '&', in this case, means "get the mem address of this variable"
*/
void* ptr; // a pointer that doesn't reference a certain size of memory
int* int_ptr; // a pointer that points to data with
// only 4 bytes of memory (on stack)
int a = 5; // allocates 4 bytes of mem and stores "5" there (as a primitive)
ptr = &a; // can only access the memory address of 'a' (not the data there)
int b = 45;
int_ptr = &b; // can access both memory address and data of 'b'
cout << ptr << "\n"; // prints mem address of 'a'
/*cout << *ptr << "\n"; <- this will error out; a void ptr cannot be
derefrenced */
cout << *(int*)ptr << "\n"; // type punning to get around void ptr (extra work)
cout << int_ptr << "\n"; // mem address of b
cout << *int_ptr << "\n"; // data stored at b
/* -- OUTPUTS -- */
/*
some memory address (arbitrary) which contains 05 00 00 00 as its data
5
some memory address (arbitrary) which contains 2D 00 00 00 as its data
45
*/
return 0; // you only need this if "main" isnt the linker entry point
// you also don't care
// ur also probably wondering why I didn't using namespace std... cherno
}
Also in C++:
- Title
- how to iterate over unordered_map c++
- Category
- C++
- Title
- c++ program how to let the user choose different game modes
- Category
- C++
- Title
- check an stack is empty c++
- Category
- C++
- Title
- calling by reference c++
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- peak in c++
- Category
- C++
- Title
- how to get the prime number in c++ where time complexity is 0(log n)
- Category
- C++
- Title
- c++ append to list
- Category
- C++
- Title
- how to get last element of set in c++
- Category
- C++
- Title
- c++ triple
- Category
- C++
- Title
- string substr c++
- Category
- C++
- Title
- stringstream in c++ with delimiter
- Category
- C++
- Title
- c++ overload operator
- Category
- C++
- Title
- pointers in cpp
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- SFML window
- Category
- C++
- Title
- c++ calculator program using switch case
- Category
- C++
- Title
- call by reference c++ example
- Category
- C++
- Title
- c++ program for matrix addition
- Category
- C++
- Title
- sort function in cpp
- Category
- C++
- Title
- length of array in cpp
- Category
- C++
- Title
- how to calculate inverse trigonometric values in c++
- Category
- C++
- Title
- how to convert n space separated integers in c++
- Category
- C++
- Title
- count number of zeros in array in O(logN)
- Category
- C++
- Title
- c++ compare char array
- Category
- C++
- Title
- while loops
- Category
- C++
- Title
- extends c++
- Category
- C++
- Title
- remove item from layout
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- C++ pointer arithmetic
- Category
- C++
- Title
- how to append two vectors in c++
- Category
- C++
- Title
- how to get a letter from the users string in c++
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++
- Title
- first prime numbers less than
- Category
- C++
- Title
- how to find hcf in c++
- Category
- C++
- Title
- cpp nan value
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- c++ how to skip the last element of vector
- Category
- C++
- Title
- how to declare a function in c++
- Category
- C++
- Title
- how to use wasd c++
- Category
- C++
- Title
- system("pause") note working c++
- Category
- C++
- Title
- inverser les éléments d'un tableau manuellement en c++
- Category
- C++
- Title
- c++ empty stream
- Category
- C++
- Title
- how to convert qt string to string
- Category
- C++
- Title
- How to find the suarray with maximum sum using divide and conquer
- Category
- C++
- Title
- kruskal's algorithm c++ hackerearth
- Category
- C++
- Title
- stringstream in c++
- Category
- C++
- Title
- RLE Encoding/Compression c++
- Category
- C++
- Title
- git branch in my bash prompt
- Category
- C++
- Title
- iterative inorder traversal
- Category
- C++
- Title
- c++ show time elapsed
- Category
- C++
- Title
- c++ compiler for sublime text
- Category
- C++
- Title
- c++ reading string
- Category
- C++
- Title
- vector concat c++
- Category
- C++
- Title
- c++ formatting
- Category
- C++
- Title
- c++ method name
- Category
- C++
- Title
- c++ do you not inherit constructor
- Category
- C++
- Title
- what does count function do in hashmap
- Category
- C++
- Title
- error: redefinition of ‘class Customer’
- Category
- C++
- Title
- pass ss tream as parameter c++
- Category
- C++
- Title
- convert stirng to int c++
- Category
- C++
- Title
- c++ ros subscriber
- Category
- C++
- Title
- add a timer c++
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- pow c++
- Category
- C++
- Title
- how to read and write in a file c++
- Category
- C++
- Title
- how to modulo 10^9+7
- Category
- C++
- Title
- counting valleys hackerrank solution in c++
- Category
- C++
- Title
- c++ server service ros
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- jump to case label c++
- Category
- C++
- Title
- restting a queue stl
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- c++ pointers
- Category
- C++
- Title
- bfs in C++
- Category
- C++
- Title
- maximum subarray sum equal with K in c++
- Category
- C++
- Title
- c++ parse int
- Category
- C++
- Title
- c++ get length of array
- Category
- C++
- Title
- how to take input in C++ in coding
- Category
- C++
- Title
- Create a program that finds the minimum value in these numbers
- Category
- C++
- Title
- how to use a new node c++
- Category
- C++
- Title
- new c++
- Category
- C++
- Title
- class is replace by structure
- Category
- C++
- Title
- how to print 5 precision float in c++
- Category
- C++
- Title
- Find a element in a map C++
- Category
- C++
- Title
- double max value c++
- Category
- C++
- Title
- how to sort a vector in c++
- Category
- C++
- Title
- how to show c++ binary files in sublime text
- Category
- C++
- Title
- error: ISO C++ forbids comparison between pointer and integer [-fpermissive] if(s[i] != "b"){
- Category
- C++
- Title
- new keyword in cpp
- Category
- C++
- Title
- gfg left view of tree
- Category
- C++
- Title
- array 2d to 1d
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- c++ get ascii value of char
- Category
- C++
- Title
- How to read a file in in C++
- Category
- C++
- Title
- primeros numeros primos
- Category
- C++
- Title
- random number in c++
- Category
- C++
- Title
- c++ remove item from list
- Category
- C++
- Title
- how to reverse a vector
- Category
- C++
- Title
- chess perft 5
- Category
- C++