We know that arrays and pointers in C are interchangeably. Namely;
Code:
//Pseudocode for giving instance.
int* bPtr;
int b;
bPtr=&b;
*(bPtr+i) | bPtr[i]
*(b+i) | b[i]
So I have tried to perform this knowledge to the structures. But I've failed. How can I overcome this problem? Or can't we do this in the structures? Or is there a special case in the structures?
Here the programme which runs:
Code:
#include <stdio.h>
#include <stdlib.h>
struct card{
char *face;
char *suit;
};
int main()
{
struct card aCard;
struct card *cardPtr;
aCard.face="Ace";
aCard.suit="Spade";
cardPtr = &aCard;
printf("%s%s%s\n%s%s%s\n%s%s%s\n",aCard.face," of ",aCard.suit,
cardPtr->face," of ",cardPtr->suit,
(*cardPtr).face," of ",(*cardPtr).suit);
return 0;
}
Here the programme which doesn't run.
Code:
#include <stdio.h>
#include <stdlib.h>
struct card{
char face[50];
char suit[50];
};
int main()
{
struct card aCard;
struct card *cardPtr;
aCard.(face[])="Ace";
aCard.(suit[])="Spade";
cardPtr = &aCard;
printf("%s%s%s\n%s%s%s\n%s%s%s\n",aCard.face," of ",aCard.suit,
cardPtr->face," of ",cardPtr->suit,
(*cardPtr).(face[])," of ",(*cardPtr).(suit[]));
//(*cardPtr).face[]," of ",(*cardPtr).suit[]);
return 0; //(*cardPtr).face," of ",(*cardPtr).suit);
}