Thread: Issue with pointer

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    11

    Issue with pointer

    I am still learning the C programming language and trying out various
    things with pointers, so please bare with me.

    I have written a program which allows me to view the memory contents
    using a pointer, I first started with this program.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define  MAX_CHARS  4
    
    typedef unsigned char BYTE;
    
    void dump(BYTE *ptr);
    
    
    int main(void) {
    
        int offset = 0;
        BYTE *ptr = NULL;
        char *memory = NULL;
    
        memory = (char *)malloc(sizeof(char) * MAX_CHARS);
    
        *(memory+0) = 'T';
        *(memory+1) = 'e';
        *(memory+2) = 's';
        *(memory+3) = 't';
    
        for (offset=0; offset<MAX_CHARS; offset++) {
            ptr = (BYTE *)memory+offset;
            dump(ptr);
        }
    
        free(memory);
    
        return 0;
    
    }
    
    
    void dump(BYTE *ptr) {
    
        printf("%x %x (%03d) ",(unsigned int)ptr,*ptr,*ptr);
    
        if (isprint(*ptr)) {
            printf("[%c]\n",*ptr);
        } else {
            printf("[.]\n");
        }
    
    }
    Output:

    80a2008 54 (084) [T]
    80a2009 65 (101) [e]
    80a200a 73 (115) [s]
    80a200b 74 (116) [t]

    This works great, just how I expected, however when I change the
    prgoram to use integers instead of chars:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    #define  MAX_INTS  4
    
    typedef unsigned char BYTE;
    
    void dump(BYTE *ptr);
    
    
    int main(void) {
    
        int offset = 0;
        BYTE *ptr = NULL;
        int *memory = NULL;
    
        memory = (int *)malloc(sizeof(int) * MAX_INTS);
    
        *(memory+0) = 1;
        *(memory+1) = 1;
        *(memory+2) = 4;
        *(memory+3) = 9;
    
        for (offset=0; offset<MAX_INTS; offset++) {
            ptr = (BYTE *)memory+offset;
            dump(ptr);
        }
    
        free(memory);
    
        return 0;
    
    }
    
    
    void dump(BYTE *ptr) {
    
        printf("%x %x (%03d) ",(unsigned int)ptr,*ptr,*ptr);
    
        if (isprint(*ptr)) {
            printf("[%c]\n",*ptr);
        } else {
            printf("[.]\n");
        }
    
    }
    Output:

    8f2e008 1 (001) [.]
    8f2e009 0 (000) [.]
    8f2e00a 0 (000) [.]
    8f2e00b 0 (000) [.]


    As you can see this does not work in the same manner, I understand that
    the char is 1-byte and the int is 4-bytes, but I thought when you increment
    a pointer it incremented based on its data type.

    I'm sure I'm doing something really dumb, can anyone please help?

    Any assistance with this problem would be much appreciated.

    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    In your dump function you ask if *ptr is a printable character. 1-9 is not printable characters, look up an ascii table to see for yourself.

    Edit: I see what your asking now, your pointer is a unsigned char pointer and you cast an int pointer to an unsigned char pointer.
    Last edited by Subsonics; 12-07-2010 at 05:24 AM.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    11

    Issue with pointer

    As per your comment about the ascii chart (thank you), I have changed the code as follows:

    Code:
        *(memory+0) = 49;
        *(memory+1) = 57;
        *(memory+2) = 55;
        *(memory+3) = 49;
    Output:

    93ec008 31 (049) [1]
    93ec009 0 (000) [.]
    93ec00a 0 (000) [.]
    93ec00b 0 (000) [.]

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Ok, now the pointer *ptr. It's of type BYTE (unsigned char) but you assign an int from *memory that is of type *int. There is a 3 byte size difference, you copy one byte from your four byte container, thus the result is corrupted. Change *ptr from BYTE to *int and it should work.

    Correction, you don't actually copy the value, but the effect of dereferencing *ptr is that only one byte gets printed since it's of type unsigned char.
    Last edited by Subsonics; 12-07-2010 at 05:37 AM.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Actually, since you have a typedef you can just change it to int. The cast in your for loop is then redundant but it should work.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    11
    I can see what I've done now. I've changed as you said and it works, many thanks for your assistance.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    I'm not paying enough attention! There is another issue here, it really should work to reach the lower byte of the int since the numbers are so low.

    And if you look at the output in the first example you see that the first one is correct. It prints 1.

    The problem is that the offset of memory is also four bytes so you are copying the first byte from int 1, then from int 2 etc.

    if you try to make the cast like this: (BYTE*)(memory+offset) it will work even for the unsigned char.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Argument from incompatible pointer type
    By AmritxD in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 03:23 PM
  2. Help with pointer issue
    By ph5 in forum C Programming
    Replies: 4
    Last Post: 11-17-2009, 04:19 PM
  3. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  4. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  5. Replies: 0
    Last Post: 03-20-2008, 07:59 AM