473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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?

Expand|Select|Wrap|Line Numbers
  1. typedef struct touch_switch {
  2.     int p_loc;
  3. } touch_switch_t;
  4.  
  5. void touch_switches_set(touch_switch_t *switches[]);
  6.  
  7.  
  8.  
  9. void main(void) {
  10.     touch_switch_t S[8];
  11.  
  12. /*    S[0].p_loc = 0x0;
  13.     S[1].p_loc = 0x1;
  14.     S[2].p_loc = 0x4;
  15.     S[3].p_loc = 0x5;
  16.     S[4].p_loc = 0x2;
  17.     S[5].p_loc = 0x3;
  18.     S[6].p_loc = 0x8;
  19.     S[7].p_loc = 0x9;*/
  20. /*    touch_switches_set(&S);*/
  21.  
  22.     int a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0;
  23. }
  24.  
  25. void touch_switches_set(touch_switch_t *switches[]) {
  26.     (*switches)[0].p_loc = 0x0;
  27.     (*switches)[1].p_loc = 0x1;
  28.     (*switches)[2].p_loc = 0x4;
  29.     (*switches)[3].p_loc = 0x5;
  30.     (*switches)[4].p_loc = 0x2;
  31.     (*switches)[5].p_loc = 0x3;
  32.     (*switches)[6].p_loc = 0x8;
  33.     (*switches)[7].p_loc = 0x9;
  34. }
  35.  
Jun 24 '08 #1
8 1847
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. "
Jun 24 '08 #2
Laharl
849 Expert 512MB
main() also returns an int, whether you're working in C or C++. void main() is nonstandard and poor practice.
Jun 24 '08 #3
JosAH
11,448 Expert 8TB
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
Jun 24 '08 #4
main() also returns an int, whether you're working in C or C++. void main() is nonstandard and poor practice.
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)
Jun 25 '08 #5
Laharl
849 Expert 512MB
I stand corrected, then, at least for microcontrollers.
Jun 25 '08 #6
weaknessforcats
9,208 Expert Mod 8TB
I stand corrected, then, at least for microcontrollers.
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.
Jun 25 '08 #7
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.
Jun 25 '08 #8
Banfa
9,065 Expert Mod 8TB
... 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

Expand|Select|Wrap|Line Numbers
  1. long short a;
  2. short long b;
  3. unsigned long short a;
  4. /* etc */
  5.  
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.
Jun 25 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: hilmilho | last post by:
Hi you, I'm facing a strange problem, and I don't know what to do. Maybe someone could help me understand this? I'm storing IP and Netmask on the struct below. 1st I store IP an print it, its...
10
by: googler | last post by:
Hello, I have a very simple question that I'm a bit confused about right now. Is it OK to allocate memory by malloc() or calloc() for a struct member and then call free() on it? For example, I have...
1
by: John | last post by:
Hi, What is the struct member aligment of a C# program? Are there any issues with P/Invoke when passing structs to 3rd party C dlls? Thanks
0
by: Urs Vogel | last post by:
Hi when setting the struct member alignment of a mixed code project to 'default', what is the actual alignment? I need to know since we're creating (struct-) data dynamically and pass it to a...
4
by: myfavdepo | last post by:
Hi friends, i am having some trouble in my prog. with struct member alignment. I have two different static libraries that i use, each with "struct member alignment" set to 8 bytes. In my...
5
by: arnaudk | last post by:
How does one initialize a const struct member of an object? Since it's const, this has to be done in the constructor initialization list. Unfortunately I can't seem to get the syntax right... ...
5
by: ssylee | last post by:
I'm not sure if I can initialize members of a struct the lazy way. For example, let's say I have a struct defined as below: typedef struct _ABC { BOOL A; BOOL B; BOOL C; } ABC;
5
by: petschy | last post by:
Hello All, I confronted recently with the fact that the size of a struct member can't be determined with sizeof(Struct::Member). I'm aware of the alternative methods to do this, however, I'm...
3
Markus
by: Markus | last post by:
Why can I only access a struct member through a pointer to said struct? I thought you were able to access a pointer's members via the dot notation? person mark; person *ptr_mark; ptr_mark->age...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.