CARVIEW |
Select Language
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 31 Jul 2025 12:36:15 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Last-Modified: Sun, 07 Nov 2021 08:22:55 GMT
Content-Encoding: gzip
Using Pointers 3 dfferent ways - C++ Articles




- Articles
- Using Pointers 3 dfferent ways
Published by angelrapallo
Oct 22, 2012 (last update: Oct 22, 2012)
Using Pointers 3 dfferent ways
Score: 3.2/5 (157 votes)





In this small article, I show you about pointers by performing the same task using pointers in 3 different way. I think for some one learning c++ this is a very good approach because you can see pointers from different angles and get a much better idea as to what they and how to use them.
the code uses 3 different approaches using pointers to create an array of strings. you can look at the it as a sparse matrix of characters or just an array of strings like
Aa01234
Bb012
Cc0123456
etc.
the 3 approaches are offset, indexes and increments
the codes uses the #define in order to be able to compile
easily the each aproach so by just commenting these 3 lines
you can see how each approach works.
for example now it is set to run using increments
the data structure I use is the char**
this is a pointer to a pointer to a character
like -> (-> character)
I initialize it to
so is basically an array of strings like
-> ->sssssssssssss
->sssssssssssss
->sssssssssssss
array_of_stringsis the main pointer -> which we move by either method
vertically (is better to think of it this way). at the moment
we dereference *array_of_strings we then have another pointer the one that
points to the actual string, the second -> above.
so array_of_strings++ moves to the next string (vertical) and
(*array_of_strings)++ points to the next character in the string horizontal.
the first approach using offsets, in this approach we don't modify
the pointers, instead we use some offset value to point to the data
like *(pointer+5) = something. because pointers hold address we can so this
so pointer+5 points to the address of the byte which is 5 bytes from pointer
in array terminology as you will see in the array approach this is equivalent to pointer[5]. In increments terminology this is equivalent to
++pointer;++pointer;++pointer;++pointer;++pointer, incrementing the pointer 5
times.
the second approach is the best and easiest, using array indexes
array[i][j].
the third approach is the increments. here we need to modify the pointer
because we move the pointer forward or backward using the ++ and -- operators.
so p[1], *(p+1) and *++p are 3 ways to do the same thing
point pointer to one byte after pointer.
in the increment method you will see i used 2 pointers
array_of_strings and new_string they are both pointers but they
behave differently. array_of_strings is a pointer to a pointer
it points to a pointer to a byte (character), while new_string
points to the actual data the string. when we do array_of_strings++
we move array_of_strings to point to the next string.
Aa01234
*array_of_strings++ -> Bb01234567
and when we do *++new_string we point to the next character in the string
Aa01234
^
|
*++new_pointer
notice i use the increment operator before *++p not *p++ because i wanted
to increment p first and then dereference. if i had done *p++ it would process
Aa012345 or which ever string twice
bellow is the code, it has comments and I think is not hard to follow
just comment two of the #define and leave the one you want to experiment with
uncommented, then set break points and see how it works.
to run the code just create a new windows console application if using
Microsoft visual studio. if using some other tool then just copy paste
the code in your Main function
the code uses 3 different approaches using pointers to create an array of strings. you can look at the it as a sparse matrix of characters or just an array of strings like
Aa01234
Bb012
Cc0123456
etc.
the 3 approaches are offset, indexes and increments
the codes uses the #define in order to be able to compile
easily the each aproach so by just commenting these 3 lines
you can see how each approach works.
for example now it is set to run using increments
|
|
the data structure I use is the char**
this is a pointer to a pointer to a character
like -> (-> character)
I initialize it to
|
|
so is basically an array of strings like
-> ->sssssssssssss
->sssssssssssss
->sssssssssssss
array_of_stringsis the main pointer -> which we move by either method
vertically (is better to think of it this way). at the moment
we dereference *array_of_strings we then have another pointer the one that
points to the actual string, the second -> above.
so array_of_strings++ moves to the next string (vertical) and
(*array_of_strings)++ points to the next character in the string horizontal.
the first approach using offsets, in this approach we don't modify
the pointers, instead we use some offset value to point to the data
like *(pointer+5) = something. because pointers hold address we can so this
so pointer+5 points to the address of the byte which is 5 bytes from pointer
in array terminology as you will see in the array approach this is equivalent to pointer[5]. In increments terminology this is equivalent to
++pointer;++pointer;++pointer;++pointer;++pointer, incrementing the pointer 5
times.
the second approach is the best and easiest, using array indexes
array[i][j].
the third approach is the increments. here we need to modify the pointer
because we move the pointer forward or backward using the ++ and -- operators.
so p[1], *(p+1) and *++p are 3 ways to do the same thing
point pointer to one byte after pointer.
in the increment method you will see i used 2 pointers
array_of_strings and new_string they are both pointers but they
behave differently. array_of_strings is a pointer to a pointer
it points to a pointer to a byte (character), while new_string
points to the actual data the string. when we do array_of_strings++
we move array_of_strings to point to the next string.
Aa01234
*array_of_strings++ -> Bb01234567
and when we do *++new_string we point to the next character in the string
Aa01234
^
|
*++new_pointer
notice i use the increment operator before *++p not *p++ because i wanted
to increment p first and then dereference. if i had done *p++ it would process
Aa012345 or which ever string twice
bellow is the code, it has comments and I think is not hard to follow
just comment two of the #define and leave the one you want to experiment with
uncommented, then set break points and see how it works.
to run the code just create a new windows console application if using
Microsoft visual studio. if using some other tool then just copy paste
the code in your Main function
|
|
Home page | Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.3
Spotted an error? contact us
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.3
Spotted an error? contact us