Confusion With Pointers

This is a discussion on Confusion With Pointers within the C Programming forums, part of the General Programming Boards category; Im a noob to programming in general, and one of the biggest confusions I've had is with pointers in C. ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    21

    Confusion With Pointers

    Im a noob to programming in general, and one of the biggest confusions I've had is with pointers in C. They make perfect sense to me conceptually, but syntactically they're incredibly confusing. I was writing a program that takes in an unsigned short and prints it in binary one digit at a time, and it worked. But when I went to try to store it in a pointer to a char[] I couldn't get it to work. Ive looked at at least five different tutorials on pointers and from what I read, the code I have should work, but it doesn't.

    I get no compile errors or even warnings, but all it prints is a single newline. I left in the code I had originally for just printing it one character at a time. If you uncomment the two printf statements in printBin(), you'll see that the loop runs properly and does what it's supposed to do.

    I think this must be a simple problem, but I can't figure it out by myself. Thank you in advance for any help you can give me.

    Code:
    #include <stdio.h>
    
    char buffer[17];
    
    char* printBin(unsigned short);
    
    int main (int argc, const char * argv[]){
        
        char* buffer2 = &buffer[0];
        
        buffer2 = printBin(360);
        
        printf("%s\n", buffer2);
        
        return 0;
    }
    
    char* printBin(unsigned short number) {
        int shift;
        int bit;
        char* binString = &buffer[0];
        
        for (shift = 15; shift >= 0; shift--, binString++) {
            
            bit = 1 << shift;
            
            if ((number & bit) == bit) {
                sscanf(binString, "1");
                //printf("1");
            }
            else {
                sscanf(binString, "0");
                //printf("0");
            }
            
        }
        //sscanf(binString, "hello");
        return binString;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why do you think scanf will print something?

    And if you want to put something somewhere, why not use the equals sign, since that's what it's for?

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Code:
    for (shift = 15; shift >= 0; shift--, binString++) {
            
            bit = 1 << shift;
            
            if ((number & bit) == bit) {
                sscanf(binString, "1");
                //printf("1");
            }
            else {
                sscanf(binString, "0");
                //printf("0");
            }
            
        }
        //sscanf(binString, "hello");
        return binString;
    }
    Where does binString point to after your loop. Is it the begginning of buffer?
    Also you might want to take a look at what sscanf does
    Last edited by AndrewHunter; 07-13-2011 at 09:45 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    21
    Ohh wow, I dont know what I was thinking using sscanf. I totally forgot what it does. Thanks for that.
    So I should switch out sscanf() for = and then decrement binString by 16?
    I'll give that a try. Thanks guys.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Don't forget your are saving a char to your buffer, not a string so: '1' instead of "1".
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    21
    Didn't work. My code is now as follows:
    Code:
    #include <stdio.h>
    
    char buffer[17];
    
    char* printBin(unsigned short);
    
    int main (int argc, const char * argv[]){
        
        char* buffer2 = &buffer[0];
        
        buffer2 = printBin(360);
        
        printf("%s\n", buffer2);
        
        return 0;
    }
    
    char* printBin(unsigned short number) {
        int shift;
        int bit;
        char* binString = &buffer[0];
        
        for (shift = 15; shift >= 0; shift--, binString++) {
            
            bit = 1 << shift;
            
            if ((number & bit) == bit) {
                binString = "1";
                //printf("1");
            }
            else {
                binString = "0";
                //printf("0");
            }
        }
        binString -= 16;
        
        return binString;
    }

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    21
    '1' saves an int to my buffer and gives me an incompatible integer to pointer conversion.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by AndrewHunter View Post
    Don't forget your are saving a char to your buffer, not a string so: '1' instead of "1".
    Check the post I gave you. ^^^
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    You need to dereference your pointer to save a value to it.
    Code:
    if ((number & bit) == bit) {
                 *binString= '1';
                //printf("1");
            }
            else {
                *binString = '0';
                //printf("0");
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    In addition, you could have binky explain pointers to you. And then after you are done laughing you could read this tutorial.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    21
    oh! it all clicks! it works now. thank you so much Andrew, you've saved me such a headache =)

    My final code is

    Code:
    #include <stdio.h>
    
    char buffer[17];
    
    char* printBin(unsigned short);
    
    int main (int argc, const char * argv[]){
        
        char* buffer2 = &buffer[0];
        
        buffer2 = printBin(360);
        
        printf("%s\n", buffer2);
        
        return 0;
    }
    
    char* printBin(unsigned short number) {
        int shift;
        int bit;
        char* binString = &buffer[0];
    
        
        for (shift = 15; shift >= 0; shift--, binString++) {
            
            bit = 1 << shift;
            
            if ((number & bit) == bit) {
                *binString = '1';
                //printf("1");
            }
            else {
                *binString = '0';
                //printf("0");
            }
        }
        binString -= 16;
        
        return binString;
    }
    And my output is:
    0000000101101000
    as it should be =)

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Since no one bothered pointing this out, I will:
    Quote Originally Posted by ObjectWithBrain View Post
    '1' saves an int to my buffer and gives me an incompatible integer to pointer conversion.
    That's not at all what it does:
    Code:
    char* printBin(unsigned short number) {
        int shift;
        int bit;
        char* binString = &buffer[0]; /* point at the buffer (which is global,
                                         so I don't know why you are using
                                         pointers at all...) */
        
        for (shift = 15; shift >= 0; shift--, binString++ /* increment pointer */) {
            
            bit = 1 << shift;
            
            if ((number & bit) == bit) {
                binString = "1"; /* point at a string literal,
                                    you are no longer in 'buffer' */
                //printf("1");
            }
            else {
                binString = "0"; /* same as above */
                //printf("0");
            }
        }
        binString -= 16;
        
        return binString;
    }
    First you point at a buffer. Then you point yourself at a string literal, so you are no longer pointing at that buffer. Then you increment that pointer, making it point to the nul character the string literal contains (since it is only 1 non-nul character, either 1 or 0). Then you go make it point at the start of a string literal again the next time through the loop.

    In the end, you don't get anything useful added to your buffer.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    It seemed like it was just an exercise in pointers to me, albeit a weird one. Also I think all those things were addressed in the above posts. Did you see the OP's final code posted?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by AndrewHunter View Post
    Did you see the OP's final code posted?
    Yes, but no one had described what was actually taking place, so I thought I would.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Good point. I will keep that in mind.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confusion using pointers
    By kluxy in forum C Programming
    Replies: 10
    Last Post: 03-27-2010, 01:07 AM
  2. Massive Confusion with Pointers
    By guitarist809 in forum C++ Programming
    Replies: 8
    Last Post: 07-22-2008, 05:01 PM
  3. array and pointers confusion
    By int80 in forum C Programming
    Replies: 5
    Last Post: 05-02-2008, 04:55 AM
  4. If else confusion
    By metaTron in forum C Programming
    Replies: 6
    Last Post: 02-12-2006, 12:26 AM
  5. Confusion about pointers
    By rishiputra in forum C Programming
    Replies: 1
    Last Post: 05-01-2003, 05:39 PM

Tags for this Thread


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