Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing a string(pointer question...) 1

Status
Not open for further replies.

Susan

Programmer
Nov 16, 1998
4
IE
I have a simple function which removes brackets from a given phone number.<br>Eg: If the phoned number passed is (000) 123 4567 then the phone number returned is 000 123 4567.<br><br>The function goes as follows:<br>int RemoveBrackets(char *PhoneNumber)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;&nbsp;&nbsp;Result = 0;<br>&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;&nbsp;*OpenBracket&nbsp;&nbsp;= NULL,<br> &nbsp;&nbsp;*CloseBracket = NULL,<br> &nbsp;&nbsp;*BeginPhNum&nbsp;&nbsp;&nbsp;= NULL,<br> &nbsp;&nbsp;*EndPhNum&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= NULL;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;/* Look for the opening bracket. */<br>&nbsp;&nbsp;&nbsp;&nbsp;OpenBracket = (char *)strstr(PhoneNumber, &quot;(&quot;);<br>&nbsp;&nbsp;&nbsp;&nbsp;if(OpenBracket != NULL)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br> BeginPhNum = ++OpenBracket;<br> <br> /* Look for the closing bracket.*/<br> CloseBracket = (char *)strstr(PhoneNumber, &quot;)&quot;);<br> if(CloseBracket != NULL)<br> {<br> &nbsp;&nbsp;&nbsp;&nbsp;EndPhNum = ++CloseBracket; <br>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;CloseBracket = --CloseBracket; <br>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;*CloseBracket&nbsp;&nbsp;= '\0';&nbsp;&nbsp;<br> &nbsp;&nbsp;&nbsp;&nbsp;<br> &nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;EndPhNum: %s\n&quot;, EndPhNum);<br><br> &nbsp;&nbsp;&nbsp;&nbsp;strcat(BeginPhNum, EndPhNum);<br><br> &nbsp;&nbsp;&nbsp;&nbsp;strcpy(PhoneNumber, BeginPhNum);<br><br> &nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;PhoneNumber(out): %s\n&quot;, PhoneNumber);<br> &nbsp;&nbsp;&nbsp;&nbsp;<br> }<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>return(Result);<br>}<br><br><br>It works correctly, returning the phone number without the brackets but I don't actually understand how the contents of 'EndPhNum' are not null/blank after 'CloseBracket' has been nulled...? I khow it's something to do with the line CloseBracket = --CloseBracket; as when this is taken out EndPhNum is then blanked after *CloseBracket = '\0';<br>However when CloseBracket = --CloseBracket; is left in then somehow the contents of EndPhNum are preserved....<br><br>Much appreciated if anyone could shed some light on this ...
 
hi susan!<br><br>let's try to trace this:<br><br>string: &quot;(000) 123 4567\0&quot;<br><br>CloseBracket = (char *)strstr(PhoneNumber, &quot;)&quot;);<br>-&gt;CloseBracket points to &quot;)&quot;<br><br>EndPhNum = ++CloseBracket;<br>-&gt;CloseBracket points to the blank<br>-&gt;EndPhNum points to the blank<br><br>CloseBracket = --CloseBracket; <br>-&gt;CloseBracket points to &quot;)&quot; again<br>-&gt;EndPhNum points to the blank<br><br>*CloseBracket&nbsp;&nbsp;= '\0';&nbsp;&nbsp;<br>-&gt; &quot;)&quot; is replaced by &quot;\0&quot;<br>-&gt; EndPhNum points to the blank<br><br>that's it. EndPhNum starts one character after the \0.<br>if you don't decrease CloseBracket EndPhNum points to<br>the same position (which is set to \0 later).<br><br>-- ralf<br>
 
thanks ralf,&nbsp;&nbsp;but how come 'BeginPhNum' only contains the initial 000 after *CloseBracket = '\0'; has been executed(ie: everything after ')' has been blanked, it was '000) 123 4567') but 'EndPhNum' still contains it's original details....???<br><br>susan
 
Hei Susan! <br><br>I guess you have a small missunderstanding of what '\0' does... <br><br><b>'\0' </b> represents actually a marker that represents the end of a string by replacing ')' with the string terminator. <br>By setting <i>CloseBracket</i> to '\0' you just marked the end of the BeginPhNumber string (confusing?).<br><br><b><font color=red> But the actual string is still in the memory </b></font>, that assignement doesnt &quot;blank&quot; (as you put it) the string, whose end part is hold by <b>EndPhNum </b>.<br><br>Imagine the string as an vector array and the pointers (CloseBracket,EndPhNum ...) as indexes in the array. The operations you made on the string are somehow equivalent to PhoneNumber[CloseBracket] = '\0', for example (don't use this as a code, OK?, i hope it was a good enough example)<br><br>If you want to &quot;blank&quot; your string, you have to do somethin like free(EndPhNum), which will free the memory contained by the string.<br><br>To be more clearer try the code:<br>&nbsp;&nbsp;&nbsp;strcpy(EndPhNum,&quot;&quot;); // setting it to the empty string, right?<br>&nbsp;&nbsp;&nbsp;EndPhNum++;<br>&nbsp;&nbsp;&nbsp;printf(&quot;%s&quot;,EndPhNum);&nbsp;&nbsp;<br><br>Have i brought you some light?
 
Thanks very much for that - I understand exactly what's going on now! You're right - I did misunderstand what '\0' does but it's clear now.<br><br>Thanks again...<br><br>Susan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top