Reputation: 11
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);
}
Upvotes: 0
Views: 92
Reputation: 22823
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:
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]);
}
}
Upvotes: 2
Reputation: 59681
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
}
}
Upvotes: 0