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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:31:57+00:00 2026-05-15T15:31:57+00:00

I am currently trying to allocate the same amount of memory for a double

  • 0

I am currently trying to allocate the same amount of memory for a double pointer. I take in a char** and want to use a bubble sort on that char** . So I create a temp char** and now I’m wondering how to correctly allocate enough memory so that I can return that temp char** to another method.

I know the way I’m allocating right now doesn’t look right and it certainly doesn’t work…otherwise I wouldn’t be asking this question. If someone could respond with some helpful advice, I would greatly appreciate it!

char** bubble_sort(char **filenames, int n)
{
    int i;
    char **new_list;
    new_list = malloc(sizeof(filenames));
    for (i = 0; i < n; i++)
    {
       // malloc(file_list.size * sizeof(int));
        new_list[i] = filenames[i];
    }
    for (i = 0; i < n; i++)
    {
        printf("%d: %s\n", i, new_list[i]);
    }

    int x;
    int y;
    for(x=0; x<n; x++)
    {
            for(y=0; y<n-1; y++)
            {
                    if(new_list[y]>new_list[y+1])
                    {
                            char *temp = new_list[y+1];
                            new_list[y+1] = new_list[y];
                            new_list[y] = temp;
                    }
            }
    }
    for (i = 0; i < n; i++)
       {
           printf("%d: %s\n", i, new_list[i]);
       }
    return new_list;
}
  • 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-15T15:31:58+00:00Added an answer on May 15, 2026 at 3:31 pm

    Here is the working copy of the program:

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    
    char** bubble_sort(const char **filenames, int n)
    {
        int i;
        char **new_list;
        new_list = (char**) malloc(sizeof(*new_list) * n);
        for (i = 0; i < n; i++)
        {
            new_list[i] = (char*) filenames[i];
        }
    
        printf("Initial list:\n");
        for (i = 0; i < n; i++)
        {
            printf("%d: %s\n", i, new_list[i]);
        }
    
        int x;
        int y;
    
        printf("List is sorted:\n");
        for(x=0; x<n; x++)
        {
                for(y=0; y<n-1; y++)
                {
                        if(strcmp(new_list[y],new_list[y+1])>0)
                        {
                                char *temp = new_list[y+1];
                                new_list[y+1] = new_list[y];
                                new_list[y] = temp;
                        }
                }
        }
        for (i = 0; i < n; i++)
           {
               printf("%d: %s\n", i, new_list[i]);
           }
        return new_list;
    }
    
    int main(){
        const char *ar[5]={
            "eee", "aaa", "bbb", "ccc", "ddd",
        };
        bubble_sort(ar, 5);
        return (0);
    }
    

    Still, keep in mind that your programming style resembles more to C than C++ (which is not always a bad thing).

    If you want to allocate new strings for your array elements, you should change the first for like this:

    for (i = 0; i < n; i++)
    {
        //new_list[i] = (char*) filenames[i];
        new_list[i] = (char*) malloc(sizeof(**new_list) * (strlen(filenames[i]) + 1));
        strcpy(new_list[i], filenames[i]);
    }
    

    And this is the C version (first one was the C++ version). Note that the string array has all its elements newly allocated, and is not using the initial strings from the input parameter.:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char** bubble_sort(char **filenames, int n)
    {
        int i;
        char **new_list;
        new_list = malloc(sizeof(*new_list) * n);
        for (i = 0; i < n; i++)
        {
            //new_list[i] = (char*) filenames[i];
            new_list[i] = malloc(sizeof(**new_list) * (strlen(filenames[i]) + 1));
            strcpy(new_list[i], filenames[i]);
        }
    
        printf("Initial list:\n");
        for (i = 0; i < n; i++)
        {
            printf("%d: %s\n", i, new_list[i]);
        }
    
        int x;
        int y;
    
        printf("List is sorted:\n");
        for(x=0; x<n; x++)
        {
                for(y=0; y<n-1; y++)
                {
                        if(strcmp(new_list[y],new_list[y+1])>0)
                        {
                                char *temp = new_list[y+1];
                                new_list[y+1] = new_list[y];
                                new_list[y] = temp;
                        }
                }
        }
        for (i = 0; i < n; i++)
           {
               printf("%d: %s\n", i, new_list[i]);
           }
        return new_list;
    }
    
    int main(){
        char *ar[5]={
            "eee", "aaa", "bbb", "ccc", "ddd",
        };
        bubble_sort(ar, 5);
        return (0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently trying at add ajax to a site, after much reading I discovered that
I currently trying to find a solution, how to ensure that a test fails
I am currently trying to diagnose a slow memory leak in my application. The
I am trying to store a large amount of boolean information that is determined
I currently trying to decide which embedded database to pick up for new .NET
Im currently trying to downlaod a audio track from a WCF, i need some
Im currently trying to learn some stuff about encryption, it's algorithms and how it
I am currently trying to copy an array I have to a new view
I'm currently trying to create a Profile Manager using a TIniFile to store the
I'm currently trying to put 5 videos back to back using AVMutableComposition like so:

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.