Thread: Just starting and going through the sites 'c tutorial'

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    6

    Just starting and going through the sites 'c tutorial'

    Hi:
    I am very new to programming, and excited to learn C. I am starting the 'C tutorial' and I already have some questions after the first lesson.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int this_is_a_number;
    
        printf( "Please enter a number: " );
        scanf( "%d", &this_is_a_number );
        printf( "You entered %d", this_is_a_number );
        getchar();
        return 0;
    }
    So when you use the '&' in the above example - the memory position/place is getting referenced, which holds the data and not the actual number 4?

    When I do the following

    Code:
    printf("memory %p\n",this_is_a_number)
    I receive 00000003

    but when I do

    Code:
    printf("memory %p\n",&this_is_a_number);
    I get the memory position.

    Where or what is storying '00000003'? scanf required me to reference the pointer to

    thank you

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by gradius85
    So when you use the '&' in the above example - the memory position/place is getting referenced, which holds the data and not the actual number 4?
    Yes. We normally call the "memory position/place" the address, hence the & used in this context is called the address-of operator.

    As for the "00000003": that probably means that you entered 3 as the input. If you entered 4 instead, well, I don't know why you would get that output, but I'm not too bothered because you aren't supposed to be printing an int with the %p format specifier in the first place (the behaviour is undefined).
    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
    Dec 2017
    Posts
    1,626
    Code:
    #include <stdio.h>
    int main()
    {
        int n;
        printf("Enter an integer: ");
        scanf("%d", &n);
        printf("The value %d is stored at address %p\n", n, &n);
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    6
    Quote Originally Posted by laserlight View Post
    int with the %p format specifier in the first place (the behaviour is undefined).
    Thank you for your help...
    When you say 'behavior' is undefined what do you mean by that, did I do something wrong in my test app? I did not see any 'warnings' or 'errors' and I used 'gcc'.

    thank you again.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    6
    Quote Originally Posted by john.c View Post
    Code:
    #include <stdio.h>
    int main()
    {
        int n;
        printf("Enter an integer: ");
        scanf("%d", &n);
        printf("The value %d is stored at address %p\n", n, &n);
        return 0;
    }
    Thank you for your help.
    Unfortunately I am still confused. '&n' is a memory pointer? Which the content as been saved as a digit variable.

    Thank you

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Unless the compiler documents what will actually happen in the particular case of undefined behaviour and you're intending that, yes, undefined behaviour means a bug since the compiler could do "anything", from simply refusing to compile to compiling with a result that you didn't expect, and the compiler doesn't have to warn you.

    That said, if you compile at a high warning level (e.g., try -Wall), the compiler might warn you.

    Quote Originally Posted by gradius85
    '&n' is a memory pointer? Which the content as been saved as a digit variable.
    n is an integer variable of type int. &n is the address of that integer variable, i.e., &n is a pointer of type pointer to int (or in C, we write it as int*)
    Last edited by laserlight; 02-14-2019 at 01:41 PM.
    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

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    6
    I think part of my problem is that '%p' is formatting for pointer addresses. However, I did not supply a pointer address. Is it correct to say that "&" reads memory to create what you preface with '&' a memory pointer?

    So, since I did not specify a pointer, the compile just used what was stored in the variable and formatted it for base-16 address?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by gradius85
    However, I did not supply a pointer address. Is it correct to say that "&" reads memory to create what you preface with '&' a memory pointer?
    It is redundant to say "pointer address" and "memory pointer". Just say "address" or "pointer". An address denotes a location in memory; a pointer is an address with type information so you can interpret what is at the address, e.g., a pointer to int points to an int object whereas a pointer to float points to a float object, even if at different times they happen to have been in the same location in memory. As for what & means, refer to my post #2.

    Quote Originally Posted by gradius85
    So, since I did not specify a pointer, the compile just used what was stored in the variable and formatted it for base-16 address?
    That's probably what printf did, yes. The compiler compiles your code such that it calls printf; the compiler doesn't do that formatting itself.
    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

  9. #9
    Registered User
    Join Date
    Feb 2019
    Posts
    6
    Thank you 'laserlight'. I will try to use the correct words and concepts. I have read the book list on the site, and I am doing the tutorial. I want to learn more about C. However, I am unsure of the books I should be looking at. Do you know of any C book that will example things slowly from a beginner level (middle school or maybe high school)?

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    When you use the '&' before a variable name, say "The address of"
    Code:
    int banana;
    
    SomeFunction(&banana); // Address of banana
    
    SomeOtherFunction(banana); // banana
    If you have used a spread-sheet before (like excel), it might be useful to think of it like this...

    Imagine you type 4 into the cell A1

    In the analogy of the spread-sheet the address is A1, and the value is 4.


    For more, read this Pointers in C - Tutorial - Cprogramming.com
    Fact - Beethoven wrote his first symphony in C

  11. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Fact - Beethoven wrote his first symphony in C

  12. #12
    Registered User
    Join Date
    Feb 2019
    Posts
    6
    Quote Originally Posted by Click_here View Post
    When you use the '&' before a variable name, say "The address of"
    Code:
    int banana;
    
    SomeFunction(&banana); // Address of banana
    
    
    
    Question - When changing values, I want ot 
    SomeOtherFunction(banana); // banana
    If you have used a spread-sheet before (like excel), it might be useful to think of it like this...

    Imagine you type 4 into the cell A1

    In the analogy of the spread-sheet the address is A1, and the value is 4.


    For more, read this Pointers in C - Tutorial - Cprogramming.com
    This does help, since my teacher has me learning Excel as well. You but a value into a cell, but reference the sheet Address. This helps me a great deal to articulate in my mind what is what.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-03-2013, 08:50 AM
  2. sites that do c++ contests?
    By InvariantLoop in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2006, 12:54 AM
  3. DirectDraw Programming tutorial sites
    By Kaminaga in forum Game Programming
    Replies: 5
    Last Post: 10-14-2005, 09:55 AM
  4. anyone who knows these sites ?
    By black in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 05-21-2003, 08:44 PM
  5. job sites
    By dP munky in forum Game Programming
    Replies: 3
    Last Post: 11-08-2002, 12:39 PM

Tags for this Thread