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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:04:40+00:00 2026-05-27T11:04:40+00:00

how to do a lexicographic compare between two pointers with unknown byte size, assuming

  • 0

how to do a lexicographic compare between two pointers with unknown byte size,
assuming both array size is equal,
eg implementing the following function to return -1 or 0 or 1 based on the comparecement and converting each byte to ascii

compare(const void *p1, const void *p2, int size)

EDIT: i added another argument with the size of bytes to compare, how can i cast each byte to char and compare between them ?

  • 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-27T11:04:41+00:00Added an answer on May 27, 2026 at 11:04 am

    You’re probably looking for the strcmp(3) function.

    Update

    If your arrays are not of ASCII strings and may contain the 0x00 byte at any position without actually meaning anything, then you’ll need the length as Oli points out, so you can use the memcmp(3) function:

    int memcmp(const void *s1, const void *s2, size_t n);
    

    Update #2

    Okay, now that we’ve established you also want the byte that differs, you’ll need your own function. Something like this (untested):

    size_t cmp(const unsigned char *c1, const unsigned char *c2, ssize_t n) {
        ssize_t i;
        for (i=0; i<n; i++) {
            if (c1[i] != c2[i])
                return i;
        }
        -1;
    }
    

    This will let you find the byte values for whichever bytes differ. Note the -1 return in case of no difference. This limits you to comparing objects whose size fit entirely in a ssize_t, which is smaller than the size_t type — but since you’re comparing two of them, I figure each one gets half the process address space at most anyway.

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

Sidebar

Related Questions

I want to compare an array of comparables. The simplest way seems the following
I've got following problem with gawk's asorti function: gawk 'BEGIN{ \ a[1]=6; \ a[2]=7;
I am trying to solve an NX2 array of all integers in lexicographic order.
I am implementing the complex algorithm which part is sorting array of ordered sequences
I'm trying to create binary tree that contains two int values and one string
A list of n strings each of length n is sorted into lexicographic order
I'm looking for an algorithm or function that is able to map a string
I have a dictionary in an unknown language. I have to find all the
I have the following code, which splits up a Vector into a string vector
I have the following (correct) solution to Project Euler problem 24. I'm relatively new

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.