Thread: Calloc Problem

  1. #1
    Registered User
    Join Date
    Jun 2005

    Calloc Problem

    Good Evening Everyone,

    Before I get started I think I should tell you all that I am a VB programmer, trying to learn C++. To make it worse, my C++ project, is on Intel Dialogic products. And that means most of the examples, are written in standard C. Anyways, I got this code script down from 137 errors down to one error. The error is:

    error C2440: '=' : cannot convert from 'void *' to 'BOARD_INFO *' Conversion from 'void*' to pointer to non-'void' requires an explicit cast

    The definition of BOARD_INFO is:

    typedef struct  {
    	char		name[BUFF_LEN];
    	int*		pChDev;
    	ct_devinfo*			pDevInfo;
    	char		nameB[BUFF_LEN];
    	char		nameC[BUFF_LEN];
    	char		buff[BUFF_LEN];
    	int			dev;
    	int			nr;
    	int			err;
    	int			line;
    	DX_IOTT*	plott;
    	char		nmC[1024];
    	int			bd, ch;
    	int			bdV, chV;
    	char*		pChannel[BUFF_LEN];
    	long		devBRD;
    	LINEDEV*	pLineDev;
    	int			nrChannels;
    I have the following line declaring a global array of BOARD_INFO:

    BOARD_INFO*		gBoardInfo;

    The line I am getting the error on is:

    gBoardInfo = calloc (gBoardCount,sizeof(BOARD_INFO));
    gBoardCount is an integer.

    I do not understand the error. I mean I do, but I don't know how it is becoming an error. Let me say what I think the error is saying, and maybe someone can correct my problem, and maybe even explain the error so that it makes sense to me if I should encounter it again.

    error C2440: '=' : cannot convert from 'void *' to 'BOARD_INFO *' Conversion from 'void*' to pointer to non-'void' requires an explicit cast

    I know that gBoardInfo is an array that hasn't been defined in size. The function will allocate X arrays of BOARD_INFO. But I just don't understand how it see's a "Void" being returned. I dunno, maybe none of what I said makes sense. . . Just wish it.

  2. #2
    Registered User
    Join Date
    May 2006
    Berkshire, UK
    You need to corece the type of the calloc:
    (gBoardInfo *)(calloc ........)
    Alternatively and more C++ would be to use new and delete.
    IF you are new to C++ you might want to consider maing it a class, rather than a struct too. Depends on what you are doing.
    I recommend a good C++ book before you go too far wrong. C++ really does require the right mindset from the start or you can end up in an awful mess.

    Good luck!

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    the void* pointer was intoduced to C as a pointer type that can
    be set to other pointer types - i think it predates casts (making
    one data type look like another). the line of code is legal in
    C since that's what the void pointer was designed for (calloc
    returns void*), but C++ introduced stronger type checks to
    prevent potential programming errors. To get around it, just use
    a simple C style cast - you're line of code becomes the folllowing:

    gBoardInfo = (BOARD_INFO*) calloc (gBoardCount,sizeof(BOARD_INFO));
    Here's a more detailed explanation of the issue

    >>(gBoardInfo *)(calloc ........)

    gBoardInfo is a variable name - a cast involves a data type -
    so michaels-r's method is wrong
    Last edited by Richie T; 05-30-2006 at 04:48 PM.
    No No's:
    fflush (stdin); gets (); void main ();

    Example of fgets (); The FAQ, C/C++ Reference

    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition

    ASCII stupid question, get a stupid ANSI

  4. #4
    Registered User
    Join Date
    May 2006
    Berkshire, UK
    Thanks for the correction - teach me to answer questiosn late at night when my wife is telling me to leave the **** computer alone! :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM