Skip to content

Instantly share code, notes, and snippets.

@markusbuchholz
Created May 22, 2021 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markusbuchholz/0f12ea089f5d5058d54b9f30c2028782 to your computer and use it in GitHub Desktop.
Save markusbuchholz/0f12ea089f5d5058d54b9f30c2028782 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
int main()
{
int arr[4] = {2, 4, 8, 16};
int *ptr_arr = &arr[0];
size_t lengthOfArray = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < lengthOfArray; i++)
{
std::cout << "value : " << arr[i] << " : "
<< "address : " << &arr[i] << std::endl;
}
for (int i = 0; i < lengthOfArray; i++)
{
std::cout << "value holded by pointer : " << (ptr_arr + i) << " : "
<< "value : " << *(ptr_arr + i) << std::endl;
}
std::cout << "-------------------------------------------" << std::endl;
std::string distros[4] = {"arch", "manjaro", "ubuntu", "debian"};
std::string *ptr_linux = &distros[0];
size_t lengthOfDist = sizeof(distros) / sizeof(distros[0]);
for (int i = 0; i < lengthOfDist; i++)
{
std::cout << " distribution : " << distros[i] << " : "
<< " address : " << &distros[i] << std::endl;
}
for (int i = 0; i < lengthOfDist; i++)
{
std::cout << "value holded by pointer : " << (ptr_linux + i) << " : "
<< " distribution : " << *(ptr_linux + i) << std::endl;
}
}
//Compile and run. It should print simiar
/*---------------------------------------------------------------
value : 2 : address : 0x7ffd57403dd0
value : 4 : address : 0x7ffd57403dd4
value : 8 : address : 0x7ffd57403dd8
value : 16 : address : 0x7ffd57403ddc
value holded by pointer : 0x7ffd57403dd0 : value : 2
value holded by pointer : 0x7ffd57403dd4 : value : 4
value holded by pointer : 0x7ffd57403dd8 : value : 8
value holded by pointer : 0x7ffd57403ddc : value : 16
-------------------------------------------
distribution : arch : address : 0x7ffd57403de0
distribution : manjaro : address : 0x7ffd57403e00
distribution : ubuntu : address : 0x7ffd57403e20
distribution : debian : address : 0x7ffd57403e40
value holded by pointer : 0x7ffd57403de0 : distribution : arch
value holded by pointer : 0x7ffd57403e00 : distribution : manjaro
value holded by pointer : 0x7ffd57403e20 : distribution : ubuntu
value holded by pointer : 0x7ffd57403e40 : distribution : debian
---------------------------------------------------------------*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment