Doubt with Pointers to fix Structure Variables


My question is, why is the pointer const wDeck , which is as a parameter in the functions fillDeck , shuffle and deal , does not have brackets at the end (that is, it is not a pointer to an array unlike the other parameters)?

If this pointer is supposed to point to the array of structure variables Card , why is the program showing errors when bracketing the wDeck parameters? And by not doing so, does the program run perfectly?

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
#include <string.h>

/* Definition of the structure 'card' */
struct card
    const char *face;
    const char *suit;
}; /* End of the definition of the structure 'card'. */

typedef struct card Card;

void fillDeck(Card *const wDeck, const char *wFace[], const char *wSuit[]);
void shuffle(Card *const wDeck);
void deal(const Card *const wDeck);

int main()
    Card deck[52];

    /* Initialize the arrays of pointers */
    const char *face[] = {"Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
    const char *suit[] = {"Hearts", "Diamonds", "Clubs", "Spades"};


    fillDeck(deck, face, suit);

    return 0;

/* Place strings into Card structures. */
void fillDeck(Card *const wDeck, const char *wFace[], const char *wSuit[])
    int i;

    for(i=0; i<=51; i++)
        wDeck[i].face = wFace[i % 13];
        wDeck[i].suit = wSuit[i / 13];

void shuffle(Card *const wDeck)
    int i;
    int j;

    Card temp;

    for(i=0; i<=51; i++)
        j = rand() % 2;
        temp = wDeck[i];
        wDeck[i] = wDeck[j];
        wDeck[j] = temp;

void deal(const Card *const wDeck)
    int i;

    for(i=0; i<=51; i++)
        printf("%5s of %-8s%s", wDeck[i].face, wDeck[i].suit, (i + 1) % 4 ? " " : "\n");

asked by Jesús Fragoso 23.01.2017 в 18:11

2 answers


In C, TYPE[] and TYPE* with basically equivalents and interchangeable , as clearly indicated by your response eferion.

However, your concern comes from another detail , indirectly related to the above, but different. In this code

void fillDeck( Card *const wDeck, const char *wFace[], const char *wSuit[] );

wFace and sWuit are const char * , that is, they are pointers to constant data ; you can modify the pointer itself, but you can not modify the data pointed . The expression *wFace = XXX would be erroneous , while the expression wFace = XXX would be correct .

wDeck , on the other hand, is a constant pointer to a data. Look at the position of const , which is not the same as in the previous ones. That means that You can modify the data, doing, for example, *wDeck = XXX ; what you can not do is modify the pointer itself .

Now, if you add the brackets

Card *const wDeck[]

Because of what we said about the pointers being interchangeable with arrays , actually, the compiler sees this:

Card *const Card *wDeck

Which is a type completely different from the original, and which does not match the value you are trying to use, which you keep taking from the variable Card deck[52] , and whose type, therefore, is Card * .

answered by 23.01.2017 в 19:37

In C, a simple pointer is just a variable that stores a memory location:

int* puntero;

When we want a pointer to store a collection of elements, then we have a one-dimensional array or a vector. If the number of elements is not too large we can choose to create it on the stack:

int vector[100];

With the previous statement what we got is to reserve space for 100 elements of type int . The different elements are going to be in consecutive memory positions:

elem1 | elem2 | elem3 | ... | elem100

Because of this feature, the compiler does not need to remember the position of each element. It is enough to know the position of the first element and apply a displacement from that position. This is why a vector is comparable to a simple pointer:

int vector[100];

int *puntero = vector; // ok

And, by extension, when we call a function that supports simple pointers it will not matter if the pointer points to a single element or a collection of them:

void func(int*);

int vector[100];
int *puntero = vector;

func(vector);  // ok
func(puntero); // ok

Regarding the use of the brackets, I would advise you to better explain what you mean ... an example of both cases of use (with error and without error).

answered by 23.01.2017 в 18:26