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?

1
  • 2
    we are getting the array we passed -1 symbol from the beginning. What does that mean? Commented Sep 2, 2016 at 12:30

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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).

6 Comments

ok, so what's wrong with this answer, please?
In my opinion it is a correct answer and I have up-voted it.
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?
@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.
@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 ...
|
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.