Thread: Two questions

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    Two questions

    Could anyone please help answering these questions. Thanks in advance.

    1. Write a statement declairing a pointer to a float and setting that pointer to the address of the variable myNum.



    2. If you add 1 to a pointer variable, how does the pointer variable chages? Please explain your answer.

  2. #2
    Unregistered
    Guest
    1)
    float myNum = 8, *pNum;

    pNum = &myNum;


    2) incrementing a pointer is the same as indexing an array.

    --Seb

  3. #3
    Unregistered
    Guest
    2) Same as incrementing an array index, that is...

    --S

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    Originally posted by Unregistered
    1)
    float myNum = 8, *pNum;

    pNum = &myNum;


    2) incrementing a pointer is the same as indexing an array.

    --Seb


    Thank you Mr. unregistered

  5. #5
    Registered User MisterBadger's Avatar
    Join Date
    Mar 2002
    Posts
    16

    Smile Here's my offering...

    Q1.
    [code]
    float myNum=25.52, *n_ptr;
    n_ptr=&myNum;
    printf("%f",*n_ptr); /*Value at address pointed at by n_ptr will be displayed on screen as 25.52*/
    [\code]

    Q2. Adding 1 to a pointer variable moves the pointer by the number of bytes of the "base type" which is 4 bytes for a float.
    In an array of float values, incrementing the pointer by one with:

    n_ptr++;

    will allow access to the float value at that memory location using indirection:

    printf("%f",*n_ptr);

    I see I'm not the first to reply! Pointers can be tricky to start with and they are at the heart of "useful" C programming...

    Cheers from MisterBadger.

  6. #6
    Registered User MisterBadger's Avatar
    Join Date
    Mar 2002
    Posts
    16

    Smile

    Yes, I got the code tags wrong... Sorry!

    MrB

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    12

    Thanks

    Thanks everyone, with your help I was able to solve my problem. thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  4. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM