Thread: function i'm writing (trying to atleast)

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

    function i'm writing (trying to atleast)

    hey, i'm trying to write a function that checks whether an array element (chosen by the user's input) is empty (aka NULL) or full (aka has a value in it). there is lots of code missing in this, because it's actually going in another program, but yeah, rewrite it like this to test out the function, and I can't seem to get it to work. The problem is what ever value I enter when prompted, returns "%d is full", where %d is my value. the code is pretty confusing, well atleast to me, but hopefully you can understand it and what i'm trying to do. Thanks!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
    	char cBoard[3][3];
    	int x;
    	int y;
    	int z = 0;
    	int iSquare;
    	printf("Enter a square number <1-9>: ");
    	scanf("%d", &iSquare);
    	memset(&cBoard,0,sizeof(cBoard));
    	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("\n\n%d IS FULL!\n\n", iSquare);
    				}
    			}
    		}
    	}
    return 0;
    }
    p.s this is for a tick tack toe i'm trying to create. so yeah, the user input is selecting which square to put his mark in, 1-9, and this function will check whether the square is being used or not (if i can get it to work properly that is) when i pass it later in main() or something.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The address of a valid element is never going to be NULL.
    You seem to confuse pointers.
    A NULL pointer is a pointer that pointers to 0, hence an invalid pointer. An invalid pointer cannot be used at all, because it will generate a seg fault.

    Code:
    memset(&cBoard,0,sizeof(cBoard));
    This is also incorrect. Remove the &. Arrays are automatically passed as pointers.
    http://cpwiki.sourceforge.net/A_pointer_on_pointers
    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.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Elysia View Post

    Code:
    memset(&cBoard,0,sizeof(cBoard));
    This is also incorrect. Remove the &. Arrays are automatically passed as pointers.
    http://cpwiki.sourceforge.net/A_pointer_on_pointers
    Better just to use array initialization

    Code:
    char cBoard[3][3] = {0};
    and avoid memset
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. help with writing function that multiplys the radius
    By Earl Lee in forum C Programming
    Replies: 6
    Last Post: 10-03-2002, 04:33 PM