I am trying to implement the concept of Jagged Array while learning the C language.
My code goes below :-
#include<stdio.h>
#include<stdlib.h>
int main() {
int r,**a,n,i,j,*ptr;
do {
printf("Enter no. of Rows : ");
scanf("%d",&r);
if(r<0)
printf("\nPlease enter a positive value\n\n");
} while(r<0);
a = (int**)malloc(r * sizeof(int*));
for(i=0; i<r; i++) {
printf("\n");
do {
printf("Enter no. of elements in row %d : ",i+1);
scanf("%d",&n);
if(n<0)
printf("\nPlease enter a positive value\n\n");
} while(n<0);
*(a+i) = (int*)malloc((n+1) * sizeof(int));
printf("\n");
printf("Input Row %d elements : ",i+1);
for(j=0; j<n; j++)
scanf("%d",&(*(*(a+i)+j)));
(*(a+i))+j = NULL;
}
printf("\n\nYou entered :-\n\n");
for(i=0; i<r; i++) {
for(j=0; (*(a+i))+j; j++)
printf("%d\t",*(*(a+i)+j));
printf("\n");
}
for(i=0; i<r; i++)
free(*(a+i));
free(a);
return 0;
}
On compilation this gives an Error :-
26 error: lvalue required as left operand of assignment
basically for this line of code where I get the value for each pointer element & assign the last pointer element in each row to null :-
printf("Input Row %d elements : ",i+1);
for(j=0; j<n; j++)
scanf("%d",&(*(*(a+i)+j)));
(*(a+i))+j = NULL;
Basically the concept for this Jagged Array is that it inputs the number of rows and then the number of elements in each row seperately and inputs that many elements.
I create a dynamic array of (n+1) * sizeof(int) in each row pointer where n is the number of elements in that row. The (n+1)th element is for storing a null pointer so that while displaying the array we can get where to break out to the next row.
Sorry for posting the whole code but since I don't know where the problem could arise from in the code so I have posted the whole code.
Any help would be appreciated.
Thankyou