struct member assignment (c-only)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • emorrp1
    New Member
    • Jun 2008
    • 4

    struct member assignment (c-only)

    Hi, I'm now in a job where I have to code in pure C (not C++) and am having trouble adjusting to the differences, hope you can help. As soon as I un-comment any one of the commented lines in the code snippet below, the program fails to compile with a syntax error on the "int a=0..." line after the commented block (This line isn't a problem while the rest is commented out). I haven't been able to identify the problem, even after searching the internet, am I missing something about the assignment operator/structs/arrays in C?

    [CODE=c]
    typedef struct touch_switch {
    int p_loc;
    } touch_switch_t;

    void touch_switches_ set(touch_switc h_t *switches[]);



    void main(void) {
    touch_switch_t S[8];

    /* S[0].p_loc = 0x0;
    S[1].p_loc = 0x1;
    S[2].p_loc = 0x4;
    S[3].p_loc = 0x5;
    S[4].p_loc = 0x2;
    S[5].p_loc = 0x3;
    S[6].p_loc = 0x8;
    S[7].p_loc = 0x9;*/
    /* touch_switches_ set(&S);*/

    int a=0,b=0,c=0,d=0 ,e=0,f=0,g=0,h= 0;
    }

    void touch_switches_ set(touch_switc h_t *switches[]) {
    (*switches)[0].p_loc = 0x0;
    (*switches)[1].p_loc = 0x1;
    (*switches)[2].p_loc = 0x4;
    (*switches)[3].p_loc = 0x5;
    (*switches)[4].p_loc = 0x2;
    (*switches)[5].p_loc = 0x3;
    (*switches)[6].p_loc = 0x8;
    (*switches)[7].p_loc = 0x9;
    }
    [/CODE]
  • emorrp1
    New Member
    • Jun 2008
    • 4

    #2
    The solution (from another board) is:

    "The reason you get an error on the int a ... is that you are not defining your variables before the start of code. In C++ you can literally put a variable definition anywhere any statement can go. Some C compilers allow this too, but far from all. So as soon as you start writing code statements (anything that isn't a type, function or variable declaration/definition), you can not declare any further variables in that block.

    Your function prototype is also missing a () set. "

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      main() also returns an int, whether you're working in C or C++. void main() is nonstandard and poor practice.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        As others already wrote: in a block you first declare and define your variables and
        only then you use them; simply reorder your lines of code. btw, if you're passing
        an array of type T simply pass if to your function and make your function expect
        a T* as its parameter.

        kind regards,

        Jos

        Comment

        • emorrp1
          New Member
          • Jun 2008
          • 4

          #5
          Originally posted by Laharl
          main() also returns an int, whether you're working in C or C++. void main() is nonstandard and poor practice.
          maybe not, but microcontroller s do strange things, including expecting main() to be declared as void (for the one I'm working on at the moment)

          Comment

          • Laharl
            Recognized Expert Contributor
            • Sep 2007
            • 849

            #6
            I stand corrected, then, at least for microcontroller s.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by Laharl
              I stand corrected, then, at least for microcontroller s.
              Don't give up so easy. main() returns an int.

              Anything else is non-ANSI/ISO and it doesn't matter if it's hardware or software.

              Comment

              • emorrp1
                New Member
                • Jun 2008
                • 4

                #8
                Originally posted by weaknessforcats
                Don't give up so easy. main() returns an int.

                Anything else is non-ANSI/ISO and it doesn't matter if it's hardware or software.
                I'm not claiming it's ANSI/ISO standard. But I now know, the reason it's void main() for microprocessors is because there's no operating system to return the int to, and most microprocessor programs end in an infinite loop, so a return value would be useless anyway.

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Originally posted by emorrp1
                  ... the reason it's void main() for microprocessors is because ...
                  "the reason it's void main() for some microprocessors is because". There are many many micro-processors and micro controllers out there that correctly use int main(). There are some that don't. This is non-standard and the code produce for these platforms is non-portable. However lots of these platforms can cope with int main(), void main() is often an extension avoid using it where possible to produce portable code.

                  Many (in fact almost all) compilers have non-standard features and extensions, the better ones also have switches to turn them off so that they compile strictly to standard.

                  One of the stranger extensions I have come across is on the PIC micro-processors which allow you to declare a variable

                  [code=c]
                  long short a;
                  short long b;
                  unsigned long short a;
                  /* etc */
                  [/code]

                  This declared a 3 byte signed or unsigned integer in order to only use the required amount of ram (some PICs have very little ram so those saved bytes can be significant).

                  Also since a PIC basically has a Harvard architecture with ram and rom memory both starting from location 0 you have to indicate where you wanted your pointers to point using the extension keywords ram and rom when declaring pointers.

                  Comment

                  Working...