Confusion With Pointers

This is a discussion on Confusion With Pointers within the C Programming forums, part of the General Programming Boards category; Oh, okay now I understand exactly why it wasn't working for me. Like I said, the syntax is very confusing ...

  1. #16
    Registered User
    Join Date
    Jun 2011
    Posts
    21
    Oh, okay now I understand exactly why it wasn't working for me. Like I said, the syntax is very confusing because of all of the *pointing-at-this and &containing-that and such.
    So char *p = 'this' assigns 'this' to the array p points to?

    I think actually the code I was looking for looks more like this:
    Code:
    #include <stdio.h>
    
    char* printBin(unsigned short, char[]);
    
    int main (int argc, const char * argv[]){
        
        char buffer[17];
        
        char* buffer2 = printBin(230, buffer);
        
        printf("%s\n", buffer2);
        
        return 0;
    }
    
    char* printBin(unsigned short number, char buffer[]) {
        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;
    }
    which would be much more functional if I were to use it in a larger program. I'm just exploring the limits of what I can do with arrays and pointers. I find it incredibly irritating that you can't just return an array.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ObjectWithBrain View Post
    Oh, okay now I understand exactly why it wasn't working for me. Like I said, the syntax is very confusing because of all of the *pointing-at-this and &containing-that and such.
    So char *p = 'this' assigns 'this' to the array p points to?
    Not really.
    Code:
    int x;  /* an integer named x, this holds a number as its value */
    int *p; /* a pointer to an int. it holds the address of an integer as its value */
    Pointers are just like non-pointers. They are things that hold values. What's a value? 5 is a value. The address of another variable is a value. Pretty much everything in C is a value.
    Code:
    x = 5; /* put the number 5 in x as its value */
    p = &x; /* put the address of x as the value of p */
    Like so. You can use a pointer that points to something valid (that you have set to point specifically at something - that you have specifically set a value to) to indirectly put or get values from whatever they point at. Just like getting or putting a value from an integer, you can get or put values to an integer through a pointer.
    Code:
    *p = 6;
    Dereference p, and put a value into the variable it points at. Likewise you could get the value the same way:
    Code:
    int y;
    y = *p;
    How do I make a function take a pointer?
    Code:
    void foo( int * );
    This prototype says that this function takes a pointer to an integer. How do we actually send it a pointer?
    Code:
    foo( p ); /* p is a pointer, so we can pass it directly */
    foo( &x ); /* pass the value of x's address */
    Remember that everything in C is a value. The value this function wants is the value a pointer holds. That means you can match "int *p" in the declaration with the "int *" that the function takes, and just pass p directly. Or, since we know that the value p wants to hold is the address of an integer, we could just pass that address directly to the function, as we did when we did foo( &x ).


    Quzah.
    Last edited by quzah; 07-14-2011 at 01:46 AM.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User
    Join Date
    Jun 2011
    Posts
    21
    Whoa, I just realized that my buffer doesn't contain anything. Why is it even there? I should get rid of it.

    Pointers are insane. I swear, everytime I think I have them figured out, I run into some issue. There's just so much functionality, it's hard to figure out what syntax gives you the right values in the right variables.

  4. #19
    Registered User
    Join Date
    Jun 2011
    Posts
    21
    Okay, I got rid of that buffer, and now it stopped working. What did I do wrong? I guess I should make it a rule not to code past 1:00 AM.

    Code:
    #include <stdio.h>
    
    char* printBin(unsigned short);
    
    int main (int argc, const char * argv[]){
        
        printf("%s\n", printBin(230));
        
        return 0;
    }
    
    char* printBin(unsigned short number) {
        int shift;
        int bit;
        char buffer[17];
        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;
    }

  5. #20
    Registered User
    Join Date
    Jun 2011
    Posts
    21
    Did I just pass an empty pointer? I remember reading that arrays are destroyed at the return of the function they're contained in, so does that mean that my pointer now points at nothing?

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ObjectWithBrain View Post
    Did I just pass an empty pointer? I remember reading that arrays are destroyed at the return of the function they're contained in, so does that mean that my pointer now points at nothing?
    What is destroyed after a function ends is any variable you declare inside that function, that isn't declared static. This is true for any type of variable, array or not.
    Code:
    void foo( void )
    {
        int x;
        ...
    }
    x is gone when that function returns.
    Code:
    int foo( void )
    {
        int x;
        ...
        return x;
    }
    The value x has is returned at the end of this function, and then x is destroyed. In your case, your array is global anyway, so it's always going to be around. But that's irrelevant, since you are returning the value the pointer holds when the function ends. The ony way it would matter is if your array were local to the function, and you were trying to return a pointer to it, and it wasn't static:
    Code:
    int *foo( void )
    {
        int array[5];
        int *p = array;
        ...
        return p; /* this would be fine, except what p is
                  pointing to is destroyed when this returns */
    }

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

  7. #22
    Registered User
    Join Date
    Jun 2011
    Posts
    21
    So if the array is local to printBin(), like in the last version I showed which didn't work, is there any way to pass the value of binString (or buffer) without passing the pointer/array itself? (since obviously I can't do that).

  8. #23
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    What exactly are you trying to accomplish now? If you are trying to figure out pointers look at the link I provided you. It probably one of the best tutorials I have seen on pointers. Here it is again for your convenience.
    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. #24
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ObjectWithBrain View Post
    So if the array is local to printBin(), like in the last version I showed which didn't work, is there any way to pass the value of binString (or buffer) without passing the pointer/array itself? (since obviously I can't do that).
    Here's a little code example you can run to demonstrate why you should never try to return arrays from functions...

    Code:
    #include <stdio.h>
    
    int* MyFunction(int a, int b, int c)
      {  static int array[3];
         array[0] = a;
         array[1] = b;
         array[2] = c;
         return array;  } // return a pointer.
    
    
    int main (void)
      { int *a1, *a2;  // int pointers
    
        printf("calling a1 = MyFunction(10,20,30);\t");
        a1 = MyFunction(10,20,30);
        printf("a1 has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        printf("calling a2 = MyFunction(100,200,300);\t");
        a2 = MyFunction(100,200,300);
        printf("a2 has %d %d %d\n",a2[0],a2[1],a2[2]);
    
        printf("\nLooks good, except...\t"); 
        printf("a1 now has %d %d %d\n",a1[0],a1[1],a1[2]);
    
        getchar();
        return 0; }
    If you remove the static keyword the array is destoryed as the function returns and things get even stranger.

    The way to deal with this is to pass a pointer to the array into the top of the function and operate on the remote array.

  10. #25
    Registered User
    Join Date
    Jun 2011
    Posts
    21
    But I want to be able to pass the value of the buffer back through to main and I don't want to have to make a separate array outside of the function call just to do that. It just seems unnecessary. I've seen people do that with malloc but that seems to have it's downsides too. Like if i was to call this in an update function of an OpenGL program I would use up thousands of bytes of memory in minutes.

  11. #26
    Registered User
    Join Date
    Jun 2011
    Posts
    21
    At 60 frames per second I'd be using 61.2kB per minute.

  12. #27
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Again, I ask; what is it that you exactly want to accomplish here? quzah and Tater have both gone the details of the code you posted with you. Are you trying to create an array in your function and then return a pointer to that? If that is the case than malloc() would be the way to go. If the array can exist anywhere then just put it in main() and pass a pointer to the array to your function (which btw is just the array name), e.g.
    Code:
    void foo(char *);
    int main(void){
    
         char myArray[5];
         foo(myArray);
         ...lines of code
         return(0);
    }
    void foo(char *ptrToArray){
         ptrToArray[x] = 'y';
         .....
    }
    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.

  13. #28
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ObjectWithBrain View Post
    But I want to be able to pass the value of the buffer back through to main and I don't want to have to make a separate array outside of the function call just to do that. It just seems unnecessary.
    Considering that C cannot return an array, what it seems is totally necessary.

    "What you want" only applies if the language can actually do it.

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Quote Originally Posted by ObjectWithBrain
    But I want to be able to pass the value of the buffer back through to main and I don't want to have to make a separate array outside of the function call just to do that.
    You don't need to make a separate array outside of the function call to do just that: the pointer passed points to the first element of the array, hence aliasing, not a separate array, is involved.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
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, 12:07 AM
  2. Massive Confusion with Pointers
    By guitarist809 in forum C++ Programming
    Replies: 8
    Last Post: 07-22-2008, 04:01 PM
  3. array and pointers confusion
    By int80 in forum C Programming
    Replies: 5
    Last Post: 05-02-2008, 03:55 AM
  4. If else confusion
    By metaTron in forum C Programming
    Replies: 6
    Last Post: 02-11-2006, 11:26 PM
  5. Confusion about pointers
    By rishiputra in forum C Programming
    Replies: 1
    Last Post: 05-01-2003, 04: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