Round Robin Program warning/segfault

Pointer from integer without a cast.

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2907 Views - Last Post: 09 December 2010 - 02:49 PM Rate Topic: -----

#1 -shadow-  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

Round Robin Program warning/segfault

Posted 09 December 2010 - 09:44 AM

So I'm getting a couple warnings with a program I'm writing
to run through the schedule for a round robin style tournament.
The warnings are causing a segfault and I'm not sure what's going
on so any help would be appreciated.

I'm on my college's school system, the OS is Scientific Linux.

GCC output:

Quote

shadow% gcc main.c
main.c: In function ‘main’:
main.c:53: warning: passing argument 2 of ‘printBoard’ makes integer from pointer without a cast
main.c: At top level:
main.c:62: warning: conflicting types for ‘printBoard’
main.c:12: warning: previous declaration of ‘printBoard’ was here
main.c:94: warning: conflicting types for ‘clearBoard’
main.c:13: warning: previous declaration of ‘clearBoard’ was here


program output:

Quote

shadow% ./a.out
How many players? 5
Enter players' names:
Player 1: bob
Player 2: bill
Player 3: john
Player 4: alex
Player 5: kyle
Segmentation fault


Heres my code:
/*******************************************************************************
****	RoundRobin -> main.c
****	Wesley DeMarco
****	Round Robin Tournament Schedule
****	12/08/10
****..........................................................................*/
#include <stdio.h>
#include <strings.h>
#include <string.h>
#include <stdlib.h>

void printBoard( int, char );
void clearBoard( int, char );
//void robin( int, char );
/*******************************************************************************
****                                                                        ****
***/                                main()                                  /***
****                                                                        ****
*******************************************************************************/
{
/***************************************************
****                DEFINE VARIABLES            ****
***************************************************/
    int pnum = 0,                    // Number of players
           i = 0;                    // Loop Control

/***************************************************
****                GET PLAYER INFO             ****
***************************************************/
    printf( "How many players? " );                  // Prompt user for input
    scanf( "%d", &pnum );                            // Get number of players

    if( !( pnum > 0 && pnum <= 100 ) )		     // Check for valid number
    {
        printf( "Number out of bounds( 1 - 100 )" );
        exit( 0 );
    }

    char pnames[pnum][100];                          // Players' names

    printf( "Enter players' names:\n" );
    for( i = 0; i < pnum; i++ )
    {
        printf( "Player %d: ", i + 1 );	             // Prompt for each player
        scanf( "%s", pnames[i] );                    // Get name from user
    }

/***************************************************
****                CREATE THE BOARD            ****
***************************************************/
    char board[pnum][pnum][1];                       // Board array E = O, X, \
    clearBoard( sizeof( board ) + 1, board );
    printBoard( sizeof( board ) + 1, board );
    //robin( sizeof( board ) + 1, board );
}

/*******************************************************************************
****                                                                        ****
***/            printBoard( int size, char board[size][size] )              /***
****                                                                        ****
*******************************************************************************/
{
/***************************************************
****            DEFINE VARIABLES                ****
***************************************************/
    int i = 0,                // Primary loop control
        j = 0;                // Secondary loop control

/***************************************************
****                PRINT THE BOARD            ****
***************************************************/
    printf( "\t\033[4m" );        // Align the board and start underline
    for( i = 0; i < size; i++ )
    {
        printf( " %d", i + 1 );
    }

    for( i = 0; i < size; i++ )
    {
        printf( "\033[0m%d\t", i + 1 );	            // Left index label
        for( j = 0; j < size; j++ )
        {
            printf( "\033[4m|%s", board[i][j] );    // Print the current cell
        }
        printf( "|\033[0m\n" );                     // Close current line
    }
}

/*******************************************************************************
****                                                                        ****
***/                clearBoard( int size, char board[size][size] )          /***
****                                                                        ****
*******************************************************************************/
{
/***************************************************
****                DEFINE VARIABLES            ****
***************************************************/
    int i = 0,					// Primary loop control
        j = 0;					// Secondary loop control

/***************************************************
****            CLEAR THE BOARD	                ****
***************************************************/
    for( i = 0; i < size; i++ )
    {
        for( j = 0; j < size; j++ )
        {
            if( i == j )
            {
                strcpy( "\\", &board[i][j] );		// Write a \ into board
            } else {
                strcpy( " ", &board[i][j] );		// Write a space
            }
        }
    }
}


Thanks in advance,
~wes

This post has been edited by -shadow-: 09 December 2010 - 11:50 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Round Robin Program warning/segfault

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,568
  • Joined: 25-December 09

Re: Round Robin Program warning/segfault

Posted 09 December 2010 - 09:56 AM

