0

Can some one explain me the working of the

  • for loop having the condition *(str+i)
  • till what point will the loop execute
  • what is the difference between count[i++] and count[i]++

      void fillCharCounts(char *str, int *count)
      {
        int i;
        for (i = 0; *(str+i);  i++)
        {
          count[*(str+i)]++;
        }
      }
    
Phong
  • 6,600
  • 4
  • 32
  • 61
navroze
  • 35
  • 1
  • 6

2 Answers2

1

Well the code counts and stores the frequency of each alphabet of str to the integer array count. After the execution of this function, count[index] has the frequency of str[index]. Let me explain what you want to know:

For Loop having the condition *(str+i):

*(str + i) is equal to str[i], it refers to the i-th character of str. character pointer's value is incremented to access the address of next index and then the address is dereferenced to get the index value which is now being pointed by the pointer str.

Till what point will the Loop execute:

Loop will continue for each *(str+i) until it finds a Null Terminator at *(str+i) i.e. assume the following string:

char str[] = "abc";

what is stored in str is now abc\0 so the loop will be executed until it reaches the \0. The condition *(str+i) is basically equal to *(str+i) != '\0';

What is the difference between count[i++] and count[i]++ :

  • count[i++] is equal to *(count + i++) that imposes a Post Increment to the address stored in count before using it. That means the value of (count+i) is incremented by one and count starts pointing to the next index, once the statement is executed.
  • count[i]++ is equal to *(count + i)++ that imposes a Post Increment to the value stored in the index pointed by (count + i). That means after the execution of this statement, the value of *(count+i) is incremented by one BUT the count is still pointing to the current index.

In Your Case the statement used in the loop is:

count[*(str+i)]++;

By applying the Order of Precedence

  1. First of all *(str+i) will be executed that will give the value stored at the address which is being pointed by (str+i)
  2. Now that value will be used as an index of count and a Post Increment will be imposed to count[index] that means the value of *(count+index) is incremented by one after the execution of the above statement.

Hope its pretty much cleared now.

Community
  • 1
  • 1
Itban Saeed
  • 1,660
  • 5
  • 25
  • 38
  • I pretty much got it but just to wrap it up suppose I have a string mom where m=index[0] o=index[1] and m=index[2] count[*(str+i)]will point to the base address of the string initially.Now this is the part where i get a bit jumbled up.After *(str+i) is evaluated what will be its value will it be an index or the value m and after it has been evaluated what will be incremented.Please explain me in the context of the string mom that i have provided. – navroze Aug 11 '15 at 14:48
  • For `str = "mom"` lets access the `count[*(str+i)]`, `( )` will be evaluated first, at that time str is a pointer to the first index of `"mom"` hence it is holding the address of m, for `i = 0` the `str+i` means `str+0` so the resultant value is again the address of `m`.`*` is now applied, it will give the value stored at `str+i` that is `m` in our case. Remember that `[ ]` operator only takes integer so it will try to [convert](http://stackoverflow.com/questions/868496/how-to-convert-char-to-integer-in-c) `m` into an integer which will result into using its`ASCII` as an index of `count`. – Itban Saeed Aug 11 '15 at 15:53
  • Oh now its clear it converts it into the ASCII value that is where i was confused.Thanks for the help appreciate it :) – navroze Aug 11 '15 at 16:20
0

This code snippet counts the number of appearances of each character (excluding the terminating null character) found in str and stores them to count. When the function returns, count acts as a character-to-number-of-appearances map. For example, count['a'] refers to the number of appearances of 'a' in str. *(str + i) is equal to str[i], which references the i-th character in the C-style string str. So the loop continues until the end of the string referenced by str (i.e., loop until the terminating null character '\0', which evaluates to false in a Boolean-context). count[*(str+i)]++; is equivalent to count[str[i]]++;, which increments the count (number of appearances) of character str[i] by one. The difference between count[i++] and count[i]++ is irrelevant to this code snippet.

Lingxi
  • 14,579
  • 2
  • 37
  • 93