Вопрос

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

Это было полезно?

Решение

a has a type int** (pointer to a pointer). (a+i) has the same type, *(a + i) is a pointer, *(a + i) +j is a pointer as well. The problem is you're trying to assign to a temporary object, which was created by the '+' operator. In fact, you're asking the compilator to solve an equation for you and then modify *(a + i), as it's the only value in your expression that is stored in memory.

The only thing you can do it assign either to *(a+i) (and therefore set location of the row) or to *(*(a+i)+j) (and set value of a particular element in the row), or to a itself (and set location of pointers to rows).

This is not a Java and int doesn't have a NULL value. NULL in C is a macro which expands to zero, which can be casted to a null pointer afterwards.

So, allocation of (n+1) elements is useless if user types zero. You can, however, change a's type to int*** and store in each cell either pointer to a single int or NULL. However, this is a greate overhead and I wouldn't do this. You'd better store length of each row as its first element.

And, as Medinoc suggested, use [] instead of asterisks and pluses. a[b] is the same as *(a+b).

Другие советы

The way parentheses are set in (*(a+i))+j causes it to be an rvalue. Likely a typo.

Can't you just use the simpler a[i][j] syntax? Parenthesis errors are less likely this way.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top