Skip to content

Instantly share code, notes, and snippets.

@dnkm
Created April 1, 2025 23:55
Show Gist options
  • Save dnkm/9066d2c8371c61fffd83831c6c5e60c1 to your computer and use it in GitHub Desktop.
Save dnkm/9066d2c8371c61fffd83831c6c5e60c1 to your computer and use it in GitHub Desktop.
c++ pointers and references
#include <bits/stdc++.h>
using namespace std;
int main() {
int x = 10;
cout << x << endl;
cout << &x << endl;
cout << *(&x) << endl;
int *y = &x;
cout << *(y) << endl;
(*y)++;
cout << x << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
int ar[10] = {1, 3};
(*ar)++;
(*(ar + 1))++;
int *i = ar;
(*i) += 10;
i += 5;
(*i) += 10;
long long *l = (long long *)i;
l++;
(*(l + 1)) += 7;
for (auto v : ar) {
cout << v << ' ';
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> vec(10);
// pointer
int *p = &(vec[2]);
(*p) += 10;
for (size_t i = 0; i < vec.size(); i++)
vec[i] += 1;
for (int *i = &(vec[0]); i < &(vec[10]); i++)
(*i)++;
// interator
for (vector<int>::iterator it = vec.begin(); it != vec.end(); ++it)
(*it)++;
for (auto it = vec.begin(); it != vec.end(); ++it)
(*it)++;
for (int v : vec)
cout << v << ' ';
cout << endl;
for (int i = 0; i <= vec.size(); i++)
cout << vec[i] << ' ';
cout << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[10];
fill(arr, arr + 5, 5); // typesafe
memset(arr, 1, sizeof(arr)); // slight edge in speed. only used for 0
// print
for (int v : arr)
cout << v << ' ';
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> vec(10);
// fill all
// works with any standard container that has iterators
fill(vec.begin(), vec.end() - 5, 5);
memset(vec.data(), 0, (vec.size() - 7) * sizeof(int));
// printing
for (auto v : vec)
cout << v << ' ';
cout << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main() {
int x = 10;
int &y = x;
y++;
cout << x << "," << y;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
void fn(int x) { // <-- add reference
x++;
cout << x << endl;
}
int main() {
int x = 10;
fn(x);
cout << x;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
void fn(int *x) {
(*x)++;
cout << *x << endl;
}
int main() {
int x = 10;
fn(&x);
cout << x;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
void print(const int arr[], int size) {
// arr[0]++;
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int ar[10] = {1, 2};
print(ar, 10);
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
void fn(vector<int> *vec) {
cout << "Pointer function: ";
(*vec)[0] += 1;
(*vec->begin()) += 1;
*((*vec).begin()) += 1;
for (int val : *vec) {
cout << val << " ";
}
cout << endl;
}
void fn2(vector<int> &vec) {
cout << "Reference function: ";
*(vec.begin()) += 1;
for (int val : vec) {
cout << val << " ";
}
cout << endl;
}
void fn3(vector<int> vec) {
cout << "Value function (cloned): ";
vec[0]++;
for (int val : vec) {
cout << val << " ";
}
cout << endl;
}
int main() {
vector<int> vec = {1, 2, 3, 4, 5};
fn3(vec); // Pass by value (creates a copy)
fn(&vec); // Pass by pointer
fn2(vec); // Pass by reference
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment