Thread: Doubt in pointer.

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Doubt in pointer.

    Hello All,
    I have small doubt in pointer, doubt as follows:-

    Code:
    int a;
    int *ptr;
    ptr = &a;
    printf("%d", &a);
    printf("%d", ptr);
    i have check in my VC++ compiler output is 1245052 1245052.

    Now if i modify above code as follows:

    Code:
    int a;
    int *ptr;
    ptr = 1245052;
    printf("%d", ptr);
    Above code can't compile why?even though i am applying valide address of a.

    Thanks in advance
    Shwetha

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. to print address you should use

    printf("%p", (void*)ptr);

    2. your integer representation of the address could be not accurate
    3. address value could change - so it not wise to set it hardcoded
    4. if you really need it - you need a cast to the correct type
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    ptr = 1245052;
    in this regard i've also a doubt can we assign any constant value to a pointer.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by BEN10 View Post
    in this regard i've also a doubt can we assign any constant value to a pointer.
    of course we can, pointer is just variable

    problems begin when you try to dereference such address in case your constant chooses some value not availble to the program
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    It is not possible to assign following way.

    Quote Originally Posted by BEN10 View Post
    in this regard i've also a doubt can we assign any constant value to a pointer.

    No it is not possible to assign constant value to pointer.

    Code:
    int *ptr = 10   Will not work. since 10 is not associated with address.
    pointer is not just variable it is variable it can only hold address of another variable.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by shwetha_siddu View Post
    No it is not possible to assign constant value to pointer.

    Code:
    int *ptr = 10   Will not work. since 10 is not associated with address.
    pointer is not just variable it is variable it can only hold address of another variable.
    you clearly do not know what you are talking about

    Code:
    int main()
    {
    	int *p = (int*)10;
    	return 0;
    }
    works fine
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM