About pointer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sandeep Patil
    New Member
    • Jun 2007
    • 1

    About pointer

    Hello,
    I am Sandeep Patil. I have one question about pointer in c and also c++.
    What is function of pointer? it is address operator. We use pointer to store address of variable to refer that variable. Then why we use char pointer, int pointer as a different pointers. Why should we not use one type of pointer to refer all types of variable?
  • svlsr2000
    Recognized Expert New Member
    • Feb 2007
    • 181

    #2
    Originally posted by Sandeep Patil
    Hello,
    I am Sandeep Patil. I have one question about pointer in c and also c++.
    What is function of pointer? it is address operator. We use pointer to store address of variable to refer that variable. Then why we use char pointer, int pointer as a different pointers. Why should we not use one type of pointer to refer all types of variable?
    Pointer types ie character or integer is to indicate what they are pointing to.
    for example u can store a character in space allocated to integer

    int * p = new int;
    *p = (int)'c';

    But according to compiler still *p contains int and not character.
    I think this answered your question

    Comment

    • DeMan
      Top Contributor
      • Nov 2006
      • 1799

      #3
      The pointer is still the same irrespective.
      The difference between a character pointer and an integer pointer, is that a character pointer treats the VALUE where it is pointing as a character, while an integer pointer treats the VALUE as an integer.....
      :
      Code:
      int *int_pointer;
      char *char_pointer;
      
      *int_pointer = 0xc0ff;  /* Set the value at the integer pointer to be larger than a character */
      char_pointer = (char*)int_pointer;
      What is the difference (in this context) between *int_pointer and *char_pointer ?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by Sandeep Patil
        Hello,
        I am Sandeep Patil. I have one question about pointer in c and also c++.
        What is function of pointer? it is address operator. We use pointer to store address of variable to refer that variable. Then why we use char pointer, int pointer as a different pointers. Why should we not use one type of pointer to refer all types of variable?
        I would like to talk about why you can't have one kind of pointer for all variables. The answer has to do with address calculations. If you have pointer, ptr:
        [code=c]
        int* ptr;
        int array[5];
        ptr = array;
        [/code]

        Here the address of element 0 (an int) is assigned to the int pointer ptr.

        ptr = ptr + 2 tells the compiler to calculate an address that adds 2 times the sizeof an int to the address in ptr.

        1000 array[0]
        1004 array[1]
        1008 array[2]
        1012 array[3]
        1016 array[4]

        So the new address is ptr = 1000 + 2 *4

        The sizeof(int) is 4.

        That is, ptr now contains 1008 which is the address of array[2].

        The compiler knows to use sizeof(int) because the pointer is a pointer to an int. If the compiler were to use any other value, like sizeof(char) or sizeof(double), then the calculated address for the array element will be wrong.

        That is why the compiler is so picky about how you use your pointers.

        Comment

        • adla sanober
          New Member
          • Jul 2007
          • 1

          #5
          Originally posted by svlsr2000
          Pointer types ie character or integer is to indicate what they are pointing to.
          for example u can store a character in space allocated to integer

          int * p = new int;
          *p = (int)'c';

          But according to compiler still *p contains int and not character.
          I think this answered your question
          can function return more than one value.

          Comment

          • Meetee
            Recognized Expert Contributor
            • Dec 2006
            • 928

            #6
            Originally posted by adla sanober
            can function return more than one value.
            You can search for your answer here


            You will have to use pointers for that

            Regards

            Comment

            Working...