Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

change array references to pointer references

Status
Not open for further replies.

Seijuro

Programmer
Oct 8, 2003
10
US
I was just wondering if I was doing this right. What I am trying to do is change the array references to pointer references and then use pointers and dereferenced pointers rather than array references. Any help would be appreciated!!

original
Code:
void sort(int *arr, int size)
{
  int i, j, k, l  ;
  for (l = 1  ; l < size ; l = 3*l+1)
  {
    /* do nothing */
  }

  l = (l - 1) / 3 ;

  while (l > 0)
  {
    for (i = 0 ; i < size ; i++)
    {
      for (j = i ; j >= l ; j -= l)
      {
	if (arr[j] < arr[j-l])
	{
	  int t ;
	  t = arr[j] ;
          arr[j] = arr[j-l] ;
	  arr[j-l] = t ;
	}
      }
    }

#if DEBUG_LEVEL > 5
    for (i = 0 ; i < COUNT ; i++)
    {
      printf(&quot;l=%d -- sorter[%d]=%d\n&quot;, l, i, arr[i]) ;
    }
#endif

    l = (l - 1)/3 ;
  }

#if DEBUG_LEVEL > 0
  checksort(arr, size) ;
#endif
Mine
Code:
void sort_p(int *arr, int size)
{
  int *i, *j, k, *l, *p  ;
  int *t ;

  for (l = arr + 1  ; l < arr+size ; l = 3*l+1)

    /* do nothing */


  l = (l - 1) / 3 ;

  while (l > 0)

    for (p = arr ; p < arr + size  ; p++)

      for (j = p ; j >= l ; j -= l)

	if (*j < *(j-l))


	  t = *j ;
          *j = *(j-l) ;
	  *(j-l) = t ;




#if DEBUG_LEVEL > 5
    for (i = 0 ; i < COUNT ; i++)

      printf(&quot;l=%d -- sorter[%d]=%d\n&quot;, l, i, *i) ;

#endif

    l = (l - 1)/3 ;


#if DEBUG_LEVEL > 0
  checksort(arr, size) ;
#endif
}
 
Hi there,

you did it right except where you said:
(in sort_p)
Code:
int *t;
this should be:

Code:
int t;
since t is an int, not a pointer to int.

Greetings,
Tobi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top