I'm trying to make some sort of database for cards for a cardgame similar to Final Fantasy VIII Triple Triad Card Game where each card has its name and scores on the top, bottom, left, and right sides of the card. I'm using arrays of structures. i can't compile it well because of errors. can anyone help me identify the erroneous part of this code? i've omitted some parts of it but basically the code below shows the meat of the program i want to make.

struct card_t {
char name[20];
int top, bottom, left, right;
};

int main(){
cardslist(); //is this the right way to call cardslist?

return 0;
}

void cardslist(){
struct card_t card[100];
card[0].name = "geezard"; //errors occur from this line onwards
card[0].top = 1;
card[0].bottom = 2;
card[0].left = 3;
card[0].right = 4;

card[1].name = "abadon";
card[1].top ........

card[2].name = ........
}

can anyone help me correct this code please? thanks!

>>cardslist(); //is this the right way to call cardslist?
Functions must be declared before they can be used. Read this link


>>card[0].name = "geezard"; //errors occur from this line onwards

You can't assign a string to a character array like that. Either make name a pointer or use strcpy() strcpy(card[0].name,"geezard");

Edited 6 Years Ago by Ancient Dragon: n/a

ok i've fixed the errors, but i've got a new problem. how come i can't access the elements of the structure? here is my new code:

#define NCARDS 100
struct card_t {
    char name[20];
    int top, bottom, left, right;
};

void cardslist(struct card_t *);

??????????????????????????????????????????????????
func1() {
    puts(pcard[0].name); // how could i access the structure element here correctly?
                             // this only prints garbage characters
}
??????????????????????????????????????????????????

int main() {
    struct card_t card[NCARDS], *pcard;
    pcard = &card[0];
    cardslist(pcard);
}


void cardslist(struct card_t *pcard) {
    strcpy(pcard[0].name, "catoblepas");
    strcpy(pcard[1].name, "thrustaevis");

    pcard[0].top = 1;
    pcard[1].top = 5;
}

is there any way i could declare the structure elements globally so that i won't bother thinking how to access them in any functions?
all answers are greatly appreciated, thank you very much!

Edited 3 Years Ago by Nick Evan: Fixed formatting

>>is there any way i could declare the structure elements globally
Yes, but global data is generaly frowned upon. Its very easy to access the structure in functions, just pass the array as a parameter just like you did with cardslist() function.

pcard = &card[0];
cardslist(pcard);

pcard is unnecessary. Just use card cardslist(card);

Edited 6 Years Ago by Ancient Dragon: n/a

>>is there any way i could declare the structure elements globally
Yes, but global data is generaly frowned upon.

pcard = &card[0];
cardslist(pcard);

pcard is unnecessary. Just use card cardslist(card);

but i really need the structure elements to be declared globally because actually, i'm also using GUI for this program as this is a "card game" and it has to have a "window" for players to be able to see their own cards and the playing board. the GUI buttons of the playing board are just waiting to be clicked and once they're clicked, my program will need to search the card that was put into the board -- the card will be searched in the structure.

the reason why i need to declare the cards and their points globally is because every button has a separate function from the rest and they need to access the structure's elements.

is there a way to declare the structure elements globally? thank you very much

Of course there is a way. Just declare the array outside any function.

in my most recent program code above, i already declared the structure elements outside other functions (though the declarations are inside the function cardslist) but how come in func1 when i accessed an element by using puts(), garbage characters were printed on the screen? could you help me correct this?

The array is declared inside a function. All you declared globally is the structure definition. That is not enough.

#define NCARDS 100
struct card_t {
char name[20];
int top, bottom, left, right;
};

struct card_t cards[NCARDS];
// functions go here

not delete the similar declaration of cards[] from main() and delete the parameter in cardslist() function. Since the array is now global it's not necessary to pass it to a function that alreay knows what it is.

Edited 6 Years Ago by Ancient Dragon: n/a

not delete the similar declaration of cards[] from main() and delete the parameter in cardslist() function.

could you post the complete statement here? i didn't understand this one. thanks

What! and you are attempting to write a gui program without the slightest notion of what you are doing :icon_eek: Does a person who doesn't know how to swim jump right into the water and attempt to scuba diving?? Do do you try to build a house before you know the difference between a screw driver and a hammer?

Start back at square one and learn C language from the beginning, not somewhere in the middle or end.

>>could you post the complete statement here
That was a complete statement.

Edited 6 Years Ago by Ancient Dragon: n/a

The array is declared inside a function. All you declared globally is the structure definition. That is not enough.

#define NCARDS 100
struct card_t {
char name[20];
int top, bottom, left, right;
};

struct card_t cards[NCARDS];
// functions go here

not delete the similar declaration of cards[] from main() and delete the parameter in cardslist() function. Since the array is now global it's not necessary to pass it to a function that alreay knows what it is.

here's what i did, but there are still errors:
could you tell me how i could correct them?

#define NCARDS 100
struct card_t{
char name[20];
int top, bottom, left, right;
};

struct card_t card[NCARDS] {
strcpy(card[0].name, "squall");
.
.
card[0].top = 1;
};

on_b00_clicked (....) {
puts(card[0].name); //what's the right way to print the first name in the array? there's an error here
}

by the way, the statement i was asking you to complete is this:
"not delete the similar declaration of cards[] from main()
and delete the parameter in cardslist() function"

and to clarify things, i started learning C 4 years ago, i've stopped, and it's only now that i'm doing some programming again. as for gui, i'm really new in using it, too bad we have no good gui background in our curriculum.

What! and you are attempting to write a gui program without the slightest notion of what you are doing :icon_eek: Does a person who doesn't know how to swim jump right into the water and attempt to scuba diving?? Do do you try to build a house before you know the difference between a screw driver and a hammer?

Start back at square one and learn C language from the beginning, not somewhere in the middle or end.

>>could you post the complete statement here
That was a complete statement.

here's what i did, but there are still errors:
could you tell me how to correct them?

#define NCARDS 100

struct card_t {
char name[20];
int top, bottom, left, right;
};

struct card_t card[NCARDS] {
strcpy(card[0].name, squall);
.
.
card[0].top = 1;
}

on_b00_clicked(....) {
puts(card[0].name); //how do i correctly print the first name stored in the structure?
}

to clarify things, i started learning C 4yrs ago, stopped, and started programming again recently. as for gui, i'm really new on using it, too bad we had no solid gui background in our curriculum.

struct temp
{
        char name[20];
};

struct temp t1[20];


int main()
{
        strcpy(t1[0].name,"abhimanipal");
        printf("%s\n",t1[0].name);
        return 0;
}

Maybe this will help you ....

From the looks of that crap you posted you need to start learning C all over again from scratch. Forget gui for now and take a refresher course in C. or buy a book and study it.

Edited 6 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.