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

  • Home
  • SEARCH
  • 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 8307177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:28:31+00:00 2026-06-08T18:28:31+00:00

So at the moment I have a function I made returning a static array,

  • 0

So at the moment I have a function I made returning a static array, is there any way to make it return a dynamic array for the sake of efficiency?

#include <stdio.h>
#include <stdlib.h>
#include "header.h"

int *charpos(char *str, char ch)
{
    int *bff, bc, ec, i, strln;
    static int ret[255];
    bc = 0;
    ec = 0;

    for(i = 0; str[i] != '\0'; i++)
        ;

    strln = i;
    for(i = 0; i <= strln; i++)
    {
        if(str[i] == ch)
            ec++;
    }

    bff = malloc(sizeof(int)*ec);
    if(sizeof(bff) > sizeof(ret))
    {
        free(bff);
        return 0;
    }

    for(i = 0; i <= 255; i++) ret[i] = '\0';
    for(i = 0; i <= strln; i++)
    {
        if(str[i] == ch)
        {
            ret[bc] = i;
            bc++;
        }
    }

    free(bff);
    return ret;
}
  • 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-08T18:28:32+00:00Added an answer on June 8, 2026 at 6:28 pm

    Functions cannot return arrays, period. You can of course a pointer or take a pointer to a block of memory that has been allocated by the caller. So, in your case…

    int *ret = malloc(255 * sizeof int);  // caller must deallocate!
    

    This does change the semantics of your code however. The caller of your function is now responsible for calling free() on the returned pointer. If they do not you will leak memory, so this adds some amount of complexity that did not exist before. I would prefer something like this instead:

    void charpos(int *p, size_t size, const char *str, char ch) {
        // initialize the memory 
        memset(p, 0, size * sizeof int);
        
        // your other code here...
    
        size_t len = strlen(str);
        // fill the caller's memory
        for(i = 0; i < len; ++i)
        {
            if(str[i] == ch)
                p[bc++] = i;
        }
    }
    

    You’re returning a pointer to int which referes to the first element of a statically allocated array.

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

Sidebar

Related Questions

let say i have function like below function doSomethingNow(){ callSomethingInFutureNotExistNow(); } at the moment
I have a function that I'm trying to optimize at the moment but I'm
So at the moment I have a multidimensional array string[,] items = new string[100,
I am making a homepage at the moment and I have made an edge
I have a function that at the moment programmed in a functional model and
I'm refactoring some code at the moment and have come across a selector: jQuery(tr,#ctl00_MainContent_MyUserControl).each(function(i,row)
At the moment i have a function and a filter which shortens titles if
I have a game server which is made in Java. I want to make
I have previously only made web applications in Asp.Net MVC, and there you can
At the moment I have a slider and an small input text box which

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.