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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:00:12+00:00 2026-05-23T08:00:12+00:00

Introduction I have an algorithm which takes a pointer to a char array. The

  • 0

Introduction

I have an algorithm which takes a pointer to a char array. The algorithm first retrieves the length of the array then reverses the array.

The problem

The problem I have is that I want to use this on a wchar_t array. And want to be able to do this without having to copy the whole function, change the name and the type of the argument.

Here is the mentioned function:

void reverseString(char *str){
  unsigned int l = getStringLength(str);
  int i = 0;
  int m = l >> 1;

  while(i < m){
    str[i] ^= str[l - 1];
    str[l - 1] ^= str[i];
    str[i] ^= str[l - 1];
    i++;
    l--;
  }
}

From googling and reading on SO this won’t be able to use a void pointer (conceptually same thing using a union) since it would leave me with a solution like this, which to me is equally bad as writing separate functions but with different names and argument types:

void reverseString(void *array, short typeSize){
  unsigned int l = getArrayLength(array);
  int m = l >> 1;
  int i = 0;
  char *str = 0;
  wchar_t *wstr = 0;

  if(typeSize == 1){
    str = (char *) array;
    while(i < m){
      str[i] ^= str[l - 1];
      str[l - 1] ^= str[i];
      str[i] ^= str[l - 1];
      i++;
      l--;
    }
  }else if(typeSize == 4){
    wstr = (wchar_t *) array;
    while(i < m){
      wstr[i] ^= wstr[l - 1];
      wstr[l - 1] ^= wstr[i];
      wstr[i] ^= wstr[l - 1];
      i++;
      l--;
    }
  }
}

Note: getStringLength is just a function which loops through the pointer till it gets to '\0' and returns the iteration sum.

The answer

I am looking for an answer which tells me how to do this in a nicer way without having to rewrite the internals of the algorithm, or an answer saying that it won’t be possible to do it any other way. I’m not looking for an answer telling me I should use this and that library which does this for me, because I’m not using this in production code, it’s purely educational to get a better understanding of how memory management works and other concepts alike.

Edit: The function I showed is just an example, I’m looking for a universal solution to problems with algorithms alike.

  • 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-23T08:00:13+00:00Added an answer on May 23, 2026 at 8:00 am

    Using “generics” in C is likely to produce code that is noticeably slower and more convoluted / difficult to read / difficult to maintain than the original code. Use the preprocessor if you must do this.

    My recommendation is to avoid this technique if at all possible: you should really only use char or wchar_t in your program, not a mixture of both! (char or UChar or almost universally preferable since you can choose the encoding, but I digress…)

    #define gchar char
    #define gstrlen strlen
    #define func_name reverse
    #include "reverse_impl.h"
    #undef gchar
    #undef gstrlen
    #undef func_name
    
    #define gchar wchar_t
    #define gstrlen wstrlen
    #define func_name wreverse
    #include "reverse_impl.h"
    #undef gchar
    #undef gstrlen
    #undef func_name
    

    Then, in reverse_impl.h:

    void func_name(gchar *str)
    {
        gchar *p = str, *q = str + gstrlen(str), t;
        if (p == q)
           return;
        q--;
        for (; p < q; p++, q--) {
            t = *p;
            *p = *q;
            *q = t;
        }
    }
    

    Also, DO NOT DO THIS:

    x ^= y; // bad!
    y ^= x;
    x ^= y;
    

    It is more difficult to read and quite possibly much slower to execute.

    Also, note that both reverse and wreverse will make garbage if you give them Unicode input: reverse will make malformed output and wreverse can switch the diacritics around or totally screw up Hangul, depending on how they’re represented.

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

Sidebar

Related Questions

i have following problem from book introduction algorithm second edition by MIT university problem
I have a section: \section{Introduction} \label{sec:introduction} I'd like a link to the section where
Introduction In my current organisation, we have many desktop and web applications all feeding
I have just started the MIT Introduction to Algorithms course through the material posted
Recently I have read about hash-tables in a very famous book Introduction to Algorithms
You have a biased random number generator that produces a 1 with a probability
I have seen Apple's example TableSearch that when touched its scope buttons come below
I have an application that reads/writes from/to message queues on remote application servers. The
I have a tab bar application and I want to display those views that
I have a function which wanders through the elements in a page and adjusts

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.