Manu
Manu

Reputation: 5784

error after compilation in C

when i compile i get the following error, why i get this error every thing looks fine.have given just a part of the code.

error: subscripted value is neither array nor pointer

main()
{
int clf_cnt,key;
struct classifier clsf,*clsf_ptr;
int choice;
printf("Creation of B tree for node %d\n",M);
while(1)
{
    printf("1.Insert\n");
    printf("2.Display\n");
    printf("3.Quit\n");
    printf("Enter your choice : ");
    scanf("%d",&choice);
    switch(choice)
            {

                    case 1:
                            printf("Enter the rules : ");
                            for(clf_cnt = 0;clf_cnt < M;clf_cnt++)
                                    {
                             error line:       clsf_ptr = &clsf[clf_cnt];

but i have declared

    struct node
    {
    int n; 
    int keys[M-1]; 
    struct node *p[M]; 
    struct classifier clsf[M-1]; 
    }*root=NULL;

then how should insert data to this array of structure

Upvotes: 2

Views: 109

Answers (5)

P&#233;ter T&#246;r&#246;k
P&#233;ter T&#246;r&#246;k

Reputation: 116266

You are trying to index a struct here: clsf[clf_cnt]. Structs can't be indexed this way, only arrays and pointers.

So writing e.g. clsf_ptr[clf_cnt] instead would make your compiler happy :-) However, then you must also initialize that pointer to actually point to an existing array of struct classifier instances, prior to using it. Otherwise you get undefined behaviour (i.e. most likely your program will crash).

Upvotes: 2

Mitch Wheat
Mitch Wheat

Reputation: 300549

clsf is declared as a single struct, but you treat it as if it was an array.

Upvotes: 1

Simone
Simone

Reputation: 11797

I think the offending line should be clsf_ptr[clf_cnt];.

Upvotes: 0

detunized
detunized

Reputation: 15289

Because clsf is of type struct classifier which isn't an array or a pointer, exactly what compiles has told you. You cannot use [] operator on a struct in C.

Upvotes: 1

Benoit Thiery
Benoit Thiery

Reputation: 6387

The problem is with clsf[clf_cnt]. clsf is not a pointer nor array.

Upvotes: 1

Related Questions