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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:47:30+00:00 2026-05-20T18:47:30+00:00

I’ve been studying C, and I decided to practice using my knowledge by creating

  • 0

I’ve been studying C, and I decided to practice using my knowledge by creating some functions to manipulate strings. I wrote a string reverser function, and a main function that asks for user input, sends it through stringreverse(), and prints the results.

Basically I just want to understand how my function works. When I call it with ‘tempstr’ as the first param, is that to be understood as the address of the first element in the array? Basically like saying &tempstr[0], right?

I guess answering this question would tell me: Would there be any difference if I assigned a char* pointer to my tempstr array and then sent that to stringreverse() as the first param, versus how I’m doing it now? I want to know whether I’m sending a duplicate of the array tempstr, or a memory address.

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

int main()
{
    char* stringreverse(char* tempstr, char* returnptr);

    printf("\nEnter a string:\n\t");

    char tempstr[1024];

    gets(tempstr);
    char *revstr = stringreverse(tempstr, revstr); //Assigns revstr the address of the first character of the reversed string.

    printf("\nReversed string:\n"
           "\t%s\n", revstr);

    main();
    return 0;
}

char* stringreverse(char* tempstr, char* returnptr)
{
    char revstr[1024] = {0};

    int i, j = 0;

    for (i = strlen(tempstr) - 1; i >= 0; i--, j++)
    {
        revstr[j] = tempstr[i]; //string reverse algorithm
    }

    returnptr = &revstr[0];
    return returnptr;
}

Thanks for your time. Any other critiques would be helpful . . only a few weeks into programming 😛

EDIT: Thanks to all the answers, I figured it out. Here’s my solution for anyone wondering:

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

void stringreverse(char* s);

int main(void)
{
    printf("\nEnter a string:\n\t");

    char userinput[1024] = {0}; //Need to learn how to use malloc() xD

    gets(userinput);
    stringreverse(userinput);

    printf("\nReversed string:\n"
           "\t%s\n", userinput);

    main();
    return 0;
}

void stringreverse(char* s)
{
    int i, j = 0;
    char scopy[1024]; //Update to dynamic buffer
    strcpy(scopy, s);

    for (i = strlen(s) - 1; i >= 0; i--, j++)
    {
        *(s + j) = scopy[i];
    }
}
  • 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-20T18:47:31+00:00Added an answer on May 20, 2026 at 6:47 pm

    First, a detail:

    int main()
    {
        char* stringreverse(char* tempstr, char* returnptr);
    

    That prototype should go outside main(), like this:

    char* stringreverse(char* tempstr, char* returnptr);
    
    int main()
    {
    

    As to your main question: the variable tempstr is a char*, i.e. the address of a character. If you use C’s index notation, like tempstr[i], that’s essentially the same as *(tempstr + i). The same is true of revstr, except that in that case you’re returning the address of a block of memory that’s about to be clobbered when the array it points to goes out of scope. You’ve got the right idea in passing in the address of some memory into which to write the reversed string, but you’re not actually copying the data into the memory pointed to by that block. Also, the line:

    returnptr = &revstr[0];
    

    Doesn’t do what you think. You can’t assign a new pointer to returnptr; if you really want to modify returnptr, you’ll need to pass in its address, so the parameter would be specified char** returnptr. But don’t do that: instead, create a block in your main() that will receive the reversed string, and pass its address in the returnptr parameter. Then, use that block rather than the temporary one you’re using now in stringreverse().

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.