0

Hi i want to ask about why i need to use printf("\n%d",x); instead of printf("\n%d",*x);? Thank you very much

#include<stdio.h>
#include<stdlib.h>
#define totalnum 8
void display(int **);
int main()
{
    int marks[totalnum]={55,65,75,85,90,78,95,60};
    printf("The marks of student A are:");  
    display(marks);
    return 0;
}
void display(int *x)
{
    int i;
    for(i=0;i<totalnum;i++)
    printf("\n%d",x);
}
4
  • 3
    Hint: check the data types. but your code and usage doesn't look proper. Commented Dec 16, 2014 at 5:49
  • The prototype and definition of display() do not match. Commented Dec 16, 2014 at 5:50
  • There are several (obscure) differences between the two versions of display() However, using int [] in the prototype is not a clear method . better to use int * as that is clear. Then both versions should use: printf("\n%d",x[i]); as it is (almost) always better to not change the passed by value parameter(s). Commented Dec 16, 2014 at 11:56
  • What's up with the Stealth edit, taking someone's code in their answer and putting it in your question? Don't do that. Do DO a rollback, or I will. That is not considered fair; it's called "dirty pool". Edit: Never mind about you doing a rollback, I did it. Commented Dec 16, 2014 at 13:32

2 Answers 2

2

There is no pass by reference in C. The array decays to a pointer in the display function which you declared wrongly as int ** instead of int * - Compiler should have given you a warning at least about this:

http://ideone.com/R3skNj

This is how your display function should be like:

void display(int *x) 
{
    int i;
    for(i = 0; i < totalnum; i++) {
        printf("\n%d",*(x+i)); // or printf("\n%d",x[i]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think your looking for something like this:

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

#define totalnum 8

void display(int *);  //Typ is 'int *' NOT 'int **'
int main() {

    int marks[totalnum] = {55,65,75,85,90,78,95,60};

    printf("The marks of student A are:");
    display(marks);

    return 0;

}

void display(int *x) {
    int i;
    for(i = 0; i < totalnum; i++) {
        printf("\n%d",*x);  //Prints the value
        x++;  //increments the pointer
    }
}

5 Comments

i want to ask what is the difference between your code and this code
'/* lab10_3.c pass by reference*/ #include<stdio.h> #include<stdlib.h> #define totalnum 8 void display(int []); int main() { int marks[totalnum]={55,65,75,85,90,78,95,60}; printf("The marks of student A are:"); display(marks); return 0; } void display(int x[]) { int i; for(i=0;i<totalnum;i++) printf("\n%d",x[i]); }'
@Jake In your Code your just printing the address of the first element of the array. In my Code i print all values of the array.
Sorry i have another code now updated plz check again plz THANK YOU i updated it in the question thanks
@Jake In the first one you pass the address of the first element of your array and print the address x times(x elements of the array). In the second example you pass the full array and you go through the values with the loop and you're printing all values of the array.

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.