Thread: Printing pointer to int in printf ( )

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    4

    Printing pointer to int in printf ( )

    Hello to All,
    I am new to programming & learning, I am trying( in wrong way)to print the content of pointer to int.

    what i have done, i have assigned pnum the starting address of const int 345;( which goes with warning in gcc compiler & need (int*) cast in VSexpress edition.

    later on I pass pnum to C-legendary printf(), strangely it prints the number 345 , instead of address.

    i have consulted many on-line docs & K&R C book , but could not understand what is happening beneath.

    Code:
    main() {
     int *pnum;
     pnum = 345; // Assign pnum start address of const int 345
    
    printf("\n value stored in pnum %d " , pnum); // it prints 345 instead of address.
    
    // printf("\n Value stored in *pnum %d" ,*pnum); uncomment to crash the program.
    
    return 0;
    }
    Theoretically it should print the address not content. forum members are requested to enlighten new learner like me

    Thanks
    Adi

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you try:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int num = 345;
        int *pnum;
        pnum = &num;
        printf("value stored in pnum: %p\n", (void*)pnum);
        printf("value stored at what pnum points to: %d\n", *pnum);
        return 0;
    }
    Observe that I:
    • Included the relevant header.
    • Declared main as returning an int, and explicitly stated that it takes no arguments with void as the parameter list.
    • Indented the code properly.
    • Declared an int with a value of 345.
    • Caused pnum to point to that int.
    • Printed the value of pnum (i.e., an address) with %p, casting pnum to void* to match.
    • Printed the value of what pnum points to (i.e., an int) with %d.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    4
    Thank you ,laserlight for help,I wish i had been more expressive in explaining the problem initially.In plain English I am trying to assign the address of integer literal 345.In fact I am trying to do something on the line, like pnum = &(345); // which is illegal.However 345 is stored (either on stack or on heap ,C implementation is silent where literals are stored ) in memory.However statement like pnum = 345; (with implicit conversion) results in storing of starting address of location where 345 is stored.So technically speaking after this assignment ,content of pnum should be starting location of 345. similar on the line of char *ps = "My string";in fact statement like *pnum =345; also result in access violation.Documentation for printf() states , that function prints the value of argument.From the result of printf(), it looks that we have assigned 345 to pnum ( after appropriate casting), not the address of 345 literal , I dont know what is happening actually.experienced programmer like you can definetly throw light on subject.thanks Adi

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by adi_K
    In plain English I am trying to assign the address of integer literal 345.
    This is a fool's errand.

    Quote Originally Posted by adi_K
    However 345 is stored (either on stack or on heap ,C implementation is silent where literals are stored ) in memory.
    Chances are the literal 345 would be stored neither on the stack nor the heap.

    Quote Originally Posted by adi_K
    However statement like pnum = 345; (with implicit conversion) results in storing of starting address of location where 345 is stored.
    No, it does not, except by rare coincidence.

    Quote Originally Posted by adi_K
    So technically speaking after this assignment ,content of pnum should be starting location of 345. similar on the line of char *ps = "My string"
    No, you are on the wrong track. "My string" is a string literal, of type char[10]. As an array, in most contexts, it is converted to a pointer to its first element. Therefore, "My string" is converted from char[10] to char*, so you can use it to initialise a char*. This does not happen for 345. 345 it is an integer literal, and not being an array, it is not converted to a pointer. You forcibly converted it to a pointer, but that does not mean that you have a pointer that points to the integer literal 345; it means that you have a pointer with a value that is probably invalid, e.g., it is an address that your program is not permitted to access.

    Quote Originally Posted by adi_K
    in fact statement like *pnum =345; also result in access violation.
    What does pnum point to? You cannot just dereference a pointer that does not point to a valid object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2016
    Posts
    4

    Thank you

    thank ou

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    A constant like 345 in your code is (most likely) stored in the code segment as a direct operand of a machine language instruction. Although it would in fact have an address, it is not easy to determine in C.

    But having the address of something usually means you want to change it. If you want to change it, then why is it a constant? If you want to change it, do what laserlight did and store it at an address you can easily get at.

  7. #7
    Registered User
    Join Date
    Jan 2016
    Posts
    4
    But having the address of something usually means you want to change it. If you want to change it, then why is it a constant? If you want to change it, do what laserlight did and store it at an address you can easily get at.
    Yeah , correct, i am just learning & clearing the doubts in fundamentals as I have many.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printf printing twice
    By travis9090 in forum C Programming
    Replies: 4
    Last Post: 11-17-2015, 02:23 AM
  2. printf printing twice?
    By drshmoo in forum C Programming
    Replies: 5
    Last Post: 03-19-2011, 02:41 AM
  3. Printf not printing all data
    By murjax in forum C Programming
    Replies: 24
    Last Post: 07-20-2009, 09:33 PM
  4. printf not printing
    By deadhippo in forum C Programming
    Replies: 5
    Last Post: 05-04-2008, 12:36 AM
  5. Printf float printing
    By judgex in forum C Programming
    Replies: 3
    Last Post: 12-08-2007, 02:47 AM

Tags for this Thread