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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:26:55+00:00 2026-06-13T04:26:55+00:00

I made a small function that fills an allocated block of memory containing every

  • 0

I made a small function that fills an allocated block of memory containing every position of a given char within a given string and returns a pointer to the memory block.

The only problem with this function is that there is no way to check the size of the memory block; so I also made a function that counts the occurrence of a given char within a string.

Here is an example of use:

/*count occurences of char within a given string*/
size_t strchroc(const char *str, const char ch)
{ 
    int c = 0;
    while(*str) if(*(str++) == ch) c++;
    return c;
}

/*build array of positions of given char occurences within a given string*/
int *chrpos(const char *str, const char ch)
{
    int *array, *tmp, c = 0, i = 0;

    if(!(array = malloc(strlen(str) * sizeof(int)))) return 0x00;
    while(str[c])
    {
        if(str[c] == ch) array[i++] = c;
        c++;
    }
    if(!(tmp = realloc(array, i * sizeof(int)))) return 0x00;
    array = tmp;
    return array;
}

int main(void)
{
    char *str = "foobar foobar";                //'o' occurs at str[1], str[2], str[8], and str[9]
    int *array, b = 0, d;

    if(!(array = chrpos(str, 'o'))) exit(1);    //array[0] = 1, array[1] = 2, array[2] = 8, array[3] = 9

    /*
     * This is okay since I know that 'o'
     * only occures 4 times in str. There
     * may however be cases where I do not
     * know how many times a given char 
     * occurs so I figure that out before
     * utilizing the contents of array.
     * I do this with my function strchroc.
     * Below is a sample of how I would 
     * utilize the data contained within
     * array. This simply prints out str
     * and on a new line prints the given
     * char's location within the str 
     * array
     */

    puts(str);
    while(b < (int) strchroc(str, 'o'))         //loop once for each 'o' 
    {
        for(d = 0; d < (b == 0 ? array[b] : array[b] - array[b - 1] - 1); d++) putc((int) ' ', stdout);
        printf("%d", array[b]);
        b++;
    }
}

Output:

foobar foobar
 12     89

My only concern is that if one of these two functions fail, there is no way for the data to be used correctly. I was thinking about making the number of occurrences of char within the string an argument for chrpos but even then I would still have to call both functions.

I was wondering if anybody had any suggestions for a way to do this so that I only need one function to build the array.

The only way I can think of is by storing the number of char occurrences into array[0] and having array[1] through array[char_occurences] holding the positions of char.

If anybody has a better idea I would greatly appreciate it.

  • 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-13T04:26:57+00:00Added an answer on June 13, 2026 at 4:26 am

    As stated in my comment the first thing is to save up the data anyway, in case you can’t shrink the allocated memory :

    if (!(tmp = realloc(array, i * sizeof(int))))
      return array;
    return (tmp); //array = tmp; is useless
    

    If you want to protect a bit more your strchroc function add a if (!str) return 0; at the beginning.

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

Sidebar

Related Questions

I am trying to make a small test function that when passed a char
I've made a small function that I'll be reusing several times in an application.
For a program of mine I made a small function to clear the various
I've made a small game that uses level, and level is just a int
I have made a small log service that i want to publish to a
i made a small menu with <ul> <li> : js: $('#ulAcoesCliente li').click(function(ev) { $(.ulNone).each(function(){
I have made a small function which should print some InfoPath files. It is
I made a small function whose work to do add class in <li> when
Working on a small program that will take a string from a user and
I made a small MVC Framework, but i need that my view Class 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.