2

The functions should use pointer arithmetic (instead of array subscripting). In other words, eliminate the loop index variables and all use of the [] operator in the functions.

void set_complement(int *a, int n, int *complement)
    {
        //pointers and variables declaration
        int i;
        int *Pa = a;
        int *Pc = complement;

        for(i = 0; i < n; i++)
            {
                if( *(Pa + i) == 0)
                    {
                        *(Pc + i) = 1;
                    }
            }
    }

My question is: Am I using pointer arithmetic in the for-loop?

12
  • Which language are you programming in, C or C++? In C++ you can pass parameters by reference and remove the need for pointers. Commented Oct 1, 2017 at 22:48
  • I am using C language! Commented Oct 1, 2017 at 22:48
  • 2
    Your tags say C and C++. Choose one. Commented Oct 1, 2017 at 22:49
  • 1
    Just for your information, for any pointer (or array) p and index i, the expression *(p + i) is exactly equal to p[i]. Commented Oct 1, 2017 at 22:49
  • 1
    @mksteve Yes it is. Commented Oct 1, 2017 at 22:51

3 Answers 3

1

In a word - yes. By adding ints to pointers you're effectively moving the pointer (and then dereferencing it), or, in other words - you are performing pointer arithmetic.

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

Comments

1

Yes, you are using pointer arithmetic. But you not eliminated loop index variable so you could write something like:

void set_complement(int *a, int n, int *complement)
{
    //pointers declaration
    int *Pa = a;
    int *Pc = complement;
    int *Pend = a + n;

    for (; Pa != Pend; ++Pa, ++Pc)
    {
        if(*Pa == 0)
        {
            *Pc = 1;
        }
    }
}

Comments

0

Yes, essentially, the indexing notation is a shorthand for pointer arithmetic. By default, the variable int *a points to the first index in that array. Technically, int *a is just a pointer to an integer, and you just 'happen to know' that other integers in memory follow it. And thus they have given us a convenient notation

a[i] // where i is the number of spaces passed point *a we want to look.

I am not sure what you are trying to do within the loop, but to access the ith element, you would do the following. My function just takes the compliment of the array a. nothing is done to c.

#include <stdio.h>

void set_compliment(int *a, int *compliment, int n) {
  int i;

  for (i = 0; i < n; i++) {

   // if (a[i] == 0)
   if ( *(a + i) == 0) {

     // a[i] = 1;
     *(a + i) = 1;

     // else if (a[i] == 1)
   } else if ( *(a+i) == 1) {

     // a[i] = 0;
     *(a + i) = 0;
   }
  }
}

//------------------------------------------------------------------------------

void display(int *a, int n) {
  int i;

  for (i = 0; i < n; i++) {
    printf("%i ", a[i]);
  }

  printf("\n");
}

//------------------------------------------------------------------------------

int main() {

  int a[] = {1, 1, 1, 1, 0};
  int c[] = {0, 0, 0, 0, 1};

  // Get length of array
  int n = sizeof(a) / sizeof(a[0]);

  set_compliment(a, c, n);

  display(a, n);
  display(c, n);

  return 0;
}

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.