Code works fine. Output received from some input In several places after the term e[something] '.' operator is used. Since e is a pointer that holds address to an element, Why isn't '->' operator is used. Is it so because e[something] will hold an entire structure as value to that index=something instead of an address to the element?
#include <stdio.h>
#include <stdlib.h>
struct element{
int i;
int j;
int x; //x represents value of A[i][j]
};
struct sparse{
int m;
int n;
int num; // num represents no. of non-zero elements
struct element *e;
};
void create(struct sparse *);
void display(struct sparse);
void create(struct sparse *s){
printf("Enter Dimensions :\n");
scanf("%d%d", &s->m, &s->n);
printf("Enter number of non-zero elements :");
scanf("%d", &s->num);
s->e = (struct element *) malloc(s->num * sizeof(struct element));
printf("Enter i, j, x :\n");
for(int k = 0; k < s->num; k++){
scanf("%d%d%d", &s->e[k].i, &s->e[k].j, &s->e[k].x); // why not s->e[k]->i
}
}
void display(struct sparse s){
int p, q, k = 0;
for(p = 0; p < s.m; p++){
for(q = 0; q < s.n; q++){
if(p == s.e[k].i && q == s.e[k].j){
printf("%d ", s.e[k].x);
k++;
}
else{
printf("0 ");
}
}
printf("\n");
}
}
int main(){
struct sparse s;
create(&s);
display(s);
return 0;
}