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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:06:09+00:00 2026-06-10T16:06:09+00:00

I have 3 questions regarding the following function. I marked them as comments in

  • 0

I have 3 questions regarding the following function. I marked them as comments in code.

  1. Why do we copy char item pointed by dest (malloced memory) to dPtr, and then in the end (after processing the input string) return dest. Wouldn’t it be simpler to operate on dest.

  2. This function basically reverses the word only if it has dashes inside. Does the marked command *subword = '\0'; put a NUL character at the place of previously incremented place (in strncpy) – subword+1 or in the subword?

  3. Is this function prone to some buffer overflows or other undesired behaviour? Now it doesn’t give me any errors, but I think I just don’t know how to debug it.


len is strlen(word)

char* function(char* word, int len)
{
    char* subword = NULL;
    char* dest;
    char* dPtr;
    size_t n;

    dest = malloc(len+1);
    if (dest == NULL) return NULL;

    dPtr = dest;               /* [1] */

    while((subword = strrchr(word, '-'))!= NULL) {

        n = len - (subword-word) - (dPtr-dest);
        if (n-1 > 0)
            strncpy(dPtr, subword+1, n-1);

        *subword = '\0';      /* [2] */
        dPtr += n-1;
        *dPtr = '-';
        dPtr++;
    }
    strncpy(dPtr, word, len - (dPtr-dest));
    dest[len] = '\0';
    return dest;              /* [1] */
}
  • 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-10T16:06:11+00:00Added an answer on June 10, 2026 at 4:06 pm
    1. dPtr is being used to move through the string and modify it. dest is being kept as the beginning of that modified string, eventually the return value of the function. At the end of each loop iteration, dPtr will point to the end of the modified string, and used for each successive strncpy. Ultimately the function returns a modified string, and dest is needed to keep track of it’s beginning.

    2. subword is being null terminated presumably so that only characters in subword will be copied into the result in the strncpy within the loop, but this actually isn’t necessary with the use of strncpy. The function moves backwards through the string looking for dashes. For each hit, subword is the next dash-delimited substring. The substring is a piece of memory inside the original string, which isn’t null-terminated. If you were using strcpy then you’d need null-termination of subword, but since you’re using strncpy the null-terminators shouldn’t be necessary: just be careful with your n values as the strncpy limit. This code null-terminates subword in place, i.e. by sticking null characters inside the original string argument. Which leads to #3:

    3. As for undesired behavior, the function does it’s work by modifying the original string argument – it’s putting those null terminators for subword into the source string – even though it’s keeping it’s own buffer for the modified and returned string.

    Finally, the code could do without dPtr by using strncat operations rather than strncpy.

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

Sidebar

Related Questions

I have some questions regarding the following code: 1 class Test(object): 2 def __init__(self):
I have two questions regarding the following function. Obviously, it is about a chat.
I have some questions regarding the following css that I found: html, body {
I have seen many articles and Questions/Answers Regarding Lock Escalation but following things are
I have a question regarding the following code snippet I came across in one
I have some questions regarding Max() What does the following query mean? SELECT MAX(X)
I have a question regarding why the following returns inconsistent values (function(ab, $, undefined
I have 2 questions regarding different declarations of the same function and global variable
I have the following questions regarding BLOBs in sqlite: Does sqlite keep track of
My question is regarding JavaScript Closures and the Eval() function. I have some code

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.