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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:52:54+00:00 2026-06-11T19:52:54+00:00

I wanted to implement a reverse function for a null-terminated char* string (null-terminated char

  • 0

I wanted to implement a reverse function for a null-terminated char* string (null-terminated char string, is that redundant?) and came up with the following solution.

The final swap swap(&tmp, &str) in the reverse function does not have any effect. What’s the reason, how is it done probably?

Remark: I have already reimplemented the reverse function by using only the string itself, a temporary char and indices only, but I am so interested in why this does not work. What haven’t I considered in this pointer matter?

#include <stdlib.h>
#include <stdio.h>

int strlen(char* str)
{
        int len = 0;
        char* ptr = str;
        while (ptr[0] != '\0')
        {
                len++;
                ptr++;
        }

        return len;
}

void swap(char **a, char **b)
{
        char* c = *a;
        *a = *b;
        *b = c;
}


void reverse(char* str)
{
        int len = strlen(str);
        char* tmp = malloc(sizeof(char) * (len + 1));
        int i;
        for (i = 0; i < len; i++)
        {
                tmp[i] = str[len - 1 - i];
        }

        tmp[len] = '\0';
        // printf(tmp); => "wolfrevOkcatS olleH"
        swap(&tmp, &str);
}

int main(int argc, char** argv)
{
        char* str = "Hello Stackoverflow\0";
        reverse(str);
        printf("%s", str);
        return 0;
}
  • 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-11T19:52:55+00:00Added an answer on June 11, 2026 at 7:52 pm

    You can just do an in place reverse, and you won’t need that pointer swap you’re doing:

    void reverse(char* str, int len)
    { 
            int i;
            for (i = 0; i < len; i++)
            {
                    char tmp = str[i];
                    str[i] = str[len - 1 - i];
                    str[len - 1 - i] = tmp;
            }
    }
    

    To answer your question, you need to pass a char** to your reverse function, because you’re changing what it will point to, and pointers are copied by value, so if you want to reflect the change of pointee you need a double pointer.

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

Sidebar

Related Questions

I wanted to implement my own debug function that has the same signature as
I wanted to implement an algorithm with Dictionary<Dictionary<char,int>, List<string>> to find the anagram words
I wanted to implement Comet in PHP and came across this page: http://www.zeitoun.net/articles/comet_and_php/start The
I wanted to implement an app that prevents calls like a firewall. When I
I wanted to implement a multi-select AutoCompleteExtender that displays a checkbox on each row.
I wanted to implement a windows service that captures dropped flat delimited files to
I wanted to implement a file search function in my Android application and I
So I've recently written an application that calculates tip and I wanted to implement
I wanted to implement a method in a abstract class that is called by
I wanted to implement a Node type like written below but I want to

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.