Thread: Assigning value during pointer declaration

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    19

    Assigning value during pointer declaration

    Hi,

    I am trying to understand the behavior of following code. Basically how does printf() prints the value rather than address.
    Does initializing value to a pointer during declaration makes a difference when assigned from a variable?

    Code:
      1 #include <stdio.h>
      2 
      3 int main() {
      4     const char *var1 = 'A';
      5     int *vint = 10;
      6     
      7     printf("\n var1 = %c", var1);
      8     printf("\n vint = %d", vint);
      9     
     10     printf("\n addr = %p", vint);
     11  
     12 }
     13

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    There are no addresses involved here.

    Code:
    int *vint = 10;
    This creates a pointer-to-int type and sets the pointer to 10. It doesn't allocate a slot of memory for an int and put 10 in it. Being able to set pointer addresses directly is useful when you have intimate knowledge of the memory layout, hence it's legal.

    If you try to dereference those pointers, I expect your program will crash, as it'll try to access memory at 10 and 'A'.
    Last edited by smokeyangel; 08-31-2013 at 09:04 AM.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    19
    Thanks for the quick reply. If this is the case then we could do the following,

    Code:
    int vinit = 10
    Why do we need such code? I see such code being used by many folks in many places.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Yes, you can do that. If you really want to initialise a pointer, you can then do:
    Code:
     int * ptr = &vinit;
    Quote Originally Posted by pkumarn
    Why do we need such code? I see such code being used by many folks in many places.
    What code do you mean? Code like in your original post is definitely not commonplace, since it makes no sense on most systems.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Code you might see often is:

    Code:
     char * str = "lalalala";
    This will set str to the address of the lalala string. String literals are special in this regard. If you'd put your 'A' in doublequotes and left the rest of the code the same, you'd get a partial address printed from the first printf.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    19
    To dig a bit more. As you said memory is not allocated for int, then how accessing "vinit" in printf() prints 10. At first glance, vinit should be address and not value 10. Maybe i am missing something here.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by pkumarn View Post
    To dig a bit more. As you said memory is not allocated for int, then how accessing "vinit" in printf() prints 10. At first glance, vinit should be address and not value 10. Maybe i am missing something here.
    vint is address with value of 10

    When you convert it to int you get 10. But it does not mean that you can get any address - convert it to int and then assign this int to another address and get the same value. Address of int and int could have different bit-lengths. By doing this you will loose half of the address, for example.

    So such conversions as used in the code should be avoided in any real code.
    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

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Indeed -- this code has plenty of potential for disaster.

    GCC will have a good moan about it:
    Code:
    test.c: In function ‘main’:
    test.c:4:25: warning: initialization makes pointer from integer without a cast [enabled by default]
    test.c:5:18: warning: initialization makes pointer from integer without a cast [enabled by default]
    test.c:7:6: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘const char *’ [-Wformat]
    test.c:8:6: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat]
    It might be a bit clearer to you if I add some explicit casts (I think this is one time where I do prefer C++ -- it won't let you do these assignments without explicit casts.):

    Code:
         const char *var1 = (const char*)'A';
         int *vint = (int*)10;
    
        int value_of_vint = *vint; // !!!!
    The extra line there will try to go and read from address 10.

    Pointers don't automatically point to anything. If you hadn't initialised them they'd have garbage like any other local variable. To use them for anything you have to write an address to them, e.g.
    Code:
      int * a = malloc(sizeof(int)); // a = addr on heap
      int i;
      int *b = &i;                 // b = addr of i, on stack
      int *c = &glob;          // address of some global 
    
      unsigned int *d = (unsigned int*)0x80001000;   // someplace....
    You wouldn't hard code an address like that unless you knew what was there and knew the program would be able to access it (e.g. a memory mapped peripheral in an embedded system).

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    19
    Thanks folks. Things are clear now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning value to char pointer
    By Ducky in forum C Programming
    Replies: 3
    Last Post: 02-03-2013, 07:26 AM
  2. Assigning a pointer to a variable
    By rlesko in forum C Programming
    Replies: 7
    Last Post: 11-15-2010, 12:45 PM
  3. Assigning Array to Pointer?
    By Ditrik in forum C Programming
    Replies: 4
    Last Post: 01-10-2010, 08:04 AM
  4. Assigning a pointer a value -- Still = 0?
    By rjg32 in forum C Programming
    Replies: 1
    Last Post: 02-05-2009, 12:16 AM
  5. Assigning to Void Pointer
    By coder8137 in forum C Programming
    Replies: 7
    Last Post: 12-06-2006, 01:38 PM

Tags for this Thread