CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jul 2008
    Posts
    36

    Someone tell me how this can be....

    Code:
    #include <stdio.h>
    
    char* arrayReturner()
    {
      static char arr[20] = "Hello World";
      printf("%s \n", arr);
      return(arr);
    }
    
    main()
    {
      char *str;
      str = arrayReturner();
    //  printf(str);
    }
    the "arrayReturner" function returns a char pointer.. Yet the printf in that routine output "hello world"... Since "arr" is being used for the printf and the return of the function shouldnt the printf show the memory location???

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Someone tell me how this can be....

    The argument corresponding to %s is always interpreted as a string, no matter what.

    Independently, in almost every case where it matters, char* is assumed to be a string as well, again no matter how else it may be used.

  3. #3
    Join Date
    Jul 2008
    Posts
    36

    Re: Someone tell me how this can be....

    Quote Originally Posted by Lindley
    The argument corresponding to %s is always interpreted as a string, no matter what.

    Independently, in almost every case where it matters, char* is assumed to be a string as well, again no matter how else it may be used.
    So depending on how you use it, a char* can be a memory address or the value in the memory address?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Someone tell me how this can be....

    A char* is always a memory address.

    However, some functions make assumptions about what sort of data that address holds. Usually they assume it's a null-terminated array of chars.

  5. #5
    Join Date
    Jul 2008
    Posts
    36

    Re: Someone tell me how this can be....

    Quote Originally Posted by Lindley
    A char* is always a memory address.

    However, some functions make assumptions about what sort of data that address holds. Usually they assume it's a null-terminated array of chars.
    Right but suppose you passed a string to printf, it would print the value of the string.. But suppose you passed a pointer to a string, what would it print then?

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Someone tell me how this can be....

    If you give it the %s format specifier, it will dereference whatever you pass it, then print out each subsequent byte interpreted as an ASCII code until it encounters a 0 byte.

    If you pass it something bogus, it'll still do exactly the same thing, but it might crash your program if it tries to read from an invalid address.

    If you pass it a string literal like "Hello", you're actually passing the address where that literal exists in memory. Usually read-only memory in that case, incidentally.

    Printf() has no notion of what its arguments "really" are. All it has is the format string, and that determines *entirely* what it tries to do with the arguments.
    Last edited by Lindley; August 14th, 2008 at 03:23 PM.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Someone tell me how this can be....

    But suppose you passed a pointer to a string, what would it print then?
    I believe the behaviour is undefined: the s conversion specifier requires the corresponding argument to be a pointer to the initial element of an array of char or an array of wchar_t.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Someone tell me how this can be....

    I believe for wchar_t the specifier must be a capital S.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Someone tell me how this can be....

    I believe for wchar_t the specifier must be a capital S.
    C99 says otherwise, but requires an l length modifier.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Jul 2008
    Posts
    36

    Re: Someone tell me how this can be....

    Quote Originally Posted by Lindley
    If you give it the %s format specifier, it will dereference whatever you pass it, then print out each subsequent byte interpreted as an ASCII code until it encounters a 0 byte.

    If you pass it something bogus, it'll still do exactly the same thing, but it might crash your program if it tries to read from an invalid address.

    If you pass it a string literal like "Hello", you're actually passing the address where that literal exists in memory. Usually read-only memory in that case, incidentally.

    Printf() has no notion of what its arguments "really" are. All it has is the format string, and that determines *entirely* what it tries to do with the arguments.
    so printf will dereference a pointer then? From what I gather here printf only gets memory addresses, is that correct?

    meaning:
    1. printf("abc123") means printf will get a memory address , and in that memory address is "abc123", is this correct?

    2.
    int myint = 5;
    printf(myint);
    -- does this mean that printf gets the memory address to where the integer for myint is stored and prints whats in that memory address?

    Im reading my c++ book at the same time that im posting in this thread and I just want to make sure I understand this, obviously im new to c++..

    If the above is correct, is there a print function that will print whatever I send it no matter what? Meaning if I send a pointer it will print the "memory address" thats stored in that pointer variable? Will cout do this?
    Last edited by tgreaves; August 14th, 2008 at 03:34 PM.

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Someone tell me how this can be....

    From what I gather here printf only gets memory addresses, is that correct?
    Obviously not. Surely you have seen:
    Code:
    printf("%d\n", 123);
    123 is an integer, not an address (though there may be an address with such a value).

    1. printf("abc123") means printf will get a memory address , and in that memory address is "abc123", is this correct?
    What do you mean by "printf will get a memory address"? Because it is true that printf() will get an address as its first argument, namely a pointer to the first char in the string literal "abc123", but this is probably not what you were thinking of.

    If the above is correct, is there a print function that will print whatever I send it no matter what?
    No, but you can write your own functions for whatever you want to print.

    does this mean that printf gets the memory address to where the integer for myint is stored and prints whats in that memory address?
    In the first place, there is no standard printf function defined that takes an integer as its first argument. As such, your code snippet will not even compile.

    Meaning if I send a pointer it will print the "memory address" thats stored in that pointer variable?
    You can print addresses by using the format specifier %p, and by casting the pointer to void*.

    Will cout do this?
    Due to the magic of operator overloading, cout can print whatever is thrown at it, so long as operator<< is overloaded for that type with respect to std::ostream. This is kind of like the idea that you can write your own functions for whatever you want to print, except that the function has a particular name.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Someone tell me how this can be....

    Quote Originally Posted by tgreaves
    so printf will dereference a pointer then?
    If you use the %s format specifier, yes. Other specifiers may result in different behavior.

    From what I gather here printf only gets memory addresses, is that correct?
    No.....the rule you're looking for is that literal strings have type char*. So, for instance:

    1. printf("abc123") means printf will get a memory address , and in that memory address is "abc123", is this correct?
    printf's first argument must be a format string. Usually that string is written as a literal, but it doesn't have to be; any char* pointing to a C-style string will do. In that sense you're correct. In this case there's no format specifier in the string, so it just prints it; but, incidentally, any of these would have an identical result:

    Code:
    printf("%s","abc123");
    
    char format[5] = "%s";
    printf(format,"abc123");
    
    char arg[10] = "abc123";
    printf("%s",arg);
    
    char format[10] = "abc123";
    printf(format);
    2.
    int myint = 5;
    printf(myint);
    -- does this mean that printf gets the memory address to where the integer for myint is stored and prints whats in that memory address?
    There's no format string here, so the compiler will complain. The way to handle integers with printf is to give it a format string with integer specifiers. %d and %i are the two most common ones.

    If the above is correct, is there a print function that will print whatever I send it no matter what? Meaning if I send a pointer it will print the "memory address" thats stored in that pointer variable?
    Format specifiers are everything with printf. You can do lots of fancy things with them, but they take some time to learn.

    Please note, however, then when you get to *scanf()*, then yes----practically everything needs to be an address.
    Last edited by Lindley; August 14th, 2008 at 04:19 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured