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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:38:27+00:00 2026-06-08T09:38:27+00:00

I am relatively new to C and I need some help with methods dealing

  • 0

I am relatively new to C and I need some help with methods dealing with arrays. Coming from Java programming, I am used to being able to say int [] method() in order to return an array. However, I have found out that with C you have to use pointers for arrays when you return them. Being a new programmer, I really do not understand this at all, even with the many forums I have looked through.

Basically, I am trying to write a method that returns a char array in C. I will provide the method (let’s call it returnArray) with an array. It will create a new array from the previous array and return a pointer to it. I just need some help on how to get this started and how to read the pointer once it is sent out of the array.

Proposed Code Format for Array Returning Function

char *returnArray(char array []){
  char returned [10];
  // Methods to pull values from the array, interpret
  // them, and then create a new array
  return &(returned[0]); // Is this correct?
}

Caller of the Function

int main(){
  int i = 0;
  char array [] = {1, 0, 0, 0, 0, 1, 1};
  char arrayCount = 0;
  char* returnedArray = returnArray(&arrayCount); // Is this correct?
  for (i=0; i<10; i++)
    printf(%d, ",", returnedArray[i]); // Is this correctly formatted?
}

I have not tested this yet as my C compiler is not working at the moment, but I would like to figure this out.

  • 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-08T09:38:28+00:00Added an answer on June 8, 2026 at 9:38 am

    You can’t return arrays from functions in C. You also can’t (shouldn’t) do this:

    char *returnArray(char array []){
     char returned [10];
     //methods to pull values from array, interpret them, and then create new array
     return &(returned[0]); //is this correct?
    } 
    

    returned is created with automatic storage duration and references to it will become invalid once it leaves its declaring scope, i.e., when the function returns.

    You will need to dynamically allocate the memory inside of the function or fill a preallocated buffer provided by the caller.

    Option 1:

    dynamically allocate the memory inside of the function (caller responsible for deallocating ret)

    char *foo(int count) {
        char *ret = malloc(count);
        if(!ret)
            return NULL;
    
        for(int i = 0; i < count; ++i) 
            ret[i] = i;
    
        return ret;
    }
    

    Call it like so:

    int main() {
        char *p = foo(10);
        if(p) {
            // do stuff with p
            free(p);
        }
    
        return 0;
    }
    

    Option 2:

    fill a preallocated buffer provided by the caller (caller allocates buf and passes to the function)

    void foo(char *buf, int count) {
        for(int i = 0; i < count; ++i)
            buf[i] = i;
    }
    

    And call it like so:

    int main() {
        char arr[10] = {0};
        foo(arr, 10);
        // No need to deallocate because we allocated 
        // arr with automatic storage duration.
        // If we had dynamically allocated it
        // (i.e. malloc or some variant) then we 
        // would need to call free(arr)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really need your help for this. I am relatively new to programming and
I am relatively new with sql and I need some help with some basic
I am relatively new to Objective C and need some array help. I have
I am relatively new to revision control and I need some help deciding if
I am relatively new to programming. I need to calculate the aspect ratio(16:9 or
Being relatively new to programming--think Programming in Objective-C by Kochan, Chapter 15--I'm wondering if
I'm relatively new to the world of WhiteBox Testing and need help designing a
Relatively new Windows Phone developer here, looking for some help. Basically I'm messing around
I'm relatively new to C# - but come from a C/C++ background. I need
I'm relatively new to Subversion, hoping to get some insights from more experienced folks.

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.