2

i am trying to sort an array, using pointers and also without using index variables

void sort(int *a,int n)
{
  int *temp;
  *temp=0;
  int *b=a;
  for(;a<a+n-1;a+=1)
    {
      for(b=a+1;b<b+n;b+=1)
        {
          if(*a>*b)
          {
            *temp=*a;
            *a=*b;
            *b=*temp;
          }
        }
    }
}

the equivalent version of the above program without using pointers is

void sort(int a[],int n)
{
  int i,j,temp;
  for(i=0;i<n-1;i++)
    {
      for(j=i+1;j<n;j++)
        {
          if(a[i]>a[j])
          {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
          }
        }
    }
}

i am doing an exercise question, and i just started learning the pointers by myself. i believe i am doing it right as i am trying to avoid the usage of index variables too. i am receiving an error segmentation fault : 11 . what am i missing here? is the program wrong in someway? or the idea itself is wrong? feedback appreciated.

6
  • 1
    We could go crazy and use variable names more than one character long. You know, if we wanted. Commented Apr 21, 2016 at 16:38
  • 1
    Note: both versions use pointers only. You never use an array for any access. Commented Apr 21, 2016 at 16:57
  • @Olaf yes that's true. whenever you pass an array to a function, it actually passes a pointer to the first element of the array. is that right ? Commented Apr 21, 2016 at 16:58
  • 1
    Rolled back. You must not change the question once you got an answer. Just add clarifications or append text to the question with a note. Commented Apr 21, 2016 at 17:00
  • 1
    @buggenerator: Not just that. int a[10]; a[1];: here a[1] already uses a pointer. (I will not further elborate, just do some research on your own) Commented Apr 21, 2016 at 17:00

4 Answers 4

3

These two lines are wrong:

int *temp;
*temp=0;

In the first line, you declare a pointer variable. This variable points somewhere, but as long as you don’t assign any value to this variable, you cannot know where it points.

Then, in the second line, you say: Wherever temp points to, write a zero into that memory cell. So the program writes to a random unknown address.

In your case, temp should not be a pointer at all, since you later need it to store an int.

As a general rule, you should not change the parameters to a function, in this case a. Rather, create one more local variable. This will prevent another bug in your code where you compare b<b+n. That expression can be transformed to 0<n for all situations except overflow. The expression should rather be p<a+n, where p is your new local variable.

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

1 Comment

yes @Roland Illig . it's a rookie mistake, i understood what i've done wrong in that
2

a<a+n-1 - that's always true if n > 1.

BTW, the argument int a[] decays to a pointer.

2 Comments

yes that's true. but still not getting what you were implying ?
A loop whose loop condition is always true, is an infinite loop - that is, you walk right off the end of the array and keep going, sorting all the memory you can reach until the program crashes. If you tried this on an old (pre-protected memory) OS, you'd probably have to power-cycle the machine to stop it.
2
*temp=0; 

is not allowed: You have not allocated memory for temp

Either use :

int temp;

or do

 int *temp;
   temp=malloc(1*sizeof(*temp)); // The dirty way.

5 Comments

alright i have made a change. created a variable and assigned the pointer to that variable to the temp. still receiving same error
Yep, just use int temp = 0; - it's not an index variable, so you don't need to change it somehow to a pointer for your exercise
Damn, that's a more easy way. but i am still receiving an error?
@sjsam i don't know what does that malloc do. i just started learning pointers and that's an exercise question
@buggenerator : just allocating 4 bytes of memory for storing and integer value. But don't use it here. It makes the code dirty
2

Omnibus edition (UB = Undefined Behaviour = bugs and errors):

void sort(int *a,int n)
{
  int *temp;
  *temp=0;  // UB: assignment to uninitialized pointer
  int *b=a;
  for(;a<a+n-1;a+=1) // UB: a<a+n-1 is always true so this is infinite
    {

first we'll replace int *temp with int temp=0 as per sjsam's answer.

void sort(int *a,int n)
{
  int temp=0;  // not an index variable, doesn't have to change to pointer

That leaves the infinite loop: you need to remember where the array ends even as you iterate through it:

  int *begin = a;
  int *end = a + n; // one-past-the-end for a half-open interval
  for (int *i = begin; i < end; ++i) {

Note that you'll need an equivalent change for the nested loop, and *temp should just be temp in the element swap.

2 Comments

alright i got it now, let me know what i interpreted is right, in the above program as i am increasing the value of a and also i am checking whether a<a+n-1. which leads to an infinite loop. isn't it?
Yes - the first time round, a + n is the same as my end. But, the second time round the loop, end stayed put (at the end of the array), while a + n has moved since we increased a. The value isn't taken once at the start unless you do that manually - the whole expression (a < a + n) gets evaluated each time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.