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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:50:41+00:00 2026-05-31T02:50:41+00:00

I new to C. I am reading a find-replace algorithm for C and I

  • 0

I new to C. I am reading a find-replace algorithm for C and I am a bit confused what the - & + operators do in this code:

char *replace(char * src, const char * search, const char * replace) {
   char * buffer = malloc(4096);  //allocate 4096 bytes in memory to new string
   char * p; //substring of my search in the src string
   int i;

   p = strstr(src, search);
   if ( NULL == p ) return src; if // 'search' not found on 'src' return src
      i =  p - src; //index of my substring

   strncpy(buffer, src, i); //copy the substring value to buffer
   sprintf(buffer + i, "%s%s", replace, 
   p + strlen(search)); // ???

   return buffer;
}
  • 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-31T02:50:42+00:00Added an answer on May 31, 2026 at 2:50 am

    Since p is a location in your character array (string) and src is the start of it,

    i = p - src;
    

    will set i to the index at which p points.

    For example, consider the following memory layout:

     [0] [1] [2] [3] [4] [5] [6] [7] [8]  [9]  <-- Indexes
     123 124 125 126 127 128 129 130 131  132  <-- Addresses
    +---+---+---+---+---+---+---+---+---+----+
    | H | i | , |   | w | o | r | l | d | \0 |
    +---+---+---+---+---+---+---+---+---+----+
      ^               ^
      |               |
     src              p
    

    In this case, p - src will give you 127 - 123 or 4, which is the index of the w within "Hi, world".

    This is called pointer arithmetic is covered in Additive operators in the ISO standard (C99 6.5.6/9):

    When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is the difference of the subscripts of the two array elements.

    It provides a scaled way of working out differences within the same array or with one pointing just beyond the end of the array (all else is undefined).

    By that I mean doing pointer arithmetic with (for example) four-byte integers, will give you a difference of one between the addresses of arr[7] and arr[8], not four as some may think.

    The buffer + i construct is simply another way of saying &(buffer[i]), the address of the ith element of the array buffer. I actually prefer the latter method since it seems more explicit in what I’m trying to represent.

    For what it’s worth, that’s not actually a very good string replacement code. It has numerous problems:

    • if no replacements are made, you have a 4K memory leak with buffer.
    • in any case, you should always check to ensure malloc hasn’t failed.
    • you have a possibility of buffer overflow the way the new string is allocated, you should really allocate based on the lengths of src search and replace.
    • you could create the new string with a single sprintf ("%*.*s%s%s", i, i, src, replace, &(src[i + strlen (search)])); or a strcpy and two strcat operations. Mixing the two seems incongruous to me.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to Linq, I can find multi-line data reading examples everywhere (by
I'm new to Java, and while reading documentation so far I can't find any
This question arose while reading the new chapter in the excellent Learn You a
I keep reading about how great this new Canvas element for HTML5 is and
Thanks for reading. I'm a bit new to jQuery, and am trying to make
From what I can tell from reading everything I can find here, this doesn't
Im sort of confused by it. The best I could find was reading through
New to WPF...was reading this WPF Routed Command with Bindings per-Tab and am close
I'm pretty new to template metaprogramming and can't find my thinking error in this
Im reading about the new Android Lint rules, and I find to prevent overdraw

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.