CrazyEngineers
  • C++ Pointers - Explain Concept In Detail

    Updated: Oct 22, 2024
    Views: 1.0K
    C++ pointers are the address variables that are used to access some memory location.

    Declaration: int *p;
    Here 'p' is an identifier that is used to store some address. The content of this memory address will of type 'int' as we mentioned in the declaration. We use any other data type also as per the type of content to be stored.
    So the content of the p will be integer type not p itself.

    Lets take an example.
    int a=5;
    int *ptr;
    ptr=&a;         //ptr now refers to the address of 'a'
    cout<<ptr;    //it'll print the address of 'a' in hexadecimal code
    cout<<*ptr;  //it'll print the contents of a ie value of the variable a.
    
    So now if we do:
    ptr=ptr+1;
    cout<<ptr;
    
    The result is that the pointer variable 'ptr' will be incremented as:
    ptr=ptr+(1*size of int)
    ie initally if ptr was refering to the memory address say 1000, now it'll refer to the memory address 1002 because size of int is 2 bytes. Here we have added the size of int because our ptr stores the integer value.
    Had it stored a float or some other value, we would had used the size of float or the appropriate data type instead.

    So now we come to know that we can increment or decrement address value using pointers. But we can increment, decrement or even multiply pointers only with integer values no matter what type of data is stored in it.
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Deepika Bansal

    MemberMar 6, 2011

    Initialization: When the pointer has not been initialized with some value, it refers to some arbitrary memory address (garbage value for the pointer) which in turn contains some random value (again a garbage value at that address) .

    To initialize the pointers with some value, the syntax is:
    int ptr;    //declaration
    ptr=new int;   //memory allocation to the pointer
    
    The 'new' operator will allocate some free memory from the memory pool. This operator is similar to the malloc operator of C.

    Similarly to de-allocate this memory space to the ptr, we use delete operator as:
    delete ptr;
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberMar 6, 2011

    Hey, great article here! Can you explain about pointers to an array and array to pointer? We often get confused in it!!! 😔
    Are you sure? This action cannot be undone.
    Cancel
  • Deepika Bansal

    MemberMar 6, 2011

    I request other CEans to please correct (if required) and contribute to this thread.
    I guess this is one of the least understood topic of C++ programming.
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberMar 6, 2011

    Deepika Bansal
    I request other CEans to please correct (if required) and contribute to this thread.
    I guess this is one of the least understood topic of C++ programming.
    Oki, am ready to help it out in this case... 😀
    Are you sure? This action cannot be undone.
    Cancel
  • Deepika Bansal

    MemberMar 6, 2011

    praveenscience
    Hey, great article here! Can you explain about pointers to an array and array to pointer? We often get confused in it!!! 😔
    Fine. I'll start about that also.
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberMar 6, 2011

    Pointers are special variables that store addresses.
    Consider a variable a and if it is assigned to a pointer p, then p contains the address of a.
    To access the value inside p, we need to prefix p with the * symbol.
    To access the address of a, we need to prefix a with the & symbol.

    Coming to the example,
    Consider, we store a = 5; and the memory location of a is at 0x8858 in RAM.
    Now, the code is like this. p = &a; p becomes a pointer now.
    The value inside p will be 0x8858, which is a hex address, better as unsigned address.
    But, the variable p itself will have an address of its own, like 0x9852, which should be greater than address of a.
    Now, if you put anything in a, it will be in *p.
    Whatever is in &a is same as p!

    I hope this clears it! 😛 Just a try! 😀
    Are you sure? This action cannot be undone.
    Cancel
  • slashfear

    MemberMar 7, 2011

    Deepika Bansal
    So now we come to know that we can increment or decrement address value using pointers. But we can increment, decrement or even multiply pointers only with integer values no matter what type of data is stored in it.
    Hey Deepika,

    Nice thread !! 😉 But I have a small question..... you stated that we can increment or decrement or even multiply pointers!! But I think we can multiply the address of pointers since C++ supports hex multiplication but we don't use the concept any where in programming because when we multiply the address of two pointers the resulting address is going to be random and will be a junk value or the resulting address might be out of the boundary.

    The place where increment and decrement of pointers are used is when your pointer points to an array so you can easily navigate through the element stored in an array by incrementing and decrementing it. Since arrays use consecutive memory location to save the elements in an array.


    Thought of putting my views forward about pointers.... 😉


    -Arvind
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberMar 7, 2011

    Deepika Bansal
    ptr=ptr+(1*size of int)
    Are you saying about the sizeof operator? or just you are asking us to substitute the value?
    Are you sure? This action cannot be undone.
    Cancel
  • Deepika Bansal

    MemberMar 11, 2011

    slashfear
    Hey Deepika,

    Nice thread !! 😉 But I have a small question..... you stated that we can increment or decrement or even multiply pointers!! But I think we can multiply the address of pointers since C++ supports hex multiplication but we don't use the concept any where in programming because when we multiply the address of two pointers the resulting address is going to be random and will be a junk value or the resulting address might be out of the boundary.

    The place where increment and decrement of pointers are used is when your pointer points to an array so you can easily navigate through the element stored in an array by incrementing and decrementing it. Since arrays use consecutive memory location to save the elements in an array.


    Thought of putting my views forward about pointers.... 😉


    -Arvind
    Hey Arvind. I myself have not use multiplication ever (never felt the need of). But when we increment or decrement the pointer value, we use an integer value for it. I guess the same way multiplication is used to move the address value to 2 times or 3 times, say.
    Are you sure? This action cannot be undone.
    Cancel
  • Deepika Bansal

    MemberMar 11, 2011

    praveenscience
    Are you saying about the sizeof operator? or just you are asking us to substitute the value?
    Praveen I'm asking to substitute the value.
    Are you sure? This action cannot be undone.
    Cancel
  • silverscorpion

    MemberMar 11, 2011

    @Deepika: I dont think you can multiply two pointer variables.
    Incrementing a pointer by 2 or more by multiplying 2 with the pointer is not pointer multiplication, because you're multiplying a pointer with a constant.

    And now to array of pointers and pointer to arrays...

    int *a[10] =======>> Array of pointers containing 10 pointer variables
    int (*a)[10] ======>> Pointer to an array of 10 integers

    This is the syntax to use an array of pointers and a pointer to an array..

    Array of pointers:

    It's nothing more than an array of pointer variables. Just like you declare an array of integer variables or array of characters, you define array of pointers. The pointers themselves can be of any type. ie., you can have an array of integer pointers or an array of character pointers etc..

    There's nothing really different between normal arrays and array of pointers. All operations performed on array elements can be performed here too. Just that, all elements would be holding address of other variables as their values..


    Pointer to an array:

    The syntax for declaring a pointer to an array is\

    int (*a)[10];

    Here, you specify that the a is a pointer variable which points to an integer array of size 10. What this means is that, when you increment this pointer variable, it will increment 10 integer sizes, ie., 20 bytes.

    For example, if an array's starting address is 1000 and you store it in a pointer to this array.

    int a[10];
    int (*b)[10];
    b=&a;


    Now b will contain the value 1000. If you increment b now, its new value will be 1020.
    Because, it's a pointer to an integer array, and the size of the array is 10, it'll increment 20 bytes, which is the size of 10 integers.

    Hope it's clear. That's all for now. Doubts are welcome.. 😀😀
    Are you sure? This action cannot be undone.
    Cancel
  • Deepika Bansal

    MemberMar 11, 2011

    silverscorpion
    @Deepika: I dont think you can multiply two pointer variables.
    Incrementing a pointer by 2 or more by multiplying 2 with the pointer is not pointer multiplication, because you're multiplying a pointer with a constant.
    I think I'm not able to clarify my point. SS I'm also saying that we can multiply a pointer variable with an integer value to increment the pointer value. We can NEVER multiply two pointer variables. Hope this works.
    Are you sure? This action cannot be undone.
    Cancel
  • slashfear

    MemberMar 15, 2011

    Deepika Bansal
    I think I'm not able to clarify my point. SS I'm also saying that we can multiply a pointer variable with an integer value to increment the pointer value. We can NEVER multiply two pointer variables. Hope this works.
    Hey deepika,

    That's What I said in the first place 😉

    -Arvind
    Are you sure? This action cannot be undone.
    Cancel
  • Deepika Bansal

    MemberMar 25, 2011

    Yeah Arvind. You were right. 😀

    I request others to please come up with your doubts and queries so that we together can learn more and more about pointers.😀
    Are you sure? This action cannot be undone.
    Cancel
  • silverscorpion

    MemberMar 25, 2011

    Hmm.. There is one doubt!

    In real world applications where pointers are used, how is the safety of the program ensured? A pointer is used to access memory locations.
    So, an incorrectly or carelessly coded program may try to access memory locations which are dangerous when accessed by programs other than the OS.
    The program may crash, or in the worst case, even the system can crash.

    So, how is it ensured that such things wont happen?
    Are you sure? This action cannot be undone.
    Cancel
  • Deepika Bansal

    MemberMar 26, 2011

    Yes SS. This is one drawback associated with the pointers. Because of the same reason, it is said that it is easy to code a virus in C++. 😔
    In fact, in C++ more security issues arise when we access the array beyond its size.

    I don't know how the security is ensured in this case.😒

    Looking at these issues, java eliminated both the concepts of C++. 😀
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberMar 26, 2011

    C++ more security issues arise when we access the array beyond its size.
    Just for information these security issues are related with buffer overflow attack 😉
    Are you sure? This action cannot be undone.
    Cancel
  • Deepika Bansal

    MemberMar 26, 2011

    Hey manish do we have some way to keep track of these issues and prevent them as well..?
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register