什么是 - 运营商做的char *? [英] what does the - operator do with char *?
问题描述
我刚接触C.我读对C一个查找替换算法和我有点困惑是什么 -
&安培; +
运营商在这种code做的:
的char *取代(的char * SRC,为const char *搜索,为const char *代替){
字符*缓冲=的malloc(4096); //在内存中新字符串分配4096个字节
字符* P; //我在src字符串搜索的字符串
INT I; P =的strstr(SRC,搜索);
如果(NULL == p)的回报SRC;如果//'搜索'对'src'中的回报SRC未找到
我= P - SRC; //我的子指数 函数strncpy(缓冲液,SRC,ⅰ); //复制子值缓冲
sprintf的(缓冲+ I,%s%S,更换,
P + strlen的(搜索)); //? 返回缓冲区;
}
由于 P
是在你的字符数组(字符串)和 SRC的位置
是它的开始,
I = P - SRC;
将设置 I
来的索引, P
分。
例如,请考虑以下的内存布局:
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]&下; - 索引
123 124 125 126 127 128 129 130 131 132 LT; - 地址
+ --- + --- + --- + --- + --- + --- + --- + --- + --- + ---- +
| H |我| ,| | W | Ø| - [R |升|开发| \\ 0 |
+ --- + --- + --- + --- + --- + --- + --- + --- + --- + ---- +
^^
| |
SRC p
在这种情况下,对 - SRC
会给你 127 - 123
或 4
,这是中的是W
指数你好,世界
。
这就是所谓的指针运算在覆盖加法运算符
ISO标准( C99 6.5.6 / 9
):
当两个指针相减,两个人都要指向同一个数组对象,或者一个过去的数组对象的最后一个元素的元素;结果是两个数组元素的下标之差
块引用>它提供了锻炼的同一阵列内或与一个指向刚刚超越阵列末端的缩放方式有所不同的 的(一切是不确定的)。
我的意思是做指针运算与(例如)四字节整数,会给你一个差的有一个
的地址之间
的ARR [7] 和改编[8]
,而不是4因为有些人可能认为。的
缓冲区+ I
结构是说的只是另一种及(缓冲[I])
中,在I
日数组缓存
元素的地址。我其实preFER后一种方法,因为它似乎在什么,我试图重新present更加明确。有关它的价值,这实际上不是一个很好的字符串替换code。它有许多的问题:
- 如果没有替代品制成,您有
一个4K内存泄漏缓存
。- 在任何情况下,你应该的总是的检查,确保
的malloc
并没有失败。- 您有缓冲区溢出的可能性,新的字符串的分配方式,你应该根据
的src
搜索长度<分配/ code>和
替换
。- 您可以创建一个
sprintf的(新的字符串,我,我,SRC,替换和放大器%* * S%s%S。(SRC [I + strlen的(搜索)));
或的strcpy
和两个strcat的
操作。混合这两种似乎格格不入给我。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; }
解决方案Since
p
is a location in your character array (string) andsrc
is the start of it,i = p - src;
will set
i
to the index at whichp
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 you127 - 123
or4
, which is the index of thew
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]
andarr[8]
, not four as some may think.The
buffer + i
construct is simply another way of saying&(buffer[i])
, the address of thei
th element of the arraybuffer
. 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
andreplace
.- you could create the new string with a single
sprintf ("%*.*s%s%s", i, i, src, replace, &(src[i + strlen (search)]));
or astrcpy
and twostrcat
operations. Mixing the two seems incongruous to me.这篇关于什么是 - 运营商做的char *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!