help,why does dev-c++ generate the warnings

G

gaulle

i'm a newbie in c. now i 'm learning it.i read a code,and compile it in
dev-c++4.9.9.0.it can
be compiled and pass.but there are some warnings.while in TC2.0,no any
warning.it's why?

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void shuffle(int[][13]);
void deal(const int[][13],const char *[],const char *[]);

main()
{
char * suit[4]={"hearts","diamonds","clubs","spades"};
char * face[13]={"ace","deuce","three","four","five","six","seven"
,"eight","nine","ten","jack","queen","king"};
int deck[4][13]={0};

srand(time(NULL));

shuffle(deck);
deal(deck,face,suit);

getch();
return 0;
}

void shuffle(int wdeck[][13])
{
int card ,row,column;
for(card=1;card<=52;card++)
{
row=rand()%4;
column=rand()%13;

while(wdeck[row][column]!=0)
{
row=rand()%4;
column=rand()%13;
}
wdeck[row][column]=card;
}
}

void deal(const int wdeck[][13],
const char *wface[],
const char *wsuit[])
{
int card,row,column;

for(card=1;card<=52;card++)
for(row=0;row<=3;row++)
for(column=0;column<=12;column++)

if(wdeck[row][column]==card)
printf("%5s of
%8s%c",wface[column],wsuit[row],card%2==0?'\n':'\t');
}
 
E

Ed Morton

gaulle said:
i'm a newbie in c. now i 'm learning it.i read a code,and compile it in
dev-c++4.9.9.0.it can
be compiled and pass.but there are some warnings.while in TC2.0,no any
warning.it's why?

If you want to know why you're getting specific warnings, why, oh why
would you not post those specific warnings?

Ed.
 
M

Martin Ambuhl

gaulle said:
i'm a newbie in c. now i 'm learning it.i read a code,and compile it in
dev-c++4.9.9.0.it can
be compiled and pass.but there are some warnings.while in TC2.0,no any
warning.it's why?

The unmodified OP's code is at EOM. Here is an edited version with the
syntactical problems (and some stylistic ones) fixed & commented on:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void shuffle(int[][13]);

/* mha: for the 'const' deleted from the line below, see the
comments between the calls to shuffle and deal. */
void deal(int[][13], const char *[], const char *[]);

/* mha: main returns an int. It does so implicitly under the old
standard, but implicit int is no longer part of the language. I have
changed 'main()' to the line below. */
int main(void)
{
/* mha: for the 'const' added to the two lines below, see the
comments between the calls to shuffle and deal. */
const char *suit[4] = { "hearts", "diamonds", "clubs", "spades" };
const char *face[13] =
{ "ace", "deuce", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "jack", "queen", "king"};

/* mha: The initialization 'int deck[4][13]={0};' is ok, but fully
bracketed initializations are better. I have changed it below. */
int deck[4][13] = { {0} };

srand(time(NULL));

shuffle(deck);

/* mha: deal() takes arguments of types const int[][13], const char
*[], and const char *[]. But the supplied argments are none of
them const. Welcome to the world of const poisoning. It is easy
enough to make suit and face arrays of pointers to const char (see
declarations above), but deck cannot be made into an array of
arrays of const int, since shuffle modifies it. This means we need
to change the type of the 1st parameter that deal expects. */
deal(deck, face, suit);

/* mha: the line below referred to a non-standard function 'getch()'.
I have changed it to a standard function. */
getchar();
return 0;
}

void shuffle(int wdeck[][13])
{
/* mha: I have not touched the four statements calling rand() in this
function. The FAQ has much better was of using rand() to get a
value in a range [0,N]. */

int card, row, column;
for (card = 1; card <= 52; card++) {
row = rand() % 4;
column = rand() % 13;

while (wdeck[row][column] != 0) {
row = rand() % 4;
column = rand() % 13;
}
wdeck[row][column] = card;
}
}

/* mha: for the 'const' deleted from the line below, see the
comments between the calls to shuffle and deal. */
void deal(int wdeck[][13], const char *wface[], const char *wsuit[])
{
int card, row, column;

for (card = 1; card <= 52; card++)
for (row = 0; row <= 3; row++)
for (column = 0; column <= 12; column++)

if (wdeck[row][column] == card)

/* mha: The line below had a string literal broken
internally by a line break. I have fixed it. */
printf("%5s of%8s%c", wface[column], wsuit[row],
card % 2 == 0 ? '\n' : '\t');
}






[OP's Code]
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void shuffle(int[][13]);
void deal(const int[][13],const char *[],const char *[]);

main()
{
char * suit[4]={"hearts","diamonds","clubs","spades"};
char * face[13]={"ace","deuce","three","four","five","six","seven"
,"eight","nine","ten","jack","queen","king"};
int deck[4][13]={0};

srand(time(NULL));

shuffle(deck);
deal(deck,face,suit);

getch();
return 0;
}

void shuffle(int wdeck[][13])
{
int card ,row,column;
for(card=1;card<=52;card++)
{
row=rand()%4;
column=rand()%13;

while(wdeck[row][column]!=0)
{
row=rand()%4;
column=rand()%13;
}
wdeck[row][column]=card;
}
}

void deal(const int wdeck[][13],
const char *wface[],
const char *wsuit[])
{
int card,row,column;

for(card=1;card<=52;card++)
for(row=0;row<=3;row++)
for(column=0;column<=12;column++)

if(wdeck[row][column]==card)
printf("%5s of
%8s%c",wface[column],wsuit[row],card%2==0?'\n':'\t');
}
 
G

gaulle

thanks,it work well.
gaulle said:
i'm a newbie in c. now i 'm learning it.i read a code,and compile it
in dev-c++4.9.9.0.it can
be compiled and pass.but there are some warnings.while in TC2.0,no any
warning.it's why?

The unmodified OP's code is at EOM. Here is an edited version with the
syntactical problems (and some stylistic ones) fixed & commented on:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void shuffle(int[][13]);

/* mha: for the 'const' deleted from the line below, see the
comments between the calls to shuffle and deal. */
void deal(int[][13], const char *[], const char *[]);

/* mha: main returns an int. It does so implicitly under the old
standard, but implicit int is no longer part of the language. I have
changed 'main()' to the line below. */
int main(void)
{
/* mha: for the 'const' added to the two lines below, see the
comments between the calls to shuffle and deal. */
const char *suit[4] = { "hearts", "diamonds", "clubs", "spades" };
const char *face[13] =
{ "ace", "deuce", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "jack", "queen", "king"};

/* mha: The initialization 'int deck[4][13]={0};' is ok, but fully
bracketed initializations are better. I have changed it below. */
int deck[4][13] = { {0} };

srand(time(NULL));

shuffle(deck);

/* mha: deal() takes arguments of types const int[][13], const char
*[], and const char *[]. But the supplied argments are none of
them const. Welcome to the world of const poisoning. It is easy
enough to make suit and face arrays of pointers to const char (see
declarations above), but deck cannot be made into an array of
arrays of const int, since shuffle modifies it. This means we need
to change the type of the 1st parameter that deal expects. */
deal(deck, face, suit);

/* mha: the line below referred to a non-standard function 'getch()'.
I have changed it to a standard function. */
getchar();
return 0;
}

void shuffle(int wdeck[][13])
{
/* mha: I have not touched the four statements calling rand() in this
function. The FAQ has much better was of using rand() to get a
value in a range [0,N]. */

int card, row, column;
for (card = 1; card <= 52; card++) {
row = rand() % 4;
column = rand() % 13;

while (wdeck[row][column] != 0) {
row = rand() % 4;
column = rand() % 13;
}
wdeck[row][column] = card;
}
}

/* mha: for the 'const' deleted from the line below, see the
comments between the calls to shuffle and deal. */
void deal(int wdeck[][13], const char *wface[], const char *wsuit[])
{
int card, row, column;

for (card = 1; card <= 52; card++)
for (row = 0; row <= 3; row++)
for (column = 0; column <= 12; column++)

if (wdeck[row][column] == card)

/* mha: The line below had a string literal broken
internally by a line break. I have fixed it. */
printf("%5s of%8s%c", wface[column], wsuit[row],
card % 2 == 0 ? '\n' : '\t');
}






[OP's Code]
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void shuffle(int[][13]);
void deal(const int[][13],const char *[],const char *[]);
main()
{
char * suit[4]={"hearts","diamonds","clubs","spades"};
char * face[13]={"ace","deuce","three","four","five","six","seven"
,"eight","nine","ten","jack","queen","king"};
int deck[4][13]={0};
srand(time(NULL));
shuffle(deck);
deal(deck,face,suit);
getch();
return 0;
}
void shuffle(int wdeck[][13])
{
int card ,row,column;
for(card=1;card<=52;card++)
{
row=rand()%4;
column=rand()%13;
while(wdeck[row][column]!=0)
{
row=rand()%4;
column=rand()%13;
}
wdeck[row][column]=card;
}
}
void deal(const int wdeck[][13],
const char *wface[],
const char *wsuit[])
{
int card,row,column;
for(card=1;card<=52;card++)
for(row=0;row<=3;row++)
for(column=0;column<=12;column++)
if(wdeck[row][column]==card)
printf("%5s of
%8s%c",wface[column],wsuit[row],card%2==0?'\n':'\t');
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top