Forum


Notice to all users

We are migrating towards a new forum system located at community.teamspeak.com, as such this forum will become read-only on January 29, 2020

Results 1 to 4 of 4
  1. #1
    Join Date
    October 2015
    Location
    Austin
    Posts
    17

    C++ Pointer Iteration Help

    Hi, I'm currently working with creating TeamSpeak3 Plugins, and I found a loop in the example that troubled me:
    Code:
            char* s;
            char msg[1024];
            anyID myID;
            uint64* ids;
            size_t i;
    	unsigned int error;
    /* Print list of all channels on this server */
            if(ts3Functions.getChannelList(serverConnectionHandlerID, &ids) != ERROR_ok) {
                ts3Functions.logMessage("Error getting channel list", LogLevel_ERROR, "Plugin", serverConnectionHandlerID);
                return;
            }
            printf("PLUGIN: Available channels:\n");
            for(i=0; ids[i]; i++) {
                /* Query channel name */
                if(ts3Functions.getChannelVariableAsString(serverConnectionHandlerID, ids[i], CHANNEL_NAME, &s) != ERROR_ok) {
                    ts3Functions.logMessage("Error querying channel name", LogLevel_ERROR, "Plugin", serverConnectionHandlerID);
                    return;
                }
                printf("PLUGIN: Channel ID = %llu, name = %s\n", (long long unsigned int)ids[i], s);
                ts3Functions.freeMemory(s);
            }
            ts3Functions.freeMemory(ids);  /* Release array */
    You can see the line
    Code:
    for(i=0; ids[i]; i++)
    while ids is a uint64* pointer and i is just a size_t.
    My question is, what exactly is this doing?


    I'm sorry if the answer is incredibly obvious.

  2. #2
    Join Date
    October 2015
    Location
    Kansas, USA
    Posts
    2
    Code:
            for(i=0; ids[i]; i++) {
                /* Query channel name */
                if(ts3Functions.getChannelVariableAsString(serverConnectionHandlerID, ids[i], CHANNEL_NAME, &s) != ERROR_ok) {
                    ts3Functions.logMessage("Error querying channel name", LogLevel_ERROR, "Plugin", serverConnectionHandlerID);
                    return;
                }
                printf("PLUGIN: Channel ID = %llu, name = %s\n", (long long unsigned int)ids[i], s);
                ts3Functions.freeMemory(s);
            }
    I thought this was simple, too, at first glance. Now I see what you mean. Simply put, it's cycling through the "ids" array one slot at a time, but without knowing what all these functions do, I can't exactly say when it stops. (Though it could be when getChannelVariableAsString == ERROR_ok, since the last line that condition executes is "return;" but I've only ever seen people use "break" to break out of a loop.)

    I'd be interested to see what someone more well-versed has to say, but hopefully my semi-educated guess helped.

  3. #3
    Join Date
    September 2012
    Posts
    6,079
    You may know for loops as in
    Code:
    for (int i = 0; i < 10; ++i) {
        std::cout << i << std::endl;
    }
    which will simply run for as long as i is smaller than 10. So the center part of the for loop is like "if (i < 10) {runloop}" and if the condition doesn't match (equals to false) it will stop running the loop.

    It probably helps to know that the call to getChannelList allocates memory for the uint64* and then just keeps writing the channel Ids into the allocated memory, followed by an additional 0 terminator.

    With that for loop from the example, it'll be "if (channel ID located at index i of the ids array) { runloop}". In C and C++ integers are convertible to bool, which anything but 0 being true. As such the loop will run for as long as there is a number at the current index that is != 0, which (since channel IDs are always > 0) will be true for as long as there are channels. It's also why getChannelList wrote a 0 at the end. That zero will cause the loop the stop and continue with whatever code comes after it.

    Since the memory got allocated in the clientlib and not your code you need to call the freeMemory function to free the memory.
    When sending PMs please make sure to include a reference link to the thread in question in the body of your message.

  4. #4
    Join Date
    October 2015
    Location
    Austin
    Posts
    17
    Thanks for the responses(and sorry for being late) they were both great and they both helped!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. mouse pointer to corner
    By Cooper262 in forum Bug Reports [EN/DE]
    Replies: 1
    Last Post: September 27th, 2013, 07:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •