Aprillia Lee
Aprillia Lee

Reputation: 13

How to fix this Error "subscripted value" on C language?

I got this Error from my program

error: subscripted value is neither array nor pointer nor vector
   84 |   }while(deck[row][column] != 0);
      |                   ^
poker_comp.c:85:13: error: subscripted value is neither array nor pointer nor vector
   85 |    deck[row][column] = card;
      |

Can help me to fix this error?

Here's the code

typedef struct card{
    int pips;
    char suit;
    bool flipped;
    struct card *next;
}Card;

typedef struct deck{
    int size;
    Card *top;
    //int topcardindex;
}Deck;

typedef struct player{
    char* name;
    Card *c1, *c2;
    int account;
}Player;


#include "poker.h"

/* [ Chapter 9: Structures and Unions ]  
 *
 *  Functions to create
 *  
 *  1) newcard(-,-): Use malloc
 *
 *  2) newdeck(): Use malloc 
 *  
 *  3) newplayer(): Use malloc
 *
 *      4) eject(): only can pop out from the top of the Deck 
 *          (pop from the bottom is not allowed) 
 *
 *  5) shuffle(-): use srand/rand
 */

/* function to complete */
Card* newcard(int _pips,char _suit){
    
    /* write codes */   
    Card *newCard = malloc(sizeof(Card));
    newCard->pips = _pips;
    newCard->suit = _suit;
    
    return newCard; 
}
    
Deck* newdeck(){
    
    /* write codes */
    Deck *deck = malloc(sizeof(Deck));
        
    Card* cursor;   
    for(char s = 0; s <= 3; s++) {
        for(int p = 2; p <= 14; p++) {
            if(deck->top == NULL) { 
                deck->top = newcard(p,s);
                cursor = deck->top;
            } else {
                cursor->next = newcard(p,s);
                cursor = cursor->next;
            } deck->size;
        }
    }
    return deck;
}

Player* newplayer(char* _name,int _account){
    
    /* write codes */   
    Player *player = malloc(sizeof(Player));
    player->name = _name;
    player->account = _account; 
    
    return player;
}

Card* eject(Deck* deck) {
    Card* temp;

    /* write codes */
    //for(i = 0; i<deck->size; i++)
    //  temp[i]=i;
    //deck_card = deck->size;
    
    temp = deck->top;
    deck->top=temp->next;

    return temp;
}

void shuffle(Deck* deck){
    
    /* write codes */
    
    int row, column, card;
    
    for(card=1; card<=52; card++){
        do{
            row = rand() %52;
            column = rand()%52;
        }while(deck[row][column] != 0);
            deck[row][column] = card;
    }
    
    srand(clock());     
    
    if(deck->size<=20) printf("\n\nERROR: deck size should be bigger than 20\n\n");
    unsigned char index = rand()%(deck->size-20)+10;
    unsigned char endex = rand()%(deck->size - index-1)+1;
    
    Card *cindex, *cendex, *aendex, *bendex;
    cendex = deck->top;
    for(int i = 0; i < index-1; i++) {
        cendex = cendex->next;
    }
    
    head->top = deck->top;
    head->size = index-1;
    cindex = cendex->next;
    cendex->next = NULL;
    aendex = cendex;
    cendex = cindex;
    
    for(int i = 0; i < endex-1; i++) {
        cendex = cendex->next;
    }

    
    mid->top = cindex;
    mid->size = endex-1;
    cindex = cendex->next;
    cendex->next = NULL;
    bendex = cendex;
    cendex = cindex;

    for(int i =0; i < (deck->size - index - endex-1); i++) {
        cendex = cendex->next;
    }
    cendex->next = NULL;
    
    
    tail->top = cindex;
    tail->size = (deck->size - index - endex);  
    
    deck->top = mid->top;
    aendex->next = tail->top;
    bendex->next = head->top;

    free(head); free(mid); free(tail);      
    return;
}

void freedeck(Deck* deck){      

    /* write codes */   

    return;
}

Upvotes: 1

Views: 111

Answers (1)

Sourav Ghosh
Sourav Ghosh

Reputation: 134286

When in doubt, check the data types.

  • deck is of type Deck *, pointer to type Deck or struct deck.

  • deck[row] gives you a variable of type Deck or struct deck. This is not a pointer type.

  • Further trying to index into deck[row] is invalid syntax, as to apply array subscripting operator [], the operand has to be "pointer to complete object type". In your case, this violates the rule, so this is invalid syntax.

If you have to use this syntax, deck has to be of type Deck **.

Upvotes: 1

Related Questions