struct member assignment (c-only)

This is a discussion on struct member assignment (c-only) within the C Programming forums, part of the General Programming Boards category; Hi, I'm now in a job where I have to code in pure C (not C++) and am having trouble ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    7

    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:
    typedef struct touch_switch {
    	int p_loc;
    } touch_switch_t;
    
    void touch_switches_set(touch_switch_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_switch_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;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    In C89, you cannot declare variables once you have done a line of code. You would have to move the variable definitions to the top of the function.

    And there is no such thing as void main() in C or in C++.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Shift those declarations to before the other statements:
    Code:
    int main(void) {
    	touch_switch_t S[8];
    	int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0;
    
    	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);
    	return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    The following code is compiling correctly on my machine with gcc -Wall -ansi -pedantic:
    Code:
    typedef struct touch_switch {
    	int p_loc;
    } touch_switch_t;
    
    void touch_switches_set(touch_switch_t (*switches)[]);
    
    
    int main(void) {
    	touch_switch_t S[8];
    	int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0;
    
    	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);
    
    	return 0;
    }
    
    void touch_switches_set(touch_switch_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;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    7
    Thank you all very much, this solution works. I've not come across the "define all variables at the start" rule before, and also thanks for solving a mysterious "suspicious pointer conversion" compiler warning, that I had no idea what to do with, but wasn't critical.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by emorrp1 View Post
    Thank you all very much, this solution works. I've not come across the "define all variables at the start" rule before, and also thanks for solving a mysterious "suspicious pointer conversion" compiler warning, that I had no idea what to do with, but wasn't critical.
    It wasn't "critical" becaue you were lucky that the start of the array and the address of the first element are the same thing - if you had wanted to do a slightly different variant, then it would quite possibly have been critical.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I may be missing something, but it seems you could simply define the function like so:
    Code:
    void touch_switches_set(touch_switch_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;
    }

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    7
    Quote Originally Posted by swoopy View Post
    I may be missing something, but it seems you could simply define the function like so:
    Code:
    void touch_switches_set(touch_switch_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;
    }
    you're almost right! I tried it, but since you're now dealing with the pointer directly rather than its dereferenced state, you need to also change the '.' to '->'

    Quote Originally Posted by tabstop
    And there is no such thing as void main() in C or in C++.
    maybe not, but microcontrollers do strange things, including expecting main() to be declared as void (for the one I'm working on at the moment)

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by emorrp1 View Post
    you're almost right! I tried it, but since you're now dealing with the pointer directly rather than its dereferenced state, you need to also change the '.' to '->'
    No, that's incorrect. The index operator [] dereferences the pointer.
    void* p;
    *p and p[0] are the same thing.
    So (*p).something and p[0].something are also the same thing.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. memory allocation for flexible array member of struct
    By jcarouth in forum C Programming
    Replies: 3
    Last Post: 09-11-2005, 12:19 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Replies: 2
    Last Post: 08-05-2002, 04:52 PM

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