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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:37:42+00:00 2026-05-12T15:37:42+00:00

just brushing up on some C for a class and I’ve run across a

  • 0

just brushing up on some C for a class and I’ve run across a little something that makes me scratch me head. For this code:

char * findString(const char * s){
/* Allocate space */    
char * ret = malloc(strlen(s) + 1);    
/* Copy characters */
char * n;
n = ret;
for ( ;*s != 0; s++)
    if (isLetter(*s))
        *n++ = *s;
*n = 0;   
/* return pointer to beginning of string */
return ret;

}

(We’re just assuming an isLetter that returns a 1/0).

The idea of the snippet is to take a string with a bunch of crap in it, and return a string that contains only the letters.

So, how does ‘ret’ work in this instance? I’m very confused by the returning of ‘ret’ when ‘n = ret’ is declared above the for loop and ‘ret’ never gets set to anything afterwards. Obviously I’m missing something here. Help!

-R. L.

  • 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-12T15:37:43+00:00Added an answer on May 12, 2026 at 3:37 pm

    both ret and n are pointers to the same block of memory. their ‘values’ are simply memory addresses — when you change *n, you change *ret, even though n and ret retain their original values.

    //make n point to the beginning of the block of memory pointed
    //to by ret
    n = ret;
    
    //iterate through the string which was passed to
    //the function
    for ( ;*s != 0; s++)
            //if the current character is a letter:
            if (isLetter(*s))
                    //set the character pointed to by n to
                    //the current character in the string, and then
                    //make n point to the next one.
                    *n++ = *s;
    

    note that the loop increments n, and then after the loop sets the last character to 0 (to null terminate the string). Now, n points to the end of the string — but since ret was never changed it still points to the beginning of the memory that you malloced before the loop. When you return it, you’re returning a pointer to the new string which is the string you passed to the function, minus all non-letters.

    Note that after this function returns, it is the caller’s responsibility to free() the memory allocated by the function, lest ye roam into memory leaks.

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

Sidebar

Related Questions

No related questions found

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.