New course! Every coder should learn Generative AI!
Try a free lesson+ 4
Input something like this:
5
3
4
5
2
1
Some minor mistakes:
1) the return type of sort function is int but you didn't return anything, so better make it void.
2) its a good practice to declare and define a function before main function.
3) printf("%d \t",*p+k); must be printf("%d \t",*(p+k));
+ 2
If you are trying to dereference something like *p+k, you have to make it inside parenthesis like that: *(p+k). *p+k makes you add k to the first element of your p array, while *(p+k) make you move the iterator to the next element, as p points to the first element, and k moves iterator by k elements forward. By the way you have to declare your function "sort" before you write it in main. (add "int sort (int, int*); between your headers and main function).
Same about scanf. But you do not have to dereference it with asterisk. You can also write just n[i]-in scanf, and p[k]-in printf instead.
If you want to, you can avoid making unnecessary variable a[30], by allocating memory dynamically to *n. It would look like this:
n=(int*)malloc(sizeof(int)*size);
Then you will have array n that can store "size" elements
0
//Not getting output
#include<stdio.h>
void sort(int size,int *p)
{
int i,j,t,k;
for(i=1;i<size;i++)
{
for(j=0;j<size-1;j++)
{
if(*(p+j)>*(p+j+1))
{
t=*(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=t;
}
}
}
printf("\n sorted array is:");
for(k=0;k<size;k++)
{
printf("%d \t",*(p+k));
}
}
int main()
{
int a[30],*n,i,size;
printf("\n enter the no of elements:");
scanf("%d",&size);
n=a;
printf(" unsorted array is :");
for(i=0;i<size;i++)
{
scanf("%d \t",n+i);
}
sort(size,n);
}