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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:47:03+00:00 2026-05-25T21:47:03+00:00

In C, how can I create a function which returns a string array? Or

  • 0

In C, how can I create a function which returns a string array? Or a multidimensional char array?

For example, I want to return an array char paths[20][20] created in a function.

My latest try is

char **GetEnv()
{
  int fd;
  char buf[1];
  char *paths[30];
  fd = open("filename" , O_RDONLY);

  int n=0;
  int c=0;
  int f=0;
  char tmp[64];

  while((ret = read(fd,buf,1))>0)
  {
    if(f==1)
    {
      while(buf[0]!=':')
      {
        tmp[c]=buf[0];
        c++;
      }
      strcpy(paths[n],tmp);
      n++;
      c=0;
    }
    if(buf[0] == '=')
      f=1;
  }
  close(fd);

  return **paths; //warning: return makes pointer from integer without a cast
  //return (char**)paths; warning: function returns address of local variable

}

I tried various ‘settings’ but each gives some kind of error.

I don’t know how C works

  • 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-25T21:47:03+00:00Added an answer on May 25, 2026 at 9:47 pm

    You can’t safely return a stack-allocated array (using the array[20][20] syntax).

    You should create a dynamic array using malloc:

    char **array = malloc(20 * sizeof(char *));
    int i;
    for(i=0; i != 20; ++i) {
        array[i] = malloc(20 * sizeof(char));
    }
    

    Then returning array works

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

Sidebar

Related Questions

I want to create a simple menu function which can call it example get_menu()
I'm looking for a PHP library/function/class which can create Identicon s.
I have a function which creates an array of Maps: map<string, int> *pMap And
I have an array of type string which I want to re-use inside a
I can create function pointers in Fortran 90, with code like real, external ::
Is there a way that I can create a function that takes an int
in C++, I can easily create a function pointer by taking the address of
It's possible to call C functions through CREATE FUNCTION but how can .NET functions
I'm looking to create a PHP function that can trim each line in a
I'm using CreateWindowEx() function to create an EDIT window, i.e. where a user can

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.