1

So I have a program that needs to increment the value in the first array, in whatever position is set by the second array.

i.e: A user enters the number 5 into an array. The program then goes to that position (5) in the second array and increments it's value by one. So far the code works fine, but I have to use Pointer notation and not Subscript, and am struggling with the correct syntax. Here's the code:

void enter_into_frequency(int *frequency_temp, int *user_numbers_temp)
{
     int i;
     for(i=0; i<NO_OF_NUMS; i++)
     {
          //Frequency array, with position as whatever number the user entered
          frequency_temp[user_numbers_temp[i]]++; //Need this in pointer notation
     }
}
Mark Barrett
  • 366
  • 2
  • 16

2 Answers2

3

The code you need in pointer notation would be the following:

 ( *(frequency_temp + *(user_numbers_temp + i)) )++;

You can dereference a pointer using the *-operator and if you add an offset to your pointer you are able to dereference your array.

You can also have a look on this What does “dereferencing” a pointer mean?

Community
  • 1
  • 1
Frodo
  • 749
  • 11
  • 23
2
( *( frequency_temp + ( *(user_numbers_temp + i) ) ) )++;

This can be used in place of what you have right now.

/*  user_numbers_temp[i]  ->  *(user_numbers_temp+i)    */
/*  frequency_temp[user_numbers_temp[i]]  -> *(frequency_temp+(*(user_numbers_temp+i))) */

And then apply the ++ operator .

ameyCU
  • 16,489
  • 2
  • 26
  • 41