You should treat these warnings as errors.

Quote

main.c:53: warning: passing argument 2 of ‘printBoard’ makes integer from pointer without a cast


In main at line 53

  printBoard( sizeof( board ) + 1, board );


What type of variable is board?

Is it a char?

Remember this is how you prototyped your function: "void printBoard( int, char );"

Also look at the actual function:

printBoard( int size, char board[size][size] )


All three of these should match in the number and the type of variables.

Note: a char is not a char[][].

You should also check out clearBoard().

Jim

This post has been edited by jimblumberg: 09 December 2010 - 09:58 AM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,518
  • Joined: 23-August 08

Re: Round Robin Program warning/segfault

Posted 09 December 2010 - 09:59 AM

DUDE!!! 175 posts and NO CODE TAGS???? WTF???

Meh, you forgot to close them...you had one at the start, but none at the end. I fixed it.
Was This Post Helpful? 0
  • +
  • -

#4 -shadow-  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

Re: Round Robin Program warning/segfault

Posted 09 December 2010 - 11:06 AM

View PostJackOfAllTrades, on 09 December 2010 - 11:59 AM, said:

DUDE!!! 175 posts and NO CODE TAGS???? WTF???

Meh, you forgot to close them...you had one at the start, but none at the end. I fixed it.

Oh I'm sorry man, yeah I missed the [/code]
I was in a rush :sad3:
thanks for fixing it :)
Was This Post Helpful? 0
  • +
  • -

#5 -shadow-  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

Re: Round Robin Program warning/segfault

Posted 09 December 2010 - 11:15 AM

View Postjimblumberg, on 09 December 2010 - 11:56 AM, said:

You should treat these warnings as errors.

Quote

main.c:53: warning: passing argument 2 of ‘printBoard’ makes integer from pointer without a cast


In main at line 53

  printBoard( sizeof( board ) + 1, board );


What type of variable is board?

Is it a char?

Remember this is how you prototyped your function: "void printBoard( int, char );"

Also look at the actual function:

printBoard( int size, char board[size][size] )


All three of these should match in the number and the type of variables.

Note: a char is not a char[][].

You should also check out clearBoard().

Jim

board is a character array:
line 51:
char board[pnum][pnum][1];                    // Board array E = O, X, \


Also, in my prototype I can't say char[][]. C doesn't accept that because
it view char singles and arrays as the same data type with the array size
defined in the actual variable: char notarray, array[5][5];

