pointers... need reassurance I'm understanding things correctly

This is a discussion on pointers... need reassurance I'm understanding things correctly within the C Programming forums, part of the General Programming Boards category; Hello guys, I've been working on a tutorial this morning and I believe I'm on the right track, I'm just ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    2

    pointers... need reassurance I'm understanding things correctly

    Hello guys, I've been working on a tutorial this morning and I believe I'm on the right track, I'm just looking for a 'yay or nay' as to if this makes sense or not.

    here's my code...

    Code:
    #include <stdio.h>
    
    
    char mem[20];        // assume mem is at base address 2000
    int x;
    int val1 = 0       ;
    int val2 = 1       ;
    char *ptr;
    
    
    int main (void) {
    ptr = &mem[val1];    // get the address of array element val1 within mem
    
    
     printf ("base address (which can be considered as 2000) %d\n", mem); //my addition...prints initial base address
     
    for (x = 0; x < 5; x++) {
        *ptr = 'A' + x + val2;
         printf ("address %d\n", ptr);  //my addition...this prints the address of the element you're currently at
         printf ("value %d\n", *ptr); //my addition...this prints what is contained inside that element
         ptr++;
    }
    getchar ();  //my addition...window won't dissappear until hitting 'enter'
    return 0;
    }
    I have added all the printf's in order to see what's going on.

    the output I receive when executing is as follows:

    Code:
    base address (which can be considered as 2000) 4202784
    address 4202784
    value 66
    address 4202785
    value 67
    address 4202786
    value 68
    address 4202787
    value 69
    address 4202788
    value 70
    I am being asked to state the 5 addresses that will be written to, and the value put into memory at that location.

    assuming I've used 'printf' in the correct fashion,

    can I then assume that my 5 addresses and values are:
    2000 (value 66)
    2001 (value 67)
    2002 (value 68)
    2003 (value 69)
    2004 (value 70)

    ...(being that I can't set the base address, I let windows choose a different number to represent 2000)


    Thanks in advance,

    Just need to to verify what I'm doing makes sense!

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    333
    You're trying to set a pointer's value directly, which isn't allowed (except for the special case of 0 or NULL)
    ptr contains the address of a variable which holds a char.

    so
    Code:
    char x = 'A'; /* x is the variable, it holds the character 'A' */
    char *ptr; /* a pointer to it */
    
    ptr = &x; /* make ptr point to x, it holds the address of x */
    printf("address %p value %c\n", ptr, *ptr); /* print out ptr, and what it points to */
    *ptr = 'F'; /* set what ptr point to to 'F' */
    printf(""x now holds %c\n", x); /* the variable x has been updated, through ptr */
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    My guess is that in your second printf you should use: "value %c\n" so that your output looks like 'B', 'C', 'D', 'E', 'F'. The rest of the program is fine.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,385
    A pointer is printed using %p in the printf family


    You can get the address of the array by using &mem[0] (the address of the first element), or you are allowed to use mem on it's own (which gets assesed as &mem[0])


    I would prefer to do both printf's at the same time, like Malcolm suggested. Also, I agree with nonoob, in that you probably wanted to see the characters, and not the number - Or better yet, you can see both


    Code:
    
        char apple[20];
        int banana;
        int i;
    
    
        char *pear;
    
    
        pear = &apple[0];
    
    
        printf ("base address (which can be considered as 2000) %p\n", apple);
    
    
        for (banana = 0, i = 0; i < 5; i++, pear++)
        {
            *pear = 'A' + i + banana;
    
    
            printf("\n\raddress %p \n\rCharacter '%c' \n\rASCII value %d \n", pear, *pear, *pear);
    
    
        }
    
    
        ...
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,307
    Quote Originally Posted by Malcolm McLean View Post
    You're trying to set a pointer's value directly, which isn't allowed (except for the special case of 0 or NULL)
    That is not true.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    2
    Quote Originally Posted by grumpy View Post
    That is not true.
    I thought so... I was confused by what he said, as my code worked perfectly.



    ...Thanks everyone for the clarification! Your responses were all very helpful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 11-04-2012, 01:13 AM
  2. Am I using pointers correctly here?
    By kisiellll in forum C Programming
    Replies: 4
    Last Post: 03-22-2009, 11:28 AM
  3. Pointers are not working correctly
    By adrian_fpd in forum C Programming
    Replies: 8
    Last Post: 11-17-2008, 06:55 PM
  4. Need help understanding how to store things in C++
    By DemonSpeeding in forum C++ Programming
    Replies: 3
    Last Post: 11-30-2006, 01:16 PM
  5. Am I understanding this C code correctly ???
    By AntonsonDJ in forum C Programming
    Replies: 3
    Last Post: 05-09-2005, 07:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21