# Thread: How can we demonstrate interchangeability of Arrays and Pointers in a structure?

1. ## How can we demonstrate interchangeability of Arrays and Pointers in a structure?

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";

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";

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);
}```

2. This should solve your problem :

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct card{
char face[50];
char suit[50];
};

int main()
{
struct card aCard;
struct card *cardPtr;

strcpy(aCard.face, "Ace");

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;
}```

I hope that I have helped .

3. Originally Posted by hefese
We know that arrays and pointers in C are interchangeably.
No they are not: C FAQ/6.2

Code:
```//Pseudocode for giving instance.
int* bPtr;
int b;

bPtr=&b;

*(bPtr+i)  | bPtr[i]
*(b+i)      | b[i]```
b is an int and has no index. *(b + i) and b[i] will get you errors.
*(bPtr + i) and bPtr[i] will return garbage because there is no array at the address of b.

Code:
```aCard.(face[])="Ace";
Assigning a string to a char-array only works in initialization:
Code:
`char string[] = "This is a string";`
In your case you have to use strcpy or even better strncpy:
Code:
`strncpy(aCard.face, "Ace", 50);`
Code:
`(*cardPtr).(face[])," of ",(*cardPtr).(suit[])`
From where have you got this weird syntax?

Bye, Andreas

4. Originally Posted by rjjj
This should solve your problem :

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct card{
char face[50];
char suit[50];
};

int main()
{
struct card aCard;
struct card *cardPtr;

strcpy(aCard.face, "Ace");

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;
}```

I hope that I have helped .
Thank you rjjj for the reply.

------------------------------------------------------------------------

Originally Posted by AndiPersti
No they are not: C FAQ/6.2

Code:
```//Pseudocode for giving instance.
int* bPtr;
int b;

bPtr=&b;

*(bPtr+i)  | bPtr[i]
*(b+i)      | b[i]```
b is an int and has no index. *(b + i) and b[i] will get you errors.
*(bPtr + i) and bPtr[i] will return garbage because there is no array at the address of b.

Code:
```aCard.(face[])="Ace";
Assigning a string to a char-array only works in initialization:
Code:
`char string[] = "This is a string";`
In your case you have to use strcpy or even better strncpy:
Code:
`strncpy(aCard.face, "Ace", 50);`
Code:
`(*cardPtr).(face[])," of ",(*cardPtr).(suit[])`
From where have you got this weird syntax?

Bye, Andreas
*It's so interesting. That is to say I have known wrong. Thank you so much for the link.
*I forgot to write brackets for int b. (int b[]; )
*I didn't know that "assigning a string to a char-array only works in initialization". Thank you for this knowledge.
*And finally it's time to answer for your question: There is no place. I made it up.

5. For these declarations:
Code:
```int array[10];
int index = 5;```
array[index] is the same as *(array + index), which is the same as *(index + array), which is the same as index[array] .

6. Originally Posted by iMalc
For these declarations:
Code:
```int array[10];
int index = 5;```
array[index] is the same as *(array + index), which is the same as *(index + array), which is the same as index[array] .
*I thought that it was a joke. But then, I just stared with mouth open at the window output like .
*Alright. I understand that I didn't know wrong this subject. I just knew nothing.
*Thank you for sharing that something confusing. It's opened a new door for me about my study.

7. Originally Posted by iMalc
array[index] is the same as *(array + index), which is the same as *(index + array), which is the same as index[array] .
Just to be pedantic, array[index] and *(array + index) are not necessarily the same. They do give equivalent results, but the way they do that (say at the level of machine instructions) is often different. And that difference can be detected in various ways (performance measures, number of instructions to achieve an effect, etc).

index[array] and array[index] are equivalent because addition is commutative for pointers and integral values (i.e. x + y and y + x are equivalent)