Doubt in pointer.

This is a discussion on Doubt in pointer. within the C Programming forums, part of the General Programming Boards category; Hello All, I have small doubt in pointer, doubt as follows:- Code: int a; int *ptr; ptr = &a; printf("%d", ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 11:16 AM
  2. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21