[edit] sorry for double post :(

This post has been edited by -shadow-: 09 December 2010 - 11:15 AM

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,568
  • Joined: 25-December 09

Re: Round Robin Program warning/segfault

Posted 09 December 2010 - 11:50 AM

Quote

Also, in my prototype I can't say char[][]. C doesn't accept that because


But you prototype should be "char[][5]" or something like that(whatever pnum is). Your functions must also be the same.

When you call the function the parameters must also match the function prototypes.

In your code:
    char board[pnum][pnum][1];                    // Board array E = O, X, \
    clearBoard( sizeof( board ) + 1, board );
   



The first line above is declaring a char variable board which is a 3 dimensional array (notice the 3 sets of []) but not initializing it to any value.

The second line is a call to the function clearBoard() which is expecting parameters of int, char[][pnum] but you are giving it an int and char[][][1] So even if your function prototype and actual function were the same they would not match this call.

You probably should refresh yourself on how arrays work, check out this link: arrays.

Then you probably should look at these links as well: Functions I and Functions II.

Also note most of your functions you have left off the return types, and although C will default to return an int you should specify the return value.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 -shadow-  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

Re: Round Robin Program warning/segfault

Posted 09 December 2010 - 12:00 PM

what if I changed line 59:
printBoard( int size, char board[size][size] )

to:
printBoard( int size, char board[size][size][1] )

[edit] Also, I'm declaring the return type as void in my prototypes
wouldn't that carry over? or not?

This post has been edited by -shadow-: 09 December 2010 - 12:01 PM

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,568
  • Joined: 25-December 09

Re: Round Robin Program warning/segfault

Posted 09 December 2010 - 12:10 PM

You must change the function prototype, function, and function call. All 3 must match in the number and type of parameters..

After seeing the does passing an board[][][1] to your function make any sense?

What is the purpose of an array with the size of 1?

What is the purpose of your board array inside the function?

What kind of data will the array hold? Will it be a single character, or a character array?


Jim
Was This Post Helpful? 0
  • +
  • -

#9 -shadow-  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

Re: Round Robin Program warning/segfault

Posted 09 December 2010 - 12:21 PM

View Postjimblumberg, on 09 December 2010 - 02:10 PM, said:

You must change the function prototype, function, and function call. All 3 must match in the number and type of parameters..

After seeing the does passing an board[][][1] to your function make any sense?

What is the purpose of an array with the size of 1?

What is the purpose of your board array inside the function?

What kind of data will the array hold? Will it be a single character, or a character array?


Jim

C does not have strings, instead it uses character arrays.
if I define board as char board[size][size], it will treat board[size] as a string.
I don't want it to do that. hence the 3rd dimension with size 1. otherwise, assuming
int size = 5, and the array contains:
\OOOO
X\OOO
XX\OO
XXX\O
XXXX\


if I called the array as char board[size][size]

board[3][2] should be equal to 'X'
but instead C would give me:
board[3][2] = X\O\0

See what I'm saying?

This post has been edited by -shadow-: 09 December 2010 - 12:22 PM

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,568
  • Joined: 25-December 09

Re: Round Robin Program warning/segfault

Posted 09 December 2010 - 12:30 PM

Quote

C does not have strings, instead it uses character arrays.
if I define board as char board[size][size], it will treat board[size] as a string.


No C will treat this a an array of characters. C does not automaticly add the "'\0'" character to the end of an array of characters.

Jim
Was This Post Helpful? 1
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,518
  • Joined: 23-August 08

Re: Round Robin Program warning/segfault

Posted 09 December 2010 - 12:43 PM

Quote

char board[size][size], it will treat board[size] as a string

Like Jim said, you're failing to understand C's handling of char arrays.

A char array is just that, an array of characters. The only time ta character array is treated as a string is when you use the standard string library functions like strlen, strcpy, etc. It's an array of characters period; only functions that expect a null-terminated character array care about the null terminator.
Was This Post Helpful? 1
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,568
  • Joined: 25-December 09

Re: Round Robin Program warning/segfault

Posted 09 December 2010 - 12:58 PM

Quote

[edit] Also, I'm declaring the return type as void in my prototypes
wouldn't that carry over? or not?


No. The actual function and the function prototype must match.

Jim
Was This Post Helpful? 1
  • +
  • -

#13 -shadow-  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

Re: Round Robin Program warning/segfault

Posted 09 December 2010 - 01:45 PM

thanks for your help guys,
I've got it narrowed down to two warnings:

Quote

shadow% gcc main.c
main.c: In function ‘clearBoard’:
main.c:112: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast
main.c:114: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast


Heres the segment of code this is looking at:
if( i == j )
{
    strcpy( "\\", board[i][j] );    // Write a \ into board
} else {
    strcpy( " ", board[i][j] );     // Write a space
}


Thanks again for all your help,
~Wes
Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 4074
  • View blog
  • Posts: 12,568
  • Joined: 25-December 09

Re: Round Robin Program warning/segfault

Posted 09 December 2010 - 01:53 PM

Quote

A char array is just that, an array of characters. The only time a character array is treated as a string is when you use the standard string library functions like strlen, strcpy, etc. It's an array of characters period; only functions that expect a null-terminated character array care about the null terminator.


So are you now using C-strings? That is the only way strcpy() will work, it needs the null character as the terminator.

However from the error message: have you changed the board array to ints?

Please show how you have defined your array "board", please post the modified code.

Jim

This post has been edited by jimblumberg: 09 December 2010 - 01:54 PM

Was This Post Helpful? 0
  • +
  • -

#15 -shadow-  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 204
  • Joined: 18-November 10

Re: Round Robin Program warning/segfault

Posted 09 December 2010 - 02:18 PM

I realized I wasn't dereferencing board in strcpy.
Its not giving any errors now but I'm still getting
a segfault from strcpy:

Heres where I'm defining board:
// In main
char board[pnum][pnum];

Heres clearBoard():
/*******************************************************************************
****                                                                        ****
***/        void clearBoard( int size, char board[size][size] )             /***
****                                                                        ****
*******************************************************************************/
{
/***************************************************
****                DEFINE VARIABLES            ****
***************************************************/
    int i = 0,                  // Primary loop control
        j = 0;                  // Secondary loop control

/***************************************************
****                CLEAR THE BOARD             ****
***************************************************/
    for( i = 0; i < size; i++ )
    {
        for( j = 0; j < size; j++ )
        {
            if( i == j )
            {
                // SEGFAULT HAPPENS HERE //
                strcpy( "\\", &board[i][j] );       // Write a \ into board
            } else {
                strcpy( " ", &board[i][j] );        // Write a space
            }
            printf( "\n" );
        }
    }
}


Heres where its segfaulting:

Quote

shadow% ./a.out
How many players? 5
Enter players' names:
Player 1: bob
Player 2: billy
Player 3: joe
Player 4: john
Player 5: jack
Segmentation fault

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2