Thread: function - return statement

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    5

    function - return statement

    lets say i created a 2-dimentional array in a function, let's call it

    Code:
    #include...
    
    char array(){
    ...
    ...
    return ???;
    }
    
    int main(){
    char board[50][50];
    ...
    ...
    board[50][50] = array();  /*this part is totally wrong, but i think you should understand what im trying to do by this*/
    
    return 0;
    }
    is there a way to return a full array into the main portion of a program?
    if not, can you somehow loop the function such that it is able to return the specific array values back into the main?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Pass the array into the function and fill it that way.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    ok... thats really not what i want to do but thx for answering... what i want somthing like the following
    Code:
     #include...
    
    char array(int a){
    int b,c, d;
    char array1[a][a];
    
    for(b =0; b<=a;b++){
    for(c=0;c<=a;c++){
    d = rand()&#37;2;
    array1[b][c] =d
    }
    }
    return (somthing...dont know what goes here);
    }
    int main(){
    int x;
    
    scanf("%d", &x);
    char array[x][x] = array(x); //pretty sure this is wrong
    ...
    ...
    return 0;
    }
    somtheing like that where an array is created in a function... is there a way to return that function created array back into the main?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Please indent your code.
    It's possible, but it's not a very friendly way. You have to allocate memory on the heap and return a char** to there.
    Main will later have to call free to release the memory.
    Therefore it is preferred to pass in a buffer to the function which it can fill. Be sure to pass along the size of the buffer, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    5
    Ok... thx... one more question... if i want to print the array in the function... then present that printed array in the main would i also have to use a char**? or buffer... or is there a way around that?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by chungt004 View Post
    Ok... thx... one more question... if i want to print the array in the function... then present that printed array in the main would i also have to use a char**? or buffer... or is there a way around that?
    What does that mean? Do you mean that now you want a string (or an array of strings, or something similar) that contains what your array would like if printed?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Function return value conflict
    By cashmerelc in forum C Programming
    Replies: 6
    Last Post: 11-12-2007, 02:05 PM
  4. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 10:40 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM