Thread: Regarding Pointer

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    26

    Regarding Pointer

    Hi Friends,

    Can we initialize a non-zero value to a pointer variable?

    Ex : int *p=10;


    Thanks in Advance!!!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Sort of. You can do that, and it will make the pointer point to the memory address 10, which you don't own; thereafter, any attempt to use the pointer will result in disaster (unless you first make it point to somewhere "real").

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    26

    Regarding Pointer again!!!!

    Thanks Friend!!!

    Also I want to know 2 more things..

    Can i declare like this:

    1) int *p = 0; // I guess p here would be a NULL pointer.

    2) int *p, q;
    p = &q;
    p=4; // What will p contain?Is it a valid statement?

    Thanks in Advance!!!!

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by tabstop View Post
    Sort of. You can do that, and it will make the pointer point to the memory address 10
    Sorry, but no. You can't implicitly convert int to int*. You have to make an explicit conversion:
    Code:
    int* p = (int*)10;
    
    or
    
    int* p = reinterpret_cast<int*>(10);
    but you can do:

    Code:
    int* p = new int(10);
    But this is a different matter.

    0 (zero) is the only literal constant allowed to be assigned to a pointer type.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    p=4; // What will p contain?Is it a valid statement?
    Again you are trying to assign an integer to a pointer. p is a pointer, q is an integer (but avoid declaring a mixture of pointers and other types on a single line), so the first two lines are fine.

    What you can do is:

    Code:
    *p = 4;
    This means, set the value of the int that p points to (in this case q) equal to 4.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM