Home > Software design >  Having some confusion with pointers
Having some confusion with pointers

Time:03-24

I have the following code that I'm trying to decipher from a former colleague:

void getIP(int p){
    char tstr[80];
    char *pt;
    strcpy(ipBuf,"NA");
    if(p==-1)return;
    
    strcpy(tstr, panes[p].stream.c_str());
    pt=strchr(tstr,':');
    if(pt){
        *pt='x'; //Blank first one
        pt=strchr(tstr,':');
        if(pt){
            *pt='\0';
            
            strcpy(ipBuf,&tstr[7]);
        }
    }
}

I'm relatively inexperienced with C so was hoping I could get some help with how this code works. Its purpose I think is to take a camera stream address and strip the port number and any extra stuff off to just give an IP address. I cannot understand through how it achieves this other than it seems to use ":" as a delimiter at a couple of stages?

To explain the function a little more, int p is a position on the grid, then it takes the stream address from that grid square and puts it into tstr.

But any explanation beyond that is much appreciated.

CodePudding user response:

As already noted in the previous comment, the function searches for the second occurence of ':' in tstr string and if it is found, replaces it with null character, effectively cutting off any remaining characters from the string. Than the characters in the string from index [7] to the null character (previously ':') are copied to ipBuf. For example, let say url is rtsp://192.168.0.200:551/stream.sdp. Digit 1 in "192" has index [7] in the string. So copying from that position to the second ":" (or null char) would copy "192.168.0.200".

CodePudding user response:

I [...] was hoping I could get some help with how this code works.

    strcpy(tstr, panes[p].stream.c_str());

Copy the contents of the std::string designated by panes[p].stream into array tstr, yielding an independent copy as a C string.

    pt=strchr(tstr,':');
    if(pt){
        *pt='x'; //Blank first one

Locate the first appearance of a ':' character in the local copy of the string. If the character is found, then replace it with an 'x'.

        pt=strchr(tstr,':');
        if(pt){
            *pt='\0';

Locate the (new) first appearance of a ':' character in the local copy of the string. If it exists, replace it with a '\0', which will be recognized as a string terminator. That is, truncate the string at that point.

            strcpy(ipBuf,&tstr[7]);

Copy the contents of the local C string, starting at the eighth character (because arrays are indexed from 0, not from 1), into the space to which ipBuf points. The magic number 7 is suspicious here, but I don't have enough information to be able to determine whether it is erroneous. My guess would be that the code is assuming that the original first colon will always appear at index 6, with the result that the substring between the (original) first and second colons is copied, but there are cleaner, clearer, more efficient ways to do that.

  • Related