Skip to content

Commit

Permalink
[update] pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhthi12 committed May 22, 2024
1 parent 5ee412a commit c2dc5a6
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 29 deletions.
45 changes: 22 additions & 23 deletions .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
name: C/C++ CI
# name: C/C++ CI

on:
push:
branches:
- main # Modify this to match your main branch name
pull_request:
branches:
- main # Modify this to match your main branch name
# on:
# push:
# branches:
# - main # Modify this to match your main branch name
# pull_request:
# branches:
# - main # Modify this to match your main branch name

jobs:
build:
runs-on: ubuntu-latest
# jobs:
# build:
# runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up CMake
uses: cmake/setup-cmake@v1
# steps:
# - name: Checkout code
# uses: actions/checkout@v2

- name: Configure CMake
run: cmake -Bbuild -S.
# - name: Set up CMake
# uses: cmake/setup-cmake@v1

- name: Build
run: cmake --build build
# - name: Configure CMake
# run: cmake -Bbuild -S.

- name: Run tests
run: cd build && ctest --output-on-failure
# - name: Build
# run: cmake --build build

# - name: Run tests
# run: cd build && ctest --output-on-failure
32 changes: 32 additions & 0 deletions Introduction/pointer/Basic/Pointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
*
* Basic explanation:
* A pointer is a variable that stores the memory address of another variable.
* Pointers are used for various purposes, including dynamic memory allocation, arrays, and functions.
*
*/

#include <iostream>

int main(){
int var = 42;
int* ptr = &var;

// Output the value of var
std::cout << "Value of var: " << var << std::endl;
// Output the address of var
std::cout << "Address of var: " << &var << std::endl;
// Output the address of ptr
std::cout << "Address of ptr: " << &ptr << std::endl;
// Output the address stored in ptr
std::cout << "Value stored in ptr: " << ptr << std::endl;
// Output the value pointed to by ptr
std::cout << "Value pointed to by ptr: " << *ptr << std::endl;

*ptr = 50;

//Output the new value of var
std::cout << "New value of var: " << var << std::endl;

return 0;
}
45 changes: 45 additions & 0 deletions Introduction/pointer/Exercises/PointerArithmetic.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @file PointerArithmetic.cpp
* @author your name (you@domain.com)
* @brief
* @version 0.1
* @date 2024-05-22
*
* @copyright Copyright (c) 2024
*
* Write a program that uses pointer arithmetic to iterate over an array of integers and print out each element.
*/

#include <iostream>
#include <iterator>

using namespace std;

/**
* total_size_of_arr = number_of_elements * size_of_each_element
= 5 * sizeof(int)
= 5 * 4
= 20 bytes
* sizeof(arr) = 20
* 1 byte = 8 bit
* int = 4byte | char = 1byte | string usually implemented as a dynamic array
* The & operator (address of the operator) is placed before the variable name and knows the address of the first memory cell in the memory area of that variable.
* The * operator (referencing operator or indirection operator) is placed before an address to retrieve the value stored at that address.
* *&a ~ a
* <data_type> *<pointer_variable_name>;
* void *p;
*/

int main(int argc, char **argv){
int arr[] = {1,2,3,4,5};

int* beginPtr = begin(arr);
int* endPtr = end(arr);

for (int* ptr = beginPtr; ptr != endPtr; ++ptr)
{
cout << "Element " << *ptr << endl;
}

return 0;
}
29 changes: 29 additions & 0 deletions Introduction/pointer/Exercises/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Simple Pointer Declaration and Usage:
Declare a pointer variable to an integer, assign it the address of an integer variable, and then print the value of the variable through the pointer.

Pointer Arithmetic:
Write a program that uses pointer arithmetic to iterate over an array of integers and print out each element.

Pointer to Pointer:
Declare a pointer to a pointer to an integer. Allocate memory for an integer, assign its address to the first pointer, and then assign the address of the first pointer to the second pointer. Print out the value of the integer using both pointers.

Dynamic Memory Allocation:
Write a program that dynamically allocates an array of integers of size n (input from the user), initializes the elements, and then prints them out using pointer notation.

Swap Function with Pointers:
Write a function that swaps the values of two integers using pointers. Test the function in the main program.

Passing Pointers to Functions:
Write a function that takes an array of integers and its size as arguments and returns the sum of all elements. Use pointers to access array elements within the function.

Pointer to Function:
Define a function that accepts a pointer to another function as an argument. Use this function to call different functions based on the pointer passed.

Pointer to Constant Data:
Declare a constant integer variable and a pointer to a constant integer variable. Try to change the value of the constant integer through the pointer. Repeat the process with a pointer to a constant pointer.

Dynamic Memory Allocation for 2D Arrays:
Write a program that dynamically allocates memory for a 2D array of integers of size m x n (input from the user), initializes the elements, and then prints them out using pointer notation.

Linked List Implementation:
Implement a simple linked list data structure in C++. Define a Node structure with an integer data field and a pointer to the next Node. Write functions to insert elements at the beginning and end of the list, delete elements, and print the list.
25 changes: 25 additions & 0 deletions Introduction/pointer/Exercises/SimplePointer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @file SimplePointer.cpp
* @author your name (you@domain.com)
* @brief
* @version 0.1
* @date 2024-05-22
*
* @copyright Copyright (c) 2024
*
* Declare a pointer variable to an integer, assign it the address of an integer variable, and then print the value of the variable through the pointer.
*/

#include <iostream>

int main() {
int var = 10;
int* ptr = &var;

std::cout << "Original value of var: " << var << std::endl;

*ptr = 20;
std::cout << "Modified value of var: " << var << std::endl;

return 0;
}
6 changes: 0 additions & 6 deletions hello.c

This file was deleted.

0 comments on commit c2dc5a6

Please sign in to comment.