Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7701121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:54:32+00:00 2026-05-31T22:54:32+00:00

Basically the exercise says Create a function which takes as an argument an array

  • 0

Basically the exercise says “Create a function which takes as an argument an array and its size and returns a new array with contains the three biggest values of the array given as the argument.” So I thought that I will take the array and sort it in the function and then give the 3 highest values to a new array and return it. That’s my code.

int *function(int *A, int k){
    int B[3],i,j,temp;

    //Sorting
    for(i=k-1;i>0;i--){
        for(j=1;j<=i;j++){
            if(A[j-1] > A[j])
            {
                temp = A[j-1];
                A[j-1] = A[j];
                A[j] = temp;
            }
        }
    }
    i = 0;
    while(i < 3){
        B[i]= A[k-1];
        i++;
        k--;
    }
    return B;   
} 

int main (int argc, const char * argv[]) {
   int A[5] = {1,8,7,4,6};
    int size = 5;
    int *func,i;

    func = function(A,size);
    for(i=0;i<3;i++)
        printf("%d ",func[i]); 
    return 0;
}

As a result I should get 8 7 6 but I’m getting 8 -1073743756 6. I can’t find the mistake. I also get a warning when I want to return B. It says “Function return address of local variable.” Maybe this has something to do with this problem. Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:54:33+00:00Added an answer on May 31, 2026 at 10:54 pm

    The ‘lifetime’ of the variable B is for the duration of the function in which it is declared. When it is out of scope it no longer exists, and its memory may be reused for other purposes. You should regard all compiler warnings as errors – they are usually semantic errors (as opposed to syntax errors); that is to say, the compiler understands the code, but it is unlikely that it is what you intended, or may have unintended or undefined behaviour.

    In this case the compiler is telling you exactly what the problem is. What you need to be asking perhaps is how best to resolve the error.

    The normal pattern to use when a caller requires an array to be filled by a function is for the caller to provide the array to the function. Because arrays ‘decay’ to pointers when passed to a function, it is normal to also pass the length, and this is useful to avoid buffer overrun. You could have the function “know” that the buffer length is always 3 in your case, but it is less maintainable and less safe.

    Typically such a function returns a pointer to the caller’s buffer, or NULL on failure, so it can be used for error checking or for use as the argument to another function.

    Example:

    int* fn( int* caller_buffer, int caller_buffer_length )
    {
        int i ;
        for( i = 0; i < caller_buffer_length; i++ )
        {
            caller_buffer[i] = ... ;
        }
    }
    

    then

    int my_buffer[3] = {0} ;
    int my_buffer_length = sizeof(my_buffer) / sizeof(*my_buffer) ;
    int* error_check = 0 ;
    
    ...
    
    error_check = fn( my_buffer, my_buffer_length ) ;
    if( error_check != 0 )
    {
        ...
    }
    

    It is possible to resolve the problem by either allocating B statically, globally, or dynamically. All these solutions I would class as “quick-and-dirty”. They may resolve this particular problem, but are not a general pattern that scales well to more complex and larger applications.

    Dynamic memory allocation locally within function() begs the question of who is then responsible to freeing the memory allocated?

    Local static allocation works, but if you call the function a second time the previous data will be lost, which may not always be acceptable. It is also not reentarnt, causing potential problems in a multi-threaded application, or recursive algorithms.

    Global data is statically allocated but as the additional problem of being globally visible, so no longer under the sole control of the one function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically what I need to do is write a function that takes in a
I have to solve an exercise using awk. Basically I need to retrieve from
I'm doing a book exercise that says to write a program that generates psuedorandom
I am pretty new to java dev & JSF framework and as an exercise
Trying to learn C++ and working through a simple exercise on arrays. Basically, I've
I am just starting new to Perl and I came across an exercise that
Okay, so basically when I click the below buttons I want a new separate
I'm trying to create a static file server in nodejs more as an exercise
Basically I have some variables that I don't want to preinitialize: originalTime = None
basically i have (state, state code) pairs, that are subsets of country [USA] ->

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.