-1

This program is supposed to count number of similar entries, using pointers, but whenever I type in number, counter is always equal to zero. What am I going wrong?

#include <stdio.h>
#include <stdlib.h>

//using function to count similar enteries in an array...
void count_similar_enteries(int array_func[10],int *number, int *ptr_to_counter);

int main()
{
    int number = 0;
    int array[10] = {0,1,1,2,3,1,2,67,65,1};
    int counter = 0;
    printf("enter a number\n");
    scanf("%d", &number);
    count_similar_enteries(array, &number,&counter);
    printf("the number of similar enteries are %d\n", counter);
    return 0;
}

void count_similar_enteries(int array_func[10],int *number, int *ptr_to_counter)
{
    int i;
    for (i = 0; i< 10 ; i++)
    {
        if(array_func[i] == *number)
        {
            *ptr_to_counter++;
            continue;
        }
        else
        {
            continue;
        }
    }   
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • 2
    @user3151918 That's a bad habit. A good C/C++ programmer will work to reduce pointer usage as much as possible (e.g. using references). It's *much* safter. That's also why modern languages like Java and C# don't have pointers at all. – Dai Jan 05 '14 at 07:38
  • 1
    @user3151918 - Why make life more difficult than it needs to be. Also pass number by value (do not need to pass a pointer to it and also enables one to use a constant). Also the `continue` is not required – Ed Heal Jan 05 '14 at 07:41
  • Perhaps a better signature for the function is `int count_similar_enteries(int *arr, size_t len, int compare_to)` – Ed Heal Jan 05 '14 at 07:49
  • @Dai, what is C/C++ and what is a "good" C/C++ programmer? The case being. this is a C only question and C doesn't have references. What are you talking about? – Jens Gustedt Jan 05 '14 at 09:02
  • 1
    Does this answer your question? [increment value of int being pointed to by pointer](https://stackoverflow.com/questions/3655728/increment-value-of-int-being-pointed-to-by-pointer) – cigien May 15 '23 at 09:32

5 Answers5

7

*ptr_to_counter++; is incrementing the pointer itself, write (*ptr_to_counter)++; to increment the value of the ptr_to_counter.

Note that continue; is redundant. You don't have to have if-else there. The loop will be "looped" anyway. You can just do:

for(i = 0; i< 10 ; i++) {
   if(array_func[i] == *number) {
      (*ptr_to_counter)++;
   }
}

I advise you to reduce the amount of pointers as much as you can, for example, you don't have to pass a counter by pointer. You can just have a local int counter; and return its value to the caller.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • Does this actually work and update the memory that `ptr_to_counter` points to? – Ed Heal Jan 05 '14 at 08:03
  • @EdHeal As he wrote it doesn't change the value in the memory, it just move the pointer to point to the next address (pointers arithmetic). After he do the changes I tell him to, the **value** will be changed and the pointer will not. – Maroun Jan 05 '14 at 08:16
  • @MarounMaroun - Your right. Had to reread it. I do not think that was the intention of the function thou. – Ed Heal Jan 05 '14 at 08:27
  • @EdHeal He wanted to increment the value pointed to by the pointer (Which is really redundant - He can have a local counter instead). – Maroun Jan 05 '14 at 08:29
2

You are incorrectly incrementing counter value at address ptr_to_counter. You have use *ptr_to_counter++ which means *(ptr_to_counter++) i.e. it is increamenting address not the value. You should use (*ptr_to_counter)++. It will work perfectly.

Cool Goose
  • 870
  • 10
  • 16
1

I think you require

*ptr_to_counter = *ptr_to_counter + 1;

EDIT

Perhaps this would be a better implementation

int count_similar_enteries(int arr, size_t len,int number)
{ 
    i;
    int count = 0;
    for (size_t i = 0; i<len ; ++i)
    {
        if(arr[i] == number)
        {
            ++count;
        }
    }
    return count;
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

If you are not sure about operator precedence please use brackets, it will also increase readability of your code.

*ptr_to_counter++ will work like *(ptr_to_counter++) because ++ has higher precedence. You should have used (*ptr_to_counter)++

0

*ptr_to_counter++

means

*(ptr_to_counter++)

which means increment the address holed by ptr_to_counter, which is taking your pointer to the adjacent address where nothing is stored(or may be garbage).

You should use (*ptr_to_counter)++ meaning increment the value pointed at by ptr_to_counter.

batman
  • 267
  • 2
  • 11