Thread: Pointer initialization

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Pointer initialization

    Hello all,

    I need to initialize a pointer variable with a knowing address. Please see code below, ptr is the final destination and value of ptr_address contains the address value, so I need to do something like ptr = *ptr_address.

    Code:
    int *ptr;
    int *ptr_address;
    int address;
    
    address = 0x10000005;
    ptr_address = &(address);
    ptr = *ptr_address;
    The problem is that compiler gives the following warning message:

    warning: assignment makes pointer from integer without a cast [enabled by default]


    Is my code wrong or there is any other way to do it without receiving this compiler warning?

    Thanks.

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The value can be cast to be a pointer to integer:

    ptr = (int *)0x10000005;
    Last edited by rcgldr; 12-28-2013 at 08:44 AM.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Hi rcgldr, the problem is that i can't use direct value because address comes as a variable value, so I need use this variable.

    I've already tried ptr = *((int *) ptr_address); but warning is the same.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    ptr = (int *)*ptr_address

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does static initialization of pointer make it null pointer
    By Saurabh Mehta in forum C Programming
    Replies: 1
    Last Post: 11-23-2012, 12:05 AM
  2. Pointer Initialization
    By johan.g1 in forum C Programming
    Replies: 6
    Last Post: 11-14-2012, 09:08 PM
  3. Struct pointer initialization
    By pe4enka in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2010, 05:16 AM
  4. Pointer Initialization Within A Struct
    By SMurf in forum C Programming
    Replies: 15
    Last Post: 02-09-2009, 11:27 AM
  5. initialization of double pointer with new
    By GaPe in forum C++ Programming
    Replies: 8
    Last Post: 01-19-2005, 11:09 AM