Thread: getting a function to return a char array

  1. #16
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I think it has to do with how gcc handles functions that return structures.
    -fpcc-struct-return
    Return “short” struct and union values in memory like longer ones, rather than in registers. This convention is less efficient, but it has the advantage of allowing intercallability between GCC-compiled files and files compiled with other compilers, particularly the Portable C Compiler (pcc).

    The precise convention for returning structures in memory depends on the target configuration macros.

    Short structures and unions are those whose size and alignment match that of some integer type.

    Warning: code compiled with the -fpcc-struct-return switch is not binary compatible with code compiled with the -freg-struct-return switch. Use it to conform to a non-default application binary interface.
    http://gcc.gnu.org/onlinedocs/gccint...te-Return.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  2. #17
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    could i also do something like this?

    Code:
    #include <stdio.h> 
    
    char testmain[5];
    
    int postchar( void )
    {
        testmain[0]='a';
        
           printf("%s",testmain);
           return;
    }
    
    int main( void )
    {
        postchar();
      printf("%s",testmain);
      
      getch();
      
      return 0;
    }
    ++ does anyone have a link to what the -> does , i cant search it on google because it wont let me.

    thanks
    Last edited by variable; 02-09-2005 at 11:28 PM. Reason: tutorial link
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

  3. #18

  4. #19
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    thanks for the link.

    is the code i posted ok? i know it works, but sometimes just because it works, it isnt right.
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

  5. #20
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Yes, your code is fine. All global variables are zeroed out at startup. So at startup testmain looks like:
    Code:
    '\0' '\0' '\0' '\0' '\0'
    Once you run:
    Code:
    testmain[0] = 'a';
    it will look like:
    Code:
    'a' '\0' '\0' '\0' '\0'
    This is a valid string so you can safely prinit it out. However, the use of globals to pass data from one function to another is often frowned upon. It is usually better to use function arguments.

  6. #21
    the lowly newb
    Join Date
    Jan 2005
    Location
    IL
    Posts
    49
    ah, ok thanks for the help

    --variable
    "Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life."

    "They say a little knowledge is a dangerous thing, but it's not one half so bad as a lot of ignorance."

    "Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it."
    [all]- Terry Pratchett

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM