PREV INDEX NEXT

Author: Stan Eisenstat
Subject: Re: [Cs223] Sorting and the prohibition on pointers
Date: Tuesday, 04 Feb 2020, 07:50:52


    > Message Posted By: Unknown
    >
    > Are we allowed to use pointers for the purpose of returning an array from
    > a function? If this is not allowed, it greatly limits our choices of
    > sorting algorithms

No, you may not use pointers to return an array from a
function, nor do you need to.  As we discussed in class
last Wednesday, arrays are in effect passed by reference,
not by value.  Thus the following function will set the
elements of its array argument to 0:

  void zap (int X[], int n) {
      for (int i = 0; i < n; i++)
	  X[i] = 0;
  }

That is, after the code snippet

  int n = 10;
  int A[n];
  zap (A, n);

the elements of the array A will all be 0.

--Stan-
PREV INDEX NEXT