Hi there, we’re Harisystems
"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".
For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.
1C++ Pointers
In C++, a pointer is a variable that stores the memory address of another variable. Pointers allow you to manipulate and access memory directly, enabling advanced memory management and efficient handling of data structures. In this article, we will explore the usage of pointers in C++ with examples.
1. Declaring and Initializing Pointers
To declare a pointer in C++, you use the asterisk (*) before the pointer name. You can initialize a pointer by assigning the address of a variable to it using the address-of operator (&). Here's an example:
#include <iostream>
int main() {
int number = 10;
int* pointer = &number;
std::cout << "Value of number: " << number << std::endl;
std::cout << "Address of number: " << &number << std::endl;
std::cout << "Value of pointer: " << pointer << std::endl;
std::cout << "Value pointed to by pointer: " << *pointer << std::endl;
return 0;
}
In the above code, we declare an integer variable number
and a pointer variable pointer
. We assign the address of number
to pointer
. The *
operator is used to dereference the pointer and access the value it points to.
2. Pointer Arithmetic
Pointers in C++ support arithmetic operations such as addition and subtraction. Here's an example:
#include <iostream>
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int* pointer = numbers;
std::cout << "First element: " << *pointer << std::endl;
std::cout << "Second element: " << *(pointer + 1) << std::endl;
std::cout << "Third element: " << *(pointer + 2) << std::endl;
return 0;
}
In the above code, we declare an integer array numbers
and a pointer pointer
pointing to the first element of the array. We use pointer arithmetic to access the elements of the array by incrementing the pointer.
3. Dynamic Memory Allocation
Pointers are commonly used in dynamic memory allocation to allocate memory during runtime. Here's an example:
#include <iostream>
int main() {
int* pointer = new int; // Allocate memory
*pointer = 10; // Assign value
std::cout << "Value: " << *pointer << std::endl;
delete pointer; // Deallocate memory
return 0;
}
In the above code, we use the new
operator to allocate memory for an integer variable. We assign a value to the memory location using the dereferenced pointer. Finally, we deallocate the memory using the delete
operator to prevent memory leaks.
4. Pointers and Functions
Pointers are often used to pass arguments by reference to functions, allowing the function to modify the original value. Here's an example:
#include <iostream>
void square(int* number) {
*number = *number * *number;
}
int main() {
int number = 5;
square(&number);
std::cout << "Squared value: " << number << std::endl;
return 0;
}
In the above code, we define a function square
that takes a pointer to an integer as a parameter. The function squares the value pointed to by the pointer. We pass the address of the number
variable to the function using the address-of operator (&
).
Pointers provide powerful capabilities in C++ for memory manipulation and advanced data handling. They enable dynamic memory allocation, pointer arithmetic, and passing arguments by reference. Mastering the use of pointers allows you to efficiently manage memory and build more sophisticated algorithms and data structures in your C++ programs.
4.5L
Learners
20+
Instructors
50+
Courses
6.0L
Course enrollments
Future Trending Courses
When selecting, a course, Here are a few areas that are expected to be in demand in the future:.
Future Learning for all
If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.
View CoursesMost Popular Course topics
These are the most popular course topics among Software Courses for learners