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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:43:14+00:00 2026-06-14T03:43:14+00:00

At the start of the programm, I allocate memory for an array of char-pointers:

  • 0

At the start of the programm, I allocate memory for an array of char-pointers:

char **buffer = calloc( 20, sizeof(char *) );

Then the user can input up to 20 words:

buffer[i] = calloc( 40, sizeof(char) );
fgets( buffer[i], 40, stdin )`

Afterwards I want to sort this array. It works as expected if I use my swap function as follows:

void swap(char *args1, char *args2) {
    char tmp[40];
    strcpy( tmp, args1 );
    strcpy( args1, args2 );
    strcpy( args2, tmp );
}

void sort( char **args, int count ) {
    ...
    swap( args[i], args[j] );
    ...
}

After thinking through this, I noticed that this was a waste of CPU since all I had to do was actually redirecting the pointers to the corresponding strings.
So I rewrote my swap function:

void swap(char **args1, char **args2) {
    char *tmp = *args1;
    *args1 = *args2;
    *args2 = tmp;
}

void sort( char **args, int count ) {
    ...
    swap( &args[i], &args[j] );
    ...
}

However, this will not work at all, the results are extremely unexpected, I cannot figure out why (I tried several printf calls and whatnot)… My understanding was that the pointers are just redirected and thus swapped, let’s say the memory looks like this:

(begin of char**):
100: *160
108: *200
116: *240
124: *280
...
(begin of char*):
160: Hello!\0
200: World!\0
...

My idea was to alter the pointers instead of the arrays for minimum CPU effort (Here: swap pointer in 100 with pointer in 108):

(begin of char**):
100: *200
108: *160
116: *240
124: *280
...
(begin of char*):
160: Hello!\0
200: World!\0
...

I tried to explain this as thorough as I could and I’m sorry if it’s too much explanation.
I would be most glad if someone could give me insight into this and help!

The full code (with the working strcpy) can be found here: http://pastie.org/5361481

  • 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-14T03:43:15+00:00Added an answer on June 14, 2026 at 3:43 am

    Your sort function should end up looking like this:

    void sort(char ** args, const int start, const int end) {
            char **pivot = &args[end];
            int i = start-1, j = start;
            while( j < end ) {
                    int cmp = strcmp( *pivot, args[j] );
                    if( cmp > 0 )
                            swap( &args[++i], &args[j] );
                    j++;
            }
            swap( &args[++i], pivot );
            if( start + 1 < i )
                    sort( args, start, i - 1 );
            if( end - 1 > i )
                    sort( args, i + 1, end );
    }
    

    I suspect that you didn’t make the pivot be a char**, but left it as a char*. If you do that, then whenever you do the swap, you aren’t actually swapping two elements in your array, but rather swapping one element of the array with a local variable. The pivot variable ends up pointing to a different string instead of the last array member pointing to a different string.

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

Sidebar

Related Questions

I am trying to start a X-Programm (e.g. gedit or Firefox) with crontab. I
I need to start a program every time the user logs in, but I
I'm trying to measure, in a C# programm, the memory usage. I'd like to
I'm working with some memory pointers. I don't want to use hash defines, please
In my custom stack allocator, I allocate a large amount of memory at when
I'm a starting C/C++ programmer. What I want is the following thing: Start program,
I discovered very interested issue with XamDataGrid LoadCustomizations method. At start program I initialize
When I start the program, I get the dialog and everything. But it closes
i am trying to start the Program D from the terminal, command in terminal
Is it possible to start a program so that it is available to a

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.