开发者

problem with array in C

I'm a programming student and I don't understand what is the problem with this code:

    #include <stdio.h>
    #include <stdlib.h>

    void merge(int a[], int low, int high, int mid){
        int i, j, k, c[50];
        i=low;
        j=mid+1;
        k=low;
        while((i<=mid)&&(j<=high)){
            if(a[i]<a[j]){
                c[k]=a[i];
                k++;
                i++;
            } //end if
        else{
            c[k]=a[j];
            k++;
            j++;
            } //end else
        } //end while
        while(i<=mid){
            c[k]=a[i];
    开发者_JAVA技巧        k++;
            i++;
        } //end while
        while(j<=high){
            c[k]=a[j];
            k++;
            j++;
        } //end while
        for(i=low;i<k;i++){
            a[i]=c[i];
        } //end for
    } //end merge()

    int mergesort(int a[], int low, int high){
        int mid;
        if(low<high){
            mid=(low+high)/2;
            mergesort(a,low,mid);
            mergesort(a,mid+1,high);
            merge(a,low,high,mid);
        } //end if
        return(0);
    } //end mergesort()

    int main(){
        int i, n, arr[100];
        do{
            scanf("%d", &n);
            if(n == 0)
                break;
            else{
                for(i = 0; i < n; i++){
                    scanf("%d", &arr[i]);
                    mergesort(arr, 0, n);
                } //end for
            } //end else
            for(i = 0; i < n; i++)
                printf("%d\n", &arr[i]);
        }while(n != 0); //end while
    } // end main()

And terminal shows me the following mistake

ej.c: In function ‘main’:
ej.c:60:5: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’

The purpose of this program is to show a sorted array.


Here:

 printf("%d\n", &arr[i]);

It should be

 printf("%d\n", arr[i]);

because you want to print the actual element of the array arr[i], and not its address &arr[i].


Notice the difference between scanf and printf . In scanf you are supposed to provide the address (explanation, why), while in printf the actual value.


printf("%d\n", &arr[i]);

arr is an array of integers
arr[i] is an integer
&arr[i] is a pointer

The printf specifier "%d" 'wants' an integer, not a pointer. Try

printf("%d\n", arr[i]);


You need the & in the scanf() line; you do not need it in the printf() line.

You pass the address of the variable to scanf(); you pass the value arr[i] to printf().


scanf("%d", &arr[i]);     // Correct

printf("%d\n", &arr[i]);  // Incorrect
printf("%d\n", arr[i]);   // Correct

Would you explain why?

With scanf(), the function needs to modify the variables in the calling function, but since C passes arguments by value, that won't work; therefore, you have to pass pointers to the variables so that scanf() can write to the variables via the pointer.

By contrast, when you are printing the values, the pass-by-value mechanism is perfect; you supply the value to be printed, and printf() cannot accidentally modify the variable in the calling function. The & is the 'address of' operator, of course. To print a simple integer (such as i), you'd write printf("%d\n", i);, wouldn't you? The same applies to arrays: arr[i] is an integer value like i is an integer value. So, to pass the integer value to printf(), write:

printf("%d\n", arr[i]);


Well the warning says it all, &arr[i] is not an int but a pointer to an int. Did you intend arr[i] instead?


In your printf near the end, you have

printf("%d\n", &arr[i]);

arr[i] is an int; &arr[i] is a pointer to it.

Lose the &.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