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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:54:00+00:00 2026-06-02T21:54:00+00:00

gcc compiles the following code without error. I’m creating a bubble sort function that

  • 0

gcc compiles the following code without error. I’m creating a bubble sort function that can be used with arrays of any data type (hence the function pointer).

It sorts the array of character strings (arr2) without a problem, however, I can’t figure out why it won’t properly sort the array of integers (arr). I added a printf statement in the compare_long function to see what is going on. It doesn’t look like the integers are being passed to it properly. Any help will be greatly appreciated.


    #include <stdio.h>
    #include <string.h>

    #define MAX_BUF 256

    long arr[10] = { 3,6,1,2,3,8,4,1,7,2};
    char arr2[5][20] = { "Mickey Mouse",
                         "Donald Duck",
                         "Minnie Mouse",
                         "Goofy",
                         "Pluto" };

    void bubble(void *p, int width, int N, int(*fptr)(const void *, const void *));
    int compare_string(const void *m, const void *n);
    int compare_long(const void *m, const void *n);

    int main(void) {
            int i;
            puts("\nBefore Sorting:\n");

            for(i = 0; i < 10; i++) {              /* show the long ints */
                    printf("%ld ",arr[i]);
            }
            puts("\n");

            for(i = 0; i < 5; i++) {               /* show the strings */
                    printf("%s\n", arr2[i]);
            }

            bubble(arr, 4, 10, compare_long);      /* sort the longs */
            bubble(arr2, 20, 5, compare_string);   /* sort the strings */
            puts("\n\nAfter Sorting:\n");

            for(i = 0; i < 10; i++) {              /* show the sorted longs */
                    printf("%d ",arr[i]);
            }
            puts("\n");

            for(i = 0; i < 5; i++) {               /* show the sorted strings */
                    printf("%s\n", arr2[i]);
            }
            return 0;
    }

    void bubble(void *p, int width, int N, int(*fptr)(const void *, const void *)) {

            int i, j, k;
            unsigned char buf[MAX_BUF];
            unsigned char *bp = p;

            for(i = N - 1; i >= 0; i--) {

                    for(j = 1; j <= i; j++) {     

                            k = fptr((void *)(bp + width*(j-1)), (void *)(bp + j*width));

                            if(k > 0) {
                                    memcpy(buf, bp + width*(j-1), width);
                                    memcpy(bp + width*(j-1), bp + j*width , width);
                                    memcpy(bp + j*width, buf, width);
                            }
                    }
            }
    }

    int compare_string(const void *m, const void *n) {
            char *m1 = (char *)m;
            char *n1 = (char *)n;
            return (strcmp(m1,n1));
    }

    int compare_long(const void *m, const void *n) {
            long *m1, *n1;
            m1 = (long *)m;
            n1 = (long *)n;

            printf("m1 = %l and n1 = %l\n", *m1, *n1);

            return (*m1 > *n1);
    }
  • 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-02T21:54:01+00:00Added an answer on June 2, 2026 at 9:54 pm

    The ANSI C spec defines long as a MINIMUM of 4 bytes (32 bits) but GCC is defining long as 8 bytes in your case. It is architecture-specific so you need to use sizeof(long) or one of the C99 types like uint32_t or int32_t if you want a specific size.

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

Sidebar

Related Questions

The following code compiles without any warning or error on G++ (GCC) 4.1.2. Is
I have following piece of code: It compiles without problems under gcc-3.4, gcc-4.3, intel
The following code compiles without warnings under GCC 4.2, and as far as I
The following code compiles in Visual C++ and gcc, but fails with Code Warrior
The following code compiles with gcc 4.5.1 but not with VS2010 SP1: #include <iostream>
The following code compiles fine using MSVC 2008. When you build GCC climbs a
Possible Duplicate: What does main return? For example, the following code compiles without any
So the following code compiles with no warnings etc with gcc, but for some
I have the following code compiled by gcc: #include <iostream> using namespace std; class
The following code doesn't compile with gcc, but does with Visual Studio: template <typename

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.