What's Wrong With This?

This is a discussion on What's Wrong With This? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { char cBoard[3][3] = {0}; int x; int y; int z ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    What's Wrong With This?

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
    	char cBoard[3][3] = {0};
    	int x;
    	int y;
    	int z = 0;
    	int iSquare;
    	printf("Enter a square number <1-9>: ");
    	scanf("%d", &iSquare);
    	for (x=1; x <= 3; x++)
    	{
    		for (y=1; y <= 3; y++)
    		{ 
    			z++;
    			if (iSquare == z)
    			{
    				if (cBoard[x-1][y-1] == NULL)
    				{
    					printf("\n\n%d is Empty!\n\n", iSquare);
    				}
    				else
    				{	
    					printf("looped %d times", z);
    					printf("\n\n%d IS FULL!\n\n", iSquare);
    				}
    			}
    		}
    	}
    return 0;
    }
    [mag1x@localhost ~]$ gcc wtf.c -o wtf
    wtf.c: In function ‘main’:
    wtf.c:21: warning: comparison between pointer and integer

    I don't understand. Does that mean my program is wrong? Thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > if (cBoard[x-1][y-1] == NULL)
    I'm guessing NULL should be 0 or '\0'
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    I don't understand. Does that mean my program is wrong?
    This is a warning so your program is compiled and you can execute it but except if you know precisely what you're doing and what's the reason of the warning, there are chances the execution does not produce what you expected. Try to compile with the -Werror flag so that warnings are considered as errors.

    Concerning your code:

    You use a char array:
    Code:
    char cBoard[3][3] = {0};
    and you try to compare one of its element to a pointer:
    Code:
    cBoard[x-1][y-1] == NULL
    Note: NULL is probably declared as (void*)0.
    As a character is not a pointer, you don't have to compare those 2 different objects, hence the compiler warning. Replace NULL by 0.

  4. #4
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Quote Originally Posted by root4 View Post
    This is a warning so your program is compiled and you can execute it but except if you know precisely what you're doing and what's the reason of the warning, there are chances the execution does not produce what you expected. Try to compile with the -Werror flag so that warnings are considered as errors.

    Concerning your code:

    You use a char array:
    Code:
    char cBoard[3][3] = {0};
    and you try to compare one of its element to a pointer:
    Code:
    cBoard[x-1][y-1] == NULL
    Note: NULL is probably declared as (void*)0.
    As a character is not a pointer, you don't have to compare those 2 different objects, hence the compiler warning. Replace NULL by 0.

    So if I was to replace:

    Code:
    char cBoard[3][3] = {0};
    with:

    Code:
    char cBoard[3][3] = {NULL};
    that would fix the problem? Or am I not understanding you properly?

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    No, the code should be something along the lines of:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
    	char cBoard[3][3] = {0};
    	int x;
    	int y;
    	int z = 0;
    	int iSquare;
    	printf("Enter a square number <1-9>: ");
    	scanf("%d", &iSquare);
    	for (x=1; x <= 3; x++)
    	{
    		for (y=1; y <= 3; y++)
    		{ 
    			z++;
    			if (iSquare == z)
    			{
    				if (cBoard[x-1][y-1] == 0)
    				{
    					printf("\n\n%d is Empty!\n\n", iSquare);
    				}
    				else
    				{	
    					printf("looped %d times", z);
    					printf("\n\n%d IS FULL!\n\n", iSquare);
    				}
    			}
    		}
    	}
    return 0;
    }
    NULL is defined as (void*)0. You cannot compare a pointer to a non-pointer, and you can't compare a pointer of one type to another type.
    (Or you can, but you shouldn't.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Elysia View Post
    NULL is defined as (void*)0.
    No it's not, but it could be.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    If it could be defined as (void*)0, then it cannot not defined as (void*)0
    It can be one, but not both!
    But in any case, it seems to be defined as (void*)0 in this case.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Whatever the case, NULL == NULL

    And that's all that matters.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    I think we can agree to that
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,658
    If it could be defined as (void*)0, then it cannot not defined as (void*)0
    That NULL could be defined as (void*)0 does not imply that it is defined as (void*)0. C99 states that the macro NULL "expands to an implementation-defined null pointer constant" and that "an integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    It was a figure of speech. If something is X (not Y), then it cannot be of Y at the same time.
    Or, in other words, if NULL is not defined to (void*)0 (X), then it cannot be defined to (void*)0 (Y).
    It can be one of those possibilities, but not both!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    In a given implementations that is true but it doesn't hold over all implementations.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    #define NULL (rand()? 0 : (void*)0)


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21