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 8924251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:21:37+00:00 2026-06-15T07:21:37+00:00

I have a function that I pass an array into and an int into

  • 0

I have a function that I pass an array into and an int into from my main function. I am doing operations to the array inside this new function, let’s call it foo. In foo, I initialize another array with 52 cells all with 0. I do operations on the array that I passed from main, and transfer that data to the newly initialized array. I want to return the new array back to the main function. But of course, I can’t return data structures like arrays. So I instead return an int pointer that points to this array. Inside the int main, I pass the pointer to have it point to various cells in the array. When I print the results of what the pointer is pointing to, it should either be pointing to 0 or an integer greater than 0. But instead, I get inconsistent results. For some reason, some of the values that SHOULD be 0, prints out garbage data. I’ve been trying to spot the bug for some time, but I just wanted a second hand look at it. Here is just the GENERAL idea for the code for this portion anyways…

int main(){
    int *retPtr;
    char input[] = "abaecedg";
    retPtr = foo(input, size);
    for(i=0; i<52; i++){
        // error displayed here
        printf("%d\n", *(retPr + i));
    }
}

int foo(char input[], int size)
{
    int arr[52] = {0};      // should initialize all 52 cells with 0.
    int i=0, value;         // looking for non-zero results in the end.        
    int *ptr = &arr[0];                        
    for(i=0; i<size; i++){
        if(arr[i] > 64 && arr[i] < 91){
            value = input[i] - 65;
            arr[value]++;
        }
    }
    return ptr;
}

Hopefully this makes sense of what I’m trying to do. In the foo function, I am trying to find the frequency of certain alphabets. I know this might be a bit cryptic, but the code is quite long with comments and everything so I wanted to make it as succinct as possible. Is there any possible reason why I’m getting correct values for some (numbers > 0, 0) and garbage values in the other?

  • 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-06-15T07:21:39+00:00Added an answer on June 15, 2026 at 7:21 am

    The reason you get garbage back is that the array created in foo is allocated in foos stack frame, and you then return a pointer into that frame. That frame is discarded when foo returns.

    You should allocate the array on the heap (using malloc and friends) if you want it to remain after foo returns. Don’t forget to free() it when you’re done with the array.

    int main(){
        char input[] = "abaecedg";
        int retPtr[] = foo(input, size); //An array and a pointer is the same thing
        ...
        free(retPtr);
    }
    
    int *foo(char input[], int size)
    {
        int arr[] = calloc(52*sizeof(int); // should initialize all 52 cells with 0.
        ...
        arr[value]++;
        ...
        return arr;
    }
    

    Another way is to let foo take an array as a parameter and work with that, in this way:

    int main(){
        int ret[52] = {0};
        ...
        foo(input, size, ret);
        ...
    }
    
    void foo(char input[], int size, int *arr)
    {
        ...
        arr[value]++;
        ...
        return; //Don't return anything, you have changed the array in-place
    }
    

    The reason this works is because an array is the exact same thing as a pointer, so you are really passing the array by reference into foo. arr will be pointing to the same place as ret, into the stack frame of main.

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

Sidebar

Related Questions

I Have an Array of users that i want to pass into a view
I have a multidimensional array of integers that only works inside of the function,
I have a function that receives a an array from the $_POST function then
I have a function that i want to pass as a parameter function myFun(){
I have a function that I want to pass an argument, market, to the
I have a pretty large object that I need to pass to a function
Inside my Controller i have function that runs after user clicks on item, which
In my Java code I have function that gets file from the client in
gcc 4.6.2 c89 I have the following 2D array that I want to pass
I have to pass two arrays 1) that are filled with 1000 int's between

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.