locked
simple C question! RRS feed

  • Question

  • Hello,

    I don't know what is going on this morning... I did this so many times in the passed and now it doesn't work anymore. Can someone please tell me what it is I am forgetting!

    #include <stdio.h>
    #include <iostream>
    using namespace std;
     
    typedef struct tag_button_size
    {
    unsigned char button_set_number;		
    signed char *mb_name;
    //...
    }button_size;
    
    typedef struct drawB{	button_size bsz;   
    //...
    } DRAWB;
                               
    DRAWB* DB_config_draw_b(button_size	*bsz)
    {
    DRAWB *objDrawB;
    objDrawB = malloc (sizeof (struct drawB)); //<<< error points here!
    objDrawB->bsz = bsz;	      		
    //...
    return objDrawB;
    }
    
    DB_free_db(DRAWB *obj_db)  
    {
    free(obj_db);
    obj_db = NULL;
    }
    
    int main()
    {
    DRAWB *objDrawB;
    button_size bsz;
    
    bsz.mb_name = "mb36"; 
    bsz.button_set_number =0; 
    
    objDrawB = DB_config_draw_b(&bsz);  
    DB_free_db(objDrawB);
    }
    
    
    
    
    I get the following error:

    1>c:\_programming\_microchip_issues\simulated in vc++\structbug\structbug\a.cpp(21) : error C2440: '=' : cannot convert from 'void *' to 'DRAWB *'

    In main(), I should be able to assign data to the button_size structure and then pass its address this way.... no?

    All help very appreciated!

    Rob

    Wednesday, September 29, 2010 5:04 PM

Answers

  • _roberto_ wrote:
    >
    > I compiled and I got the following errors. I put the code in main() back the way it was... and I still get the following errors.
    >
    >1>------ Build started: Project: x.c, Configuration: Debug Win32 ------
    >1>Compiling...
    >1>a.c
    >1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(39) : error C2143: syntax error : missing '{' before ':'
    >1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(39) : error C2059: syntax error : ':'
    >1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2143: syntax error : missing '{' before ':'
    >1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2059: syntax error : ':'
     
    You are compiling a C file, but you have #include <cstdio>, which is a C++
    header. That's the cause of ALL of these errors.
     
    If you really want to work in straight C, use <stdio.h>, not <cstdio>. If
    you really want to work in C++, change your file's extension to .cpp.
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.
     

    Tim Roberts, DDK MVP
    • Marked as answer by Yi Feng Li Thursday, October 7, 2010 4:19 AM
    Friday, October 1, 2010 6:18 AM

