Thread: type casting problem?

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    3

    type casting problem?

    I am attempting to compile some code that I got in an ISO document for barcode generation. When I try to compile it in MS Visual C++, I get the following error:

    mappings.cpp(147) : error C2440: '=' : cannot convert from 'void *' to 'int *'
    I have put the line in question in bold and red below. Does anyone have any suggestions? Thanks in advance.

    Code:
    #include <stdio.h>
    #include <alloc.h>
    #include <stdlib.h>
    
    int nrow, ncol, *array;
    
    /* "module" places "chr+bit" with appropriate wrapping within array[] */
    void module(int row, int col, int chr, int bit)
    {
    	if (row < 0) { row += nrow; col += 4 - ((nrow+4)%8); }
    	if (col < 0) { col += ncol; row += 4 - ((ncol+4)%8); }
    	array[row*ncol+col] = 10*chr + bit;
    }
    
    
    /* "utah" places the 8 bits of a utah-shaped symbol character in ECC200 */
    void utah(int row, int col, int chr)
    { 
    	module(row-2,col-2,chr,1);
    	module(row-2,col-1,chr,2);
    	module(row-1,col-2,chr,3);
    	module(row-1,col-1,chr,4);
    	module(row-1,col,chr,5);
    	module(row,col-2,chr,6);
    	module(row,col-1,chr,7);
    	module(row,col,chr,8);
    }
    
    
    /* "cornerN" places 8 bits of the four special corner cases in ECC200 */
    void corner1(int chr)
    {
    	module(nrow-1,0,chr,1);
    	module(nrow-1,1,chr,2);
    	module(nrow-1,2,chr,3);
    	module(0,ncol-2,chr,4);
    	module(0,ncol-1,chr,5);
    	module(1,ncol-1,chr,6);
    	module(2,ncol-1,chr,7);
    	module(3,ncol-1,chr,8);
    }
    
    void corner2(int chr)
    { 
    	module(nrow-3,0,chr,1);
    	module(nrow-2,0,chr,2);
    	module(nrow-1,0,chr,3);
    	module(0,ncol-4,chr,4);
    	module(0,ncol-3,chr,5);
    	module(0,ncol-2,chr,6);
    	module(0,ncol-1,chr,7);
    	module(1,ncol-1,chr,8);
    }
    
    void corner3(int chr)
    { 
    	module(nrow-3,0,chr,1);
    	module(nrow-2,0,chr,2);
    	module(nrow-1,0,chr,3);
    	module(0,ncol-2,chr,4);
    	module(0,ncol-1,chr,5);
    	module(1,ncol-1,chr,6);
    	module(2,ncol-1,chr,7);
    	module(3,ncol-1,chr,8);
    }
    
    void corner4(int chr)
    { 
    	module(nrow-1,0,chr,1);
    	module(nrow-1,ncol-1,chr,2);
    	module(0,ncol-3,chr,3);
    	module(0,ncol-2,chr,4);
    	module(0,ncol-1,chr,5);
    	module(1,ncol-3,chr,6);
    	module(1,ncol-2,chr,7);
    	module(1,ncol-1,chr,8);
    }
    
    
    /* "ecc200" fills an nrow x ncol array with appropriate values for ECC200 */
    void ecc200(void)
    { 
    	int row, col, chr;
    
    	/* First, fill the array[] with invalid entries */
    	for (row=0; row<nrow; row++) {
    
    		for (col=0; col<ncol; col++) {
    			array[row*ncol+col] = 0;
    		}
    	}
    
    
    /* Starting in the correct location for character #1, bit 8,... */
    chr = 1; row = 4; col = 0;
    do {
    	/* repeatedly first check for one of the special corner cases, then... */
    	if ((row == nrow) && (col == 0)) corner1(chr++);
    	if ((row == nrow-2) && (col == 0) && (ncol%4)) corner2(chr++);
    	if ((row == nrow-2) && (col == 0) && (ncol%8 == 4)) corner3(chr++);
    	if ((row == nrow+4) && (col == 2) && (!(ncol%8))) corner4(chr++);
    
    
    	/* sweep upward diagonally, inserting successive characters,... */
    	do {
    
    		if ((row < nrow) && (col >= 0) && (!array[row*ncol+col]))
    			utah(row,col,chr++);
    			row -= 2; col += 2;
    			}
    		
    		while ((row >= 0) && (col < ncol));
    			row += 1; col += 3;
    
    		/* & then sweep downward diagonally, inserting successive characters,... */
    		do {
    
    			if ((row >= 0) && (col < ncol) && (!array[row*ncol+col]))
    			utah(row,col,chr++);
    			row += 2; col -= 2;
    		} 
    		while ((row < nrow) && (col >= 0));
    			row += 3; col += 1;
    		/* ... until the entire array is scanned */
    	} while ((row < nrow) || (col < ncol));
    		/* Lastly, if the lower righthand corner is untouched, fill in fixed pattern */
    		if (!array[nrow*ncol-1]) {
    			array[nrow*ncol-1] = array[nrow*ncol-ncol-2] = 1;
    		}
    }
    
    
    /* "main" checks for valid command line entries, then computes & displays array */
    void main(int argc, char *argv[])
    {
    	int x, y, z;
    
    	if (argc <= 3) {
    		printf("Command line: ECC200 #_of_Data_Rows #_of_Data_Columns\n");
    	}
    	else {
    		nrow = ncol = 0;
    		nrow = atoi(argv[1]); ncol = atoi(argv[2]);
    		
    		if ((nrow >= 6) && (~nrow&0x01) && (ncol >= 6) && (~ncol&0x01)) {
    			
    			array = malloc(sizeof(int) * nrow * ncol);
    			
    			ecc200();
    			
    			for (x=0; x<nrow; x++) {
    				for (y=0; y<ncol; y++) {
    				z = array[x*ncol+y];
    					if (z == 0) printf(" WHI");
    					else if (z == 1) printf("BLK");
    					else printf("%3d.%d",z/10,z%10);
    				}
    				
    				printf("\n");
    			}
    
    			free(array);
    		}
    	}
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop compiling as C++. While you're at it, main is not a void function. It returns an integer.


    Quzah.
    Last edited by quzah; 01-30-2006 at 10:16 AM. Reason: p
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    When I compile it as a .c file, it throws up even more errors - all of which are in the "c:\program files\microsoft visual studio\vc98\include\stl_alloc.h" file.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you even including alloc.h? What, is stdlib.h not good enough for you?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    3
    I'm just trying to compile the code I was given. I can send you the author's address if it irks you so much.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    #include <stdio.h>
    /*!! #include <alloc.h>*/
    #include <stdlib.h>
    
    int nrow, ncol, *array;
    
    /* "module" places "chr+bit" with appropriate wrapping within array[] */
    void module(int row, int col, int chr, int bit)
    {
        if (row < 0) { row += nrow; col += 4 - ((nrow+4)%8); }
        if (col < 0) { col += ncol; row += 4 - ((ncol+4)%8); }
        array[row*ncol+col] = 10*chr + bit;
    }
    
    
    /* "utah" places the 8 bits of a utah-shaped symbol character in ECC200 */
    void utah(int row, int col, int chr)
    {
        module(row-2,col-2,chr,1);
        module(row-2,col-1,chr,2);
        module(row-1,col-2,chr,3);
        module(row-1,col-1,chr,4);
        module(row-1,col,chr,5);
        module(row,col-2,chr,6);
        module(row,col-1,chr,7);
        module(row,col,chr,8);
    }
    
    
    /* "cornerN" places 8 bits of the four special corner cases in ECC200 */
    void corner1(int chr)
    {
        module(nrow-1,0,chr,1);
        module(nrow-1,1,chr,2);
        module(nrow-1,2,chr,3);
        module(0,ncol-2,chr,4);
        module(0,ncol-1,chr,5);
        module(1,ncol-1,chr,6);
        module(2,ncol-1,chr,7);
        module(3,ncol-1,chr,8);
    }
    
    void corner2(int chr)
    {
        module(nrow-3,0,chr,1);
        module(nrow-2,0,chr,2);
        module(nrow-1,0,chr,3);
        module(0,ncol-4,chr,4);
        module(0,ncol-3,chr,5);
        module(0,ncol-2,chr,6);
        module(0,ncol-1,chr,7);
        module(1,ncol-1,chr,8);
    }
    
    void corner3(int chr)
    {
        module(nrow-3,0,chr,1);
        module(nrow-2,0,chr,2);
        module(nrow-1,0,chr,3);
        module(0,ncol-2,chr,4);
        module(0,ncol-1,chr,5);
        module(1,ncol-1,chr,6);
        module(2,ncol-1,chr,7);
        module(3,ncol-1,chr,8);
    }
    
    void corner4(int chr)
    {
        module(nrow-1,0,chr,1);
        module(nrow-1,ncol-1,chr,2);
        module(0,ncol-3,chr,3);
        module(0,ncol-2,chr,4);
        module(0,ncol-1,chr,5);
        module(1,ncol-3,chr,6);
        module(1,ncol-2,chr,7);
        module(1,ncol-1,chr,8);
    }
    
    
    /* "ecc200" fills an nrow x ncol array with appropriate values for ECC200 */
    void ecc200(void)
    {
        int row, col, chr;
    
        /* First, fill the array[] with invalid entries */
        for (row=0; row<nrow; row++) {
    
            for (col=0; col<ncol; col++) {
                array[row*ncol+col] = 0;
            }
        }
    
    
    /* Starting in the correct location for character #1, bit 8,... */
    chr = 1; row = 4; col = 0;
    do {
        /* repeatedly first check for one of the special corner cases, then... */
        if ((row == nrow) && (col == 0)) corner1(chr++);
        if ((row == nrow-2) && (col == 0) && (ncol%4)) corner2(chr++);
        if ((row == nrow-2) && (col == 0) && (ncol%8 == 4)) corner3(chr++);
        if ((row == nrow+4) && (col == 2) && (!(ncol%8))) corner4(chr++);
    
    
        /* sweep upward diagonally, inserting successive characters,... */
        do {
    
            if ((row < nrow) && (col >= 0) && (!array[row*ncol+col]))
                utah(row,col,chr++);
                row -= 2; col += 2;
                }
    
            while ((row >= 0) && (col < ncol));
                row += 1; col += 3;
    
            /* & then sweep downward diagonally, inserting successive characters,... */
            do {
    
                if ((row >= 0) && (col < ncol) && (!array[row*ncol+col]))
                utah(row,col,chr++);
                row += 2; col -= 2;
            }
            while ((row < nrow) && (col >= 0));
                row += 3; col += 1;
            /* ... until the entire array is scanned */
        } while ((row < nrow) || (col < ncol));
            /* Lastly, if the lower righthand corner is untouched, fill in fixed pattern */
            if (!array[nrow*ncol-1]) {
                array[nrow*ncol-1] = array[nrow*ncol-ncol-2] = 1;
            }
    }
    
    
    /* "main" checks for valid command line entries, then computes & displays array */
    int  /* NOT void */ main(int argc, char *argv[])
    {
        int x, y, z;
    
        if (argc <= 3) {
            printf("Command line: ECC200 #_of_Data_Rows #_of_Data_Columns\n");
        }
        else {
            nrow = ncol = 0;
            nrow = atoi(argv[1]); ncol = atoi(argv[2]);
    
            if ((nrow >= 6) && (~nrow&0x01) && (ncol >= 6) && (~ncol&0x01)) {
    
                array = malloc(sizeof(int) * nrow * ncol);
    
                ecc200();
    
                for (x=0; x<nrow; x++) {
                    for (y=0; y<ncol; y++) {
                    z = array[x*ncol+y];
                        if (z == 0) printf(" WHI");
                        else if (z == 1) printf("BLK");
                        else printf("%3d.%d",z/10,z%10);
                    }
    
                    printf("\n");
                }
    
                free(array);
            }
        }
        return 0; /*!! added */
    }
    Compiles just fine here with gcc, when compiled as a C program.

    > all of which are in the "c:\program files\microsoft visual studio\vc98\include\stl_alloc.h" file.
    Which is a C++ header file - C doesn't know about STL.
    Remove that redundant alloc.h file (as per my example).

    > I'm just trying to compile the code I was given. I can send you the author's address if it irks you so much.
    So are you here to learn, or just here to dump code you've been given and hope someone will fix the problems for you?

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    your progrome seems to work fine with DEV C++ compiler. provided if u compile with the .c extention and removing alloc.h

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. difference between type conversion and type casting
    By Bargi in forum C Programming
    Replies: 1
    Last Post: 01-23-2007, 03:17 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Type casting
    By Lionmane in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 02:16 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. qwerty/azerty keyboard type problem + question about loop.
    By Robin Hood in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2002, 01:03 PM