4

This is a simple function for finding occurrences of a specific letter.

1:  int count_x(char *p, char a)
2:  {
3:      int count = 0;
4:      while(*p != '\0')
5:      {
6:        if (*p == a)
7:            count++;
8:        p++;
9:      }
10:      return count;
11:  }

We can get access to a specific element by using p[n], or we can dereference it *p and get a first element of that array as an example, and all of that stuff we usually do.
The strange thing to me is located at the line number 8. When we write p++, we are getting the array we passed -1 symbol from the beginning. So if it was hello, world then it would be ello, world. We are somehow iterating throuhg the indices but i don't really understand how.

Can i have an explanation of how all of that stuff works?

sodaluv
  • 449
  • 2
  • 9
  • 22

3 Answers3

5

The loop condition *p != '\0' means: *iterate until the value pointed by p is '\0'. The statement inside the loop body

p++;

increments the pointer to next character of the string passed.

For first iteration

+--------+--------+--------+--------+--------+--------+--------+--- ----+--------+--------+--------+--------+
|        |        |        |        |        |        |        |        |        |        |        |        |
|  'h'   |  'e'   |  'l'   |  'l'   |  'o'   |  ','   |  'w'   |  'o'   |  'r'   |  'l'   |  'd'   |  '\0'  |
|        |        |        |        |        |        |        |        |        |        |        |        |
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
  ^
  |       
  p   

Second iteration :

+--------+--------+--------+--------+--------+--------+--------+--- ----+--------+--------+--------+--------+
|        |        |        |        |        |        |        |        |        |        |        |        |
|  'h'   |  'e'   |  'l'   |  'l'   |  'o'   |  ','   |  'w'   |  'o'   |  'r'   |  'l'   |  'd'   |  '\0'  |
|        |        |        |        |        |        |        |        |        |        |        |        |
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------+
             ^
             | 
             p

and so on. When p comes to point '\0', condition *p != '\0' becomes false and loop terminates. On every iteration it is the pointer p which changes the location where it points. String remain at its initial stored location.

haccks
  • 104,019
  • 25
  • 176
  • 264
1

TL:DR - Using a pointer to the array, we iterate through the individual elements, just the same as we do using array indexing.


When you pass an array to a function, it decays to the pointer to the first element. So, char *p points to the first element in the array.

In this case, the function expects a null-terminated char array (or, pointer to the first element of a null-terminated char array) as the first argument.

Once you increment p, it points to the next element. That's why, if you try to print the incoming string, (actually "hello, world"), it now prints "ello, world".

Also, by checking *p != '\0' you're making sure you don't go past the array (null-terminated).

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • ok, so what's wrong with this answer, please? – Sourav Ghosh Sep 02 '16 at 12:34
  • In my opinion it is a correct answer and I have up-voted it. – Vlad from Moscow Sep 02 '16 at 12:39
  • Once you increment p, it points to the next element. Yeah, i got it. My point was to firure out how exactly all of that works? When we're saying p++, what exactly do we increment? – sodaluv Sep 02 '16 at 12:42
  • @Caleb we increment the pointer. a pointer is an address pointing to some _object_. Now, once that _object_ is an array, it's elements are in contiguous memory locations. So, when we increment the pointer, it points to the address of the next element. – Sourav Ghosh Sep 02 '16 at 12:44
  • @VladfromMoscow Appreciate it, sir. However, I was more interested in getting a comment from the downvoter. At times, this hit-and-run DVs are quite frustrating.... _sigh_ ... – Sourav Ghosh Sep 02 '16 at 12:46
  • @SouravGhosh; These are the *ghost souls* buddy. No one can catch them :\ – haccks Sep 02 '16 at 12:51
0

Expression s[i] (where s is a character array) is evaluated the following way: s is converted to pointer to its first element and the so-called pointer arithmetic is applied. Then the result pointer is dereferenced.

So you can imaging s[i] the following way

char *p = s;
int i = 0;
p = p + i;
//...
if ( *p == a ) count++;

To get the next character you could write

++i;
p = p + i;
//..

However this is equivalent to

++p;
p = p + i;

where i has its previous value.

So you can increase either the index of the pointer. Now just assume that i is always equal to 0.

Then you can write

char *p = s;
int i = 0;
p = p + i;
//...
if ( *p == a ) count++;
++p;
p = p + 0;
if ( *p == a ) count++;
// and so on
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335