Pointers
Pointers as Function Arguments - Call by Reference
Call by Value
#include <iostream>
using std::cout;
using std::endl;
void Increment (int a)
{
a = a + 1;
cout << "Address of Variable a in increment = " << &a << endl;
}
int main()
{
int a;
a = 10;
cout << "Address of Variable a in Local Scope = " << &a << endl;
Increment(a);
cout << "a = " << a << endl;
}
Output:
Address of Variable a in Local Scope = 0x7ffee34ae60c
Address of Variable a in increment = 0x7ffee34ae5dc
a = 10
Pass by Pointer
#include <iostream>
using std::cout;
using std::endl;
void Increment (int *p)
{
*p = *p + 1;
}
int main()
{
int a;
a = 10;
Increment(&a);
cout << "a = " << a << endl;
}
Output:
a = 11
Pointers and Arrays
Example Code
#include <iostream>
using std::cout;
using std::endl;
int main()
{
int A[] = {2,4,5,8,1};
cout << "Return Address of A: " << endl;
cout << A << endl;
cout << &A[0] << endl;
cout << "Return Value of A: " << endl;
cout << A[0] << endl;
cout << *A << endl;
}
Output
Return Address of A:
0x7ffeeb89e5f0
0x7ffeeb89e5f0
Return Value of A:
2
2
Arrays as Functions Arguments
Example 1
#include <iostream>
using std::cout;
using std::endl;
int SumOfElements(int A[], int size)
{
int sum = 0;
for(int i = 0; i< size; i++)
{
sum += A[i];
}
return sum;
}
int main()
{
int A[] = {1,2,3,4,5};
int size = sizeof(A)/sizeof(A[0]);
int total = SumOfElements(A, size);
cout << "Sum of the Elements: " << total << endl;
}
Output
Sum of the Elements: 15
Example
Output
Example
Output
Character Arrays and Pointers Part 1
How to store Strings Example 1
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char C[4];
C[0] = 'J';
C[1] = 'o';
C[2] = 'h';
C[3] = 'n';
cout << C << endl;
}
Output:
John ���
- Notice all the Extra Characters!
Example 2
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char C[5];
C[0] = 'J';
C[1] = 'o';
C[2] = 'h';
C[3] = 'n';
C[4] = '\0';
cout << C << endl;
}
Output:
John
Example 3
-
An Array of 20, which is way bigger than the number of Char we include in the Array
-
The NULL termination will end once found (\0)
#include <iostream>
using std::cout;
using std::endl;
int main()
{
char C[20];
C[0] = 'J';
C[1] = 'o';
C[2] = 'h';
C[3] = 'n';
C[4] = '\0';
cout << C << endl;
}
Output:
John
Arrays and Pointers are different types that used in similar manner
Arrays are always passed to function by reference (only pass the base address)
Another Example
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
void print_array(char* C)
{
int i = 0;
while(C[i] != '\0')
{
cout << "index: " << i << ": " << C[i] << " " << "\n";
i++;
}
cout << endl;
}
int main()
{
char C[20] = "Hello";
print_array(C);
}
Output
index: 0: H
index: 1: e
index: 2: l
index: 3: l
index: 4: o
Character Arrays and Pointers Part 2
Example Code
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
void print_array(char C[])
{
while(*C != '\0')
{
cout << *C;
C++;
}
cout << endl;
}
int main()
{
char C[20] = "Hello";
print_array(C);
}
Output
Hello
Another Example
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
void print(char *C)
{
C[0] = 'A'; // Change value in Array
while(*C != '\0')
{
cout << "C: " << *C << endl;
C++;
}
cout << endl;
}
int main()
{
char C[20] = "Hello";
print(C);
}
Output
C: A
C: e
C: l
C: l
C: o
Another Example using const keyword
- With the const Keyword, we can’t change/overwrite any positions in the array
#include <iostream>
using std::cout;
using std::endl;
#include <string>
using std::string;
void print(const char *C)
{
// C[0] = 'A'; // Change value in Array
while(*C != '\0')
{
cout << "C: " << *C << endl;
C++;
}
cout << endl;
}
int main()
{
char C[20] = "Hello";
print(C);
}
Output:
C: H
C: e
C: l
C: l
C: o