Hey guys I am currently learning DSA and in ADT I have a problem:
This is a code that creates a custom array and takes the input of the array and stores it and prints it too but I want to ask that what does that [i]
do in printf("%d\n",(a->ptr)[i]);
that thing is what I am not getting in this code
#include<stdio.h>
#include<stdlib.h>
struct myArray{
int total_size;
int used_size;
int *ptr;
};
void createArray(struct myArray * a,int tSize,int uSize)
{
a->total_size = tSize;
a->used_size = uSize;
a->ptr = (int *) malloc(tSize * sizeof(int));
}
void show(struct myArray * a){
for(int i=0; i < a->used_size; i++){
printf("%d\n",(a->ptr)[i]);
}
}
void setVal(struct myArray * a){
int n;
for(int i=0; i < a->used_size; i++){
printf("Enter Element %d: ", i);
scanf("%d",&n);
(a->ptr)[i] = n;
}
}
int main(){
struct myArray marks;
createArray(&marks,10,2);
printf("We are running setVal now\n");
setVal(&marks);
printf("We are running show now\n");
show(&marks);
return 0;
}