All replies

  • malloc returns void, maybe it is because of type checking you turned on or off in compiler. I suggest (not sure)  to turn off/on Enable Run-Time Type Information.
    1. Whiter than the white is UV bright! 2. Eat your vegetables or you are going to turn in to fat dumb asses! 3. Fruits won't cut it 4 wheel drive with levers to lock differentials was a hack
    Wednesday, September 29, 2010 5:09 PM
  • This is not C code, it is C++, and C++ does not allow implicit conversions from void* to other pointer types. Either use new instead of malloc or add an explicit cast.
    Wednesday, September 29, 2010 5:21 PM
  • Hello _roberto_,

     

    1. The return value of malloc() is void*. This return void pointer must be cast to a pointer to a DRAWB struct, as follows :

    objDrawB = (DRAWB*)malloc (sizeof (struct drawB));

     

    2. Furthermore, the line :

    objDrawB->bsz = bsz;

    would also cause a compilation error because DRAWB::bsz is of type button_size and the right hand side value "bsz" is a pointer to a button_size.

    Hence it would need to be changed to something like :

    objDrawB->bsz = *bsz;

     

    3. Finally, in the main() function, the line :

    bsz.mb_name = "mb36";

    will need to be changed to :

    bsz.mb_name = (signed char*)"mb36";

     

    - Bio.

     

    Wednesday, September 29, 2010 5:23 PM
  • Hi Ling Bio Liong,

    Thankyou for replying!

    I did the changes you suggested and it compiled fine for a few times.

    Then I added in the following line:

    strncpy(i, (const char*) objDrawB->bsz->mb_name, 4);

    in main() to confirm the contents of bsz.mb_name, like this,

    int main()
    {
    button_size bsz;
    DRAWB *objDrawB;
    signed char i[4];
    
    bsz.mb_name = (signed char*) "mb36"; 
    bsz.button_set_number =0; 
    
    objDrawB = DB_config_draw_b(&bsz);  
    strncpy(i, (const char*) objDrawB->bsz->mb_name, 4);
    DB_free_db(objDrawB);
    }
    

     I compiled and I got the following errors. I put the code in main() back the way it was... and I still get the following errors.

     

    1>------ Build started: Project: x.c, Configuration: Debug Win32 ------

    1>Compiling...

    1>a.c

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(39) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(39) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(42) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(43) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(44) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(45) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(46) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(47) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(47) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(47) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(47) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(47) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(47) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(48) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(48) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(48) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(48) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(48) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(48) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(49) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(49) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(49) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(49) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(50) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(50) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(50) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(50) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(51) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(51) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(51) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(51) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(51) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(51) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(52) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(52) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(52) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(52) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(52) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(52) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(53) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(53) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(53) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(53) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(53) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(53) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(54) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(54) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(54) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(54) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(54) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(54) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(55) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(55) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(55) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(55) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(55) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(55) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(56) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(56) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(21) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : error C2059: syntax error : ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : error C2143: syntax error : missing '{' before ':'

    1>c:\program files\microsoft visual studio 9.0\vc\include\cstdlib(23) : fatal error C1003: error count exceeds 100; stopping compilation

    1>Build log was saved at "file://c:\_PROGRAMMING\_MICROCHIP_ISSUES\SIMULATED IN VC++\structBug3\x.c\x.c\Debug\BuildLog.htm"

    1>x.c - 102 error(s), 0 warning(s)

    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    completely discouraged!

    Rob

    Wednesday, September 29, 2010 5:59 PM
  • objDrawB = (DRAWB *)DDB_config_draw_b(&bsz);
    

    I could not find you casting in your code as others mentioning. Probably one thing you may have to do is cast as above and see if it reduces the errors?

     


    akx Solutions

    Vote if helpful (or) Mark it Answered if answered your query.
    Wednesday, September 29, 2010 6:33 PM
  • Those errors indicate that the problem in your code is before your #includes, which is not code you have shown us.
    Wednesday, September 29, 2010 6:37 PM
  • Hi ildjarn,

    If I double click on any of those errors, they take me to code I did not write??? It takes me to the cstdlib file which is one of the internal compiler files ... I guess!!!

    But, I went a few versions back of the sample code... (that is before these errors ever surfaced)  and I am continuing from there.

    Today's problem is far from solved, I am having a bad day. I will get back with further debugging problems in other posts.

    Thanks!

    Rob

    Wednesday, September 29, 2010 6:46 PM
  • Hello Lin Bio Liong,

    >2. Furthermore, the line :

    >objDrawB->bsz = bsz;

    >would also cause a compilation error because DRAWB::bsz is of type button_size and the right hand side value "bsz" is a pointer to a button_size.

    >Hence it would need to be changed to something like :

    >objDrawB->bsz = *bsz;

     I left it like this:

    objDrawB->bsz = bsz;

    and it still works. I guess your other two suggestions solved the issue at hand.

    Thanks Bio

    Rob

     

    Wednesday, September 29, 2010 6:51 PM
  • _roberto_ wrote:
    >
    > I compiled and I got the following errors. I put the code in main() back the way it was... and I still get the following errors.
    >
    >1>------ Build started: Project: x.c, Configuration: Debug Win32 ------
    >1>Compiling...
    >1>a.c
    >1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(39) : error C2143: syntax error : missing '{' before ':'
    >1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(39) : error C2059: syntax error : ':'
    >1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2143: syntax error : missing '{' before ':'
    >1>c:\program files\microsoft visual studio 9.0\vc\include\cstdio(41) : error C2059: syntax error : ':'
     
    You are compiling a C file, but you have #include <cstdio>, which is a C++
    header. That's the cause of ALL of these errors.
     
    If you really want to work in straight C, use <stdio.h>, not <cstdio>. If
    you really want to work in C++, change your file's extension to .cpp.
    --
    Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.
     

    Tim Roberts, DDK MVP
    • Marked as answer by Yi Feng Li Thursday, October 7, 2010 4:19 AM
    Friday, October 1, 2010 6:18 AM
  • Thanks Tim!
    Friday, October 1, 2010 7:29 PM