Thread: Arrays

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    11

    Arrays

    hi
    Can anyone tell me how can i return a array of three integers form a function;

    Code:
         
    int funtion()
    {
    int array[]={ 2,3,4 };
    
    return array[];}
    will it work

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You cannot return an array from a function. What is the purpose of this function? Depending on its purpose, you could say, assign to an array that is passed (as a pointer) to this function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    This is the way you "return array". As laserlight said you return the pointer:

    Code:
    int funtion()
    {
      int array[]={ 2,3,4 };
    
      return array;
    }
    But keep in mind that this should be WRONG. You will be returning a local variable. I mean array is a local variable for function() so the memory is allocated when calling function() and freed when you leave function() so you cannot return it. If you wanted the above you would have to use malloc(). Or pass the array as a parameter to the function as this:

    Code:
    int funtion(int *array)
    {
      array[]={ 2,3,4 }; //not sure if this would work
    
      return array;
    }
    Generally there are no real arrays in C. Keep in mind that array[i] means *(array+i), thus saying the value of the memory that is shown by the pointer array plus i spaces of memory.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Code:
    array[]={ 2,3,4 }; //not sure if this would work
    Nope it wouldn't. That's an initializer list and can (as the name suggests) only be used at the initialization stage, i.e. when the variable is defined.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    int funtion()
    {
      int array[]={ 2,3,4 };
    
      return array;
    }
    Nope, this would be wrong.
    int[3] != int
    So essentially, you are returning an incorrect type, as well as a local variable as you describe.
    Also, when passing an array to a function, ALWAYS pass the array's size!

    Code:
    void funtion(int* array, int size)
    {
      if (size < 3)
        /* ERROR: Too small buffer */;
      int array2[] = { 2, 3, 4 };
      memcpy(array, array2, sizeof(array2));
    }
    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.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Elysia View Post
    Code:
    int funtion()
    {
      int array[]={ 2,3,4 };
    
      return array;
    }
    Nope, this would be wrong.
    int[3] != int
    Right. So you would need (see the *):
    Code:
    int * function(...) {...}

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, that would work.
    Only the local variable problem left that you mentioned
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM