C++ pointers explained easy 👈

preview_player
Показать описание
C++ pointers tutorial example explained

#C++ #pointers
Рекомендации по теме
Комментарии
Автор

#include <iostream>

int main() {

// pointers = variable that stores a memory address of another variable
// sometimes it's easier to work with an address

// & address-of operator
// * dereference operator

std::string name = "Bro";
int age = 21;
std::string freePizzas[5] = {"pizza1", "pizza2", "pizza3", "pizza4", "pizza5"};

std::string *pName = &name;
int *pAge = &age;
std::string *pFreePizzas = freePizzas;

std::cout << *pName << '\n';
std::cout << *pAge << '\n';
std::cout << *pFreePizzas << '\n';

return 0;
}

BroCodez
Автор

if its not clear to everyone, arrays are just pointers, which point to the first element in the list, for example: arr[i] is the same as *(arr + i), its also one of the reasons you can write i[arr] in C++ and it will give the same output as arr[i], there is also other things that are possible with arrays/pointers:

#include <iostream>

int main(int argc, char *argv[])
{
int size = 3;
int array[size] = {10, 5, 3, 1, -2, -4, -7};

std::cout <<
std::cout << "Bytes array: " << sizeof(array) << '\n'; // Bytes array: 12
std::cout << "Bytes first element: " << sizeof(*array)<< '\n'; // Bytes first element: 4
std::cout << "Elements in array: " << << '\n'; // Elements in array: 3
std::cout <<



std::cout << *array << '\n'; // output: 10
std::cout << *(array + 2) << '\n'; // output: 3
std::cout << array[5] << '\n'; // output: -4
std::cout << array['a'] << '\n'; // output: 0, because 'a' = 97, and array[97] is not declared so it could be any number
array['a'] = 13; // the compiler doesnt give an error, but this could break everything, because its outside the array
std::cout << array['a'] << '\n'; // output: 13

std::cout <<

char str1[13] = "Hello, World"; // 12 chacters + 1 NULL character
std::string str2 = "Hello, World";

std::cout << str1 << '\n'; // output: H
std::cout << str2 << '\n'; // output: H

std::cout << sizeof(str1) << '\n'; // output: 13
std::cout << sizeof(str2) << '\n'; // output: 24
}


Obs: (in C strings are also just pointers, but in C++ they are a class)

tarsow
Автор

int diddy = 1000;
int *pDiddy = &diddy;

Pranav-nxcm
Автор

Bro has the best videos ngel. W bro❤❤👍

Leon-rw
Автор

bro you are so amazing you solved my biggest problem
of pointers love U😘😘😘😘😘😘😘😘

abdullahsaid-lhhv
Автор

int pointer = 100;
int *pPointer = &pointer

wlsedlacek
Автор

If I want to locate the pizza3 in the array, how should I use my pointer?

shreedhoni
Автор

assignment: #include <iostream>

int main()
{
double gpa[] = {2.5, 3.3, 4.3, 5.0, 4.05};
double *pGPA = gpa;
for (int i = 0; i < 5; i ++)
{
std::cout << pGPA[i] << "\n";
}

return 0;
}
What's my GPA? A, A+, or F?

HunaBopa
Автор

int main{
std:: string = Bruh;
std:: string = *pBruh = &Bruh;

std::cout << *pBruh << '\n';
}

tavares._art
Автор

#include <iostream>

int main() {

double temp = 44.2;
double *pTemp = &temp;
std::cout << *pTemp;
return 0;
}

Yousif_Maksabo
Автор

#include <iostream>

int main()
{
char grades[5] = {'A', 'B', 'C', 'D', 'F'};
char *pGrades = grades;

std::cout << "The grades are:";
for(int i = 0; i < i++)
{
std::cout << *(pGrades+i);
}
return 0;
}

code-charm-linux
Автор

Thank you! It looks like I just kept forgetting the & which is why my code never worked for my homework

MooseShower
Автор

why does the output to your freepizzas array and the pointer to said array differs when arrays are already pointers?
i tried it myself and for me it outputs the same thing

Mellows.
Автор

why don't you use namespace std : (

grimquokka
Автор

int pizza =15;
int *pPizza=&pizza;
cout<<&pizza;

dghuvcg
Автор

Bro, why don't you show yourself on your video tutorials ? I think it would be more interactive for your pupils or fans to engage learning.

HunaBopa
Автор

int main() {

std::string name = "Marcus";
std::string *pName = &name;

std::cout << *pName << '\n';

int age = 29;
int *pAge = &age;
std::cout << *pAge << '\n';

std::string countries[7] = {"India", "China", "Palestine", "Israel", "Japan", "USA", "Iraq"};
std::string* pCon = countries;
//std::cout << *pCon << '\n';
std::cout <<
std::cout << "Countries:\n";
std::cout <<
int size = sizeof(countries) / sizeof(countries[0]);

for (int i = 0; i < size; i++)
{
std::cout << *(pCon + i) << '\n';
}

return 0;
}

TomášHulina
Автор

int main(){


string favouriteGames[5] = {"Overwatch 1", "Terraria", "Rocket League", "Minecraft", "Overwatch 2"};
string *pFavouriteGames = favouriteGames;
cout << *pFavouriteGames << "\n";


}

heavyymetal