Thread: question regarding pointers

  1. #1
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54

    question regarding pointers

    A very naive question;

    if declare a pointer
    int *x.

    then a int
    int y.

    now x points to an address.
    and so do &y.

    so can I say that x and &y are same type of containers?

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Well, they are both variables that are represented by a finite series of bits. Other than that, pointer types and integer types conceptually are completely different. I'm not sure what you mean by "containers."

    When you refer to "x" that is basically the same thing as referring to "&y" assuming that you've coded x = &y at some point prior.

  3. #3
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    To make things clear. If there is a function with a syntax like:

    function( char *x )

    would it be same as

    function( char y )


    or function( &y ) be same as function( x ) ?

  4. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Quote Originally Posted by jas_atwal View Post
    To make things clear. If there is a function with a syntax like:

    function( char *x )

    would it be same as

    function( char y )


    or function( &y ) be same as function( x ) ?
    Yes, &y and x are the same type. Is that what you meant by container?

    The prototype would be function(char *x) but the actual call would look like function(&y).

    Quick example:

    Code:
    #include <stdio.h>
    
    /* function expecting a pointer-to-integer */
    void foo(int *x)
    {
         printf("&#37;d\n", *x);
    }
    
    int main()
    {
        int *ptr;
        int num = 100;
        ptr = &num;
        foo(&num);
        foo(ptr);
    }

  5. #5
    Dragon Rider jas_atwal's Avatar
    Join Date
    Nov 2007
    Location
    India
    Posts
    54
    Gotcha!! Thank you so much MacNilly !!!

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by jas_atwal View Post
    so can I say that x and &y are same type of containers?
    Yes. They are both the same type. But one is an l-value, the other is not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM