C++ Pointers - Explain Concept In Detail

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<So now if we do:
ptr=ptr+1;
cout<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.

Replies

  • Deepika Bansal
    Deepika Bansal
    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;
  • PraveenKumar Purushothaman
    PraveenKumar Purushothaman
    Hey, great article here! Can you explain about pointers to an array and array to pointer? We often get confused in it!!! ๐Ÿ˜”
  • Deepika Bansal
    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.
  • PraveenKumar Purushothaman
    PraveenKumar Purushothaman
    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... ๐Ÿ˜€
  • Deepika Bansal
    Deepika Bansal
    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.
  • PraveenKumar Purushothaman
    PraveenKumar Purushothaman
    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! ๐Ÿ˜€
  • slashfear
    slashfear
    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
  • PraveenKumar Purushothaman
    PraveenKumar Purushothaman
    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?
  • Deepika Bansal
    Deepika Bansal
    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.
  • Deepika Bansal
    Deepika Bansal
    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.
  • silverscorpion
    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.

    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.. ๐Ÿ˜€๐Ÿ˜€
  • Deepika Bansal
    Deepika Bansal
    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.
  • slashfear
    slashfear
    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
  • Deepika Bansal
    Deepika Bansal
    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.๐Ÿ˜€
  • silverscorpion
    silverscorpion
    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?
  • Deepika Bansal
    Deepika Bansal
    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++. ๐Ÿ˜€
  • Manish Goyal
    Manish Goyal
    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 ๐Ÿ˜‰
  • Deepika Bansal
    Deepika Bansal
    Hey manish do we have some way to keep track of these issues and prevent them as well..?

You are reading an archived discussion.

Related Posts

This page made to add a new ideas in any specialization in the life or the science , and to add solutions for problems , personal experiences in your personal...
Plz provide me any seminar topic IEEE standard on any CSE Topic ! Thanks!!!
Apple recently launched iPad 2 on March 2. One of the most remarkable features in iPad 2 is presence of Full HD 1080p video recording through rear camera. It also...
now a days carburretors are not used in the vehicle and i jst want to know that which unit used in place of carburretors and how does it functions..??
Opening and Closing Files Files are opened in PHP using the fopen command. The command takes two parameters, the file to be opened, and the mode in which to open...