{"id":109,"date":"2026-05-20T11:46:25","date_gmt":"2026-05-20T11:46:25","guid":{"rendered":"https:\/\/gigz.pk\/cpp\/?post_type=lesson&#038;p=109"},"modified":"2026-05-22T10:38:45","modified_gmt":"2026-05-22T10:38:45","slug":"pointer-arithmetic","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/cpp\/?lesson=pointer-arithmetic","title":{"rendered":"Pointer Arithmetic"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Pointer arithmetic in C++ allows you to perform mathematical operations on pointers. It is mainly used for traversing arrays and working with memory efficiently.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Pointer Arithmetic?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Pointer arithmetic means performing operations like increment, decrement, addition, and subtraction on pointers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Since pointers store memory addresses, these operations move the pointer to different memory locations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Pointer Arithmetic?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Pointer arithmetic is useful because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Helps traverse arrays efficiently<\/li>\n\n\n\n<li>Provides direct memory access<\/li>\n\n\n\n<li>Improves performance<\/li>\n\n\n\n<li>Is widely used in low-level programming<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Pointer Arithmetic Operations<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">C++ supports the following operations on pointers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Increment (<code>++<\/code>)<\/li>\n\n\n\n<li>Decrement (<code>--<\/code>)<\/li>\n\n\n\n<li>Addition (<code>+<\/code>)<\/li>\n\n\n\n<li>Subtraction (<code>-<\/code>)<\/li>\n\n\n\n<li>Difference between pointers<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Incrementing a Pointer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Incrementing a pointer moves it to the next memory location of its data type.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    int arr&#91;3] = {10, 20, 30};<br><br>    int *ptr = arr;<br><br>    cout &lt;&lt; *ptr &lt;&lt; endl;<br><br>    ptr++;<br><br>    cout &lt;&lt; *ptr &lt;&lt; endl;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>10<br>20<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How Increment Works<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If an integer takes 4 bytes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First address \u2192 1000<\/li>\n\n\n\n<li>Next address \u2192 1004<\/li>\n\n\n\n<li>Next address \u2192 1008<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The pointer automatically moves according to data type size.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Decrementing a Pointer<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>ptr--;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This moves the pointer to the previous memory location.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Adding Value to Pointer<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>ptr = ptr + 2;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This moves the pointer forward by 2 elements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example of Addition<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    int arr&#91;5] = {1, 2, 3, 4, 5};<br><br>    int *ptr = arr;<br><br>    ptr = ptr + 3;<br><br>    cout &lt;&lt; *ptr;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>4<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Subtracting Pointers<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can find the distance between two pointers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;<br>using namespace std;<br><br>int main() {<br><br>    int arr&#91;5] = {10, 20, 30, 40, 50};<br><br>    int *ptr1 = &amp;arr&#91;1];<br>    int *ptr2 = &amp;arr&#91;4];<br><br>    cout &lt;&lt; ptr2 - ptr1;<br><br>    return 0;<br>}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>3<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Pointer Arithmetic with Arrays<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Arrays and pointers are closely connected.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int arr&#91;3] = {5, 10, 15};<br><br>cout &lt;&lt; *(arr + 1);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Output<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>10<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Important Rules of Pointer Arithmetic<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Arithmetic works based on data type size<\/li>\n\n\n\n<li>Only valid within array boundaries<\/li>\n\n\n\n<li>Cannot multiply or divide pointers<\/li>\n\n\n\n<li>Avoid accessing invalid memory locations<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Pointer Arithmetic<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Faster array traversal<\/li>\n\n\n\n<li>Efficient memory handling<\/li>\n\n\n\n<li>Useful in system programming<\/li>\n\n\n\n<li>Reduces indexing overhead<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Risks of Incorrect Pointer Arithmetic<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Accessing invalid memory<\/li>\n\n\n\n<li>Program crashes<\/li>\n\n\n\n<li>Undefined behavior<\/li>\n\n\n\n<li>Security issues<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Pointer Arithmetic is Important<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Pointer arithmetic is important because it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Supports efficient memory operations<\/li>\n\n\n\n<li>Helps in array processing<\/li>\n\n\n\n<li>Is used in data structures<\/li>\n\n\n\n<li>Is essential in advanced C++ programming<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real-Life Example<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Think of hotel room numbers:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Moving from one room to the next follows a sequence<\/li>\n\n\n\n<li>Pointer arithmetic moves between memory locations similarly<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Pointer arithmetic in C++ allows programmers to navigate memory efficiently using pointers. It is widely used in arrays, dynamic memory management, and advanced programming concepts for high-performance applications.<audio autoplay=\"\"><\/audio><\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/cpp\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate C++ > Pointers > Pointer Arithmetic<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><\/div>\n","protected":false},"menu_order":31,"template":"","class_list":["post-109","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Pointer Arithmetic - Learn C++Language with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn C++ pointer arithmetic with examples of increment, decrement, addition, subtraction, and array traversal techniques.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Pointer Arithmetic - Learn C++Language with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn C++ pointer arithmetic with examples of increment, decrement, addition, subtraction, and array traversal techniques.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn C++Language with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-22T10:38:45+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?lesson=pointer-arithmetic\",\"url\":\"https:\\\/\\\/gigz.pk\\\/\",\"name\":\"Pointer Arithmetic - Learn C++Language with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\"},\"datePublished\":\"2026-05-20T11:46:25+00:00\",\"dateModified\":\"2026-05-22T10:38:45+00:00\",\"description\":\"Learn C++ pointer arithmetic with examples of increment, decrement, addition, subtraction, and array traversal techniques.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/cpp\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate C++ > Pointers > Pointer Arithmetic\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/\",\"name\":\"Learn C++Language with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/cpp\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Pointer Arithmetic - Learn C++Language with GiGz.PK","description":"Learn C++ pointer arithmetic with examples of increment, decrement, addition, subtraction, and array traversal techniques.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/","og_locale":"en_US","og_type":"article","og_title":"Pointer Arithmetic - Learn C++Language with GiGz.PK","og_description":"Learn C++ pointer arithmetic with examples of increment, decrement, addition, subtraction, and array traversal techniques.","og_url":"https:\/\/gigz.pk\/","og_site_name":"Learn C++Language with GiGz.PK","article_modified_time":"2026-05-22T10:38:45+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/cpp\/?lesson=pointer-arithmetic","url":"https:\/\/gigz.pk\/","name":"Pointer Arithmetic - Learn C++Language with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/cpp\/#website"},"datePublished":"2026-05-20T11:46:25+00:00","dateModified":"2026-05-22T10:38:45+00:00","description":"Learn C++ pointer arithmetic with examples of increment, decrement, addition, subtraction, and array traversal techniques.","breadcrumb":{"@id":"https:\/\/gigz.pk\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/cpp"},{"@type":"ListItem","position":2,"name":"Intermediate C++ > Pointers > Pointer Arithmetic"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/cpp\/#website","url":"https:\/\/gigz.pk\/cpp\/","name":"Learn C++Language with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/cpp\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/cpp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}