Thursday 15 August 2013

c - Subtracting from pointer to get length -


I wanted to search the length of one part of the string after searching within a larger string.

I can not use strlen because I am working with binary data.

  char * temp = "This is some random text"; Four * temp1 = strstr (temp, "something"); Int len ​​= strlen (temp); Int len1 = 0; Len1 = temp + len - temp1;   

len1 returns negative values ​​(even its positive value is false) to get the length of "some random text"

Last reply :

You are looking for len - (temp1 -)

. The length of the first part is temp1 - temp .

Long answer :

Since strlen (which you used in your example, even if it's just the appropriate text Message works) until you can not find the \ 0 character, then you can only use strlen (temp1) to input For the length of the last part if you are really worried that by calling strlen will harm your performance (actually?), Then you len - (temp1 - temp) .

If you are interested in the length of the first part of the input, you only have to point the object.

If you want to work with a binary array in which they have \ 0 in the non-terminal state you do not not strlen in your code , however, you should have a way to specify the length of the entire input. Either you have it in an integer variable or you have a specific delimiter which is a length-computing function.

If you have an integer variable for length, then after the length of the first part of the input, by the pointer object, you should only do lane - (temp1 - temp) Does matter.

If you have a long-computing function, then it should only be used as a temp1 argument.

PS : Do not forget to check that the strstr faucet returns (by the way, you can not use the strstr If you have binary data inside the buffer)

No comments:

Post a Comment