![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
_olaf_
Joined: 24 Feb 2005 Posts: 32
|
Changing multiple Variables in struct in one command |
Posted: Tue Feb 03, 2015 5:28 am |
|
|
Hi,
I have a question about structures.
I have a structure with some variables
Code: |
typedef struct MenuStructure
{
char LcdString[20];
char MenuIndex[3];
unsigned char up;
unsigned char down;
unsigned char enter;
}MenuStructure;
|
If I want to change one of the variables I can make ist as followed
Code: |
MenuStructure.up =1;
|
But how can I Change all the Variables with one command? e.g.
Code: |
MenuStructure = {"test", 3, 0, 0, 0}; |
does not work.
Does anyone have an idea how to do this?
And yes, I'm not a professional software developer
CompilerVersion PCW 5.038 |
|
![](templates/subSilver/images/spacer.gif) |
kWoody_uk
Joined: 29 Jan 2015 Posts: 47 Location: United Kingdom
|
|
Posted: Tue Feb 03, 2015 6:26 am |
|
|
Hi,
You were close, but you need to actually create the structure variable first:-
Code: |
typedef struct MenuStructure {
char LcdString [20];
char MenuIndex [3];
unsigned char up;
unsigned char down;
unsigned char enter;
} MenuStructure; // Remember, because you used Typedef, this is NOT a variable name, but the typename
void main( void )
{
MenuStructure menu = { "test", 3, 0, 0, 0 };
menu.down = 3;
}
|
Best Regards,
Keith |
|
![](templates/subSilver/images/spacer.gif) |
_olaf_
Joined: 24 Feb 2005 Posts: 32
|
|
Posted: Tue Feb 03, 2015 6:56 am |
|
|
Thanks for your answer
If I use the following code in the main
Code: |
void main( void )
{
MenuStructure menu = { "test", 3, 0, 1, 0 };
menu.down = 3;
} |
does that mean that the variables contain
LcdString= test
MenuIndex= 3
up= 0
down= 1
enter= 0
after the code
Code: | MenuStructure menu = { "test", 3, 0, 1, 0 }; |
and in the next step
LcdString= test
MenuIndex= 3
up= 0
down= 3
enter= 0
after the code
? |
|
![](templates/subSilver/images/spacer.gif) |
kWoody_uk
Joined: 29 Jan 2015 Posts: 47 Location: United Kingdom
|
|
Posted: Tue Feb 03, 2015 7:42 am |
|
|
Yes, you are correct but also note that LcdString will also contain the null char after the t in test to terminate the string.
I only added the
as another example of changing the elements of the structure (but you already knew this). You are correct though.
Best regards,
Keith |
|
![](templates/subSilver/images/spacer.gif) |
_olaf_
Joined: 24 Feb 2005 Posts: 32
|
|
Posted: Tue Feb 03, 2015 1:54 pm |
|
|
So, I tried it today, but it doesn't work
the processor is included in the main.h and linked in MPLAB X
I changed my Code to the only things I need Code: | typedef struct MenuStructure {
char LcdString [20];
char MenuIndex [3];
unsigned char up;
unsigned char down;
unsigned char enter;
}MenuStructure; // Remember, because you used Typedef, this is NOT a variable name, but the typename
void main(void)
{
MenuStructure menu = { "test", 2, 0, 0, 0 };
MenuStructure.up =1;
} |
When I compile this test program I get the following error messages
Quote: | D:\Programmierung\PIC_C\Struct_Test\Struct_Test.X\main.c:16:39: Error#102 Expect comma <- This is an Error for the first Line in main
D:\Programmierung\PIC_C\Struct_Test\Struct_Test.X\main.c:16:43: Error#43 Expecting a declaration <- 1. line
D:\Programmierung\PIC_C\Struct_Test\Struct_Test.X\main.c:16:44: Error#43 Expecting a declaration <- 1. line
D:\Programmierung\PIC_C\Struct_Test\Struct_Test.X\main.c:17:14: Error#28 Expecting an identifier <- 1.line
D:\Programmierung\PIC_C\Struct_Test\Struct_Test.X\main.c:17:18: Error#43 Expecting a declaration <- 1.line
D:\Programmierung\PIC_C\Struct_Test\Struct_Test.X\main.c:17:19: Error#43 Expecting a declaration <- 2. line
D:\Programmierung\PIC_C\Struct_Test\Struct_Test.X\main.c:17:20: Error#43 Expecting a declaration <- 2. line
D:\Programmierung\PIC_C\Struct_Test\Struct_Test.X\main.c:19:1: Error#43 Expecting a declaration
8 Errors, 0 Warnings. |
Any Idea? |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Feb 03, 2015 2:13 pm |
|
|
Quote: | MenuStructure.up =1; |
You're trying to initialize a typedef. That's not what you intend.
You want to initialize an element of the actual instance of the structure.
Quote: | MenuStructure menu = { "test", 2, 0, 0, 0 }; |
I don't know if you're aware of it, but the line above does not initialize
elements 'down' or 'enter'. Look at the .LST file. Change the init values
from 0,0,0 to 3,4,5 so you can see it more clearly. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19380
|
|
Posted: Tue Feb 03, 2015 2:14 pm |
|
|
Your types need to match what you are giving.
The single digit '2', does not match an array.
Code: |
MenuStructure menu = { "test", {2,0,0}, 0, 0, 0 };
menu.up = 3;
|
You can initialise a char array with a text string, or with numbers to fill the array (as I show). It needs to see a declaration for the first char array, then one for the second, then three values.
Then on your second line you try to write the value to the typedef, not the variable....
Use a 'indicator name' for the typedef, like:
Code: |
typedef struct MenuStructure {
char LcdString [20];
char MenuIndex [3];
unsigned char up;
unsigned char down;
unsigned char enter;
}MenuStructure_type; // Remember, because you used Typedef, this is NOT a variable name, but the typename
|
You make a point of reminding yourself that this is the typename, then forget it..... |
|
![](templates/subSilver/images/spacer.gif) |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|