function i'm writing (trying to atleast)

This is a discussion on function i'm writing (trying to atleast) within the C Programming forums, part of the General Programming Boards category; hey, i'm trying to write a function that checks whether an array element (chosen by the user's input) is empty ...

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 04:45 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01: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

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