CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Pointers and struct problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
noyz



Joined: 31 Jan 2009
Posts: 59

View user's profile Send private message

Pointers and struct problem
PostPosted: Fri May 27, 2011 7:44 pm     Reply with quote

Hi i have a issue :
Code:

typedef struct {
      int x1;
      int x2;
      int y1;
      int y2;
      int text_X;
      int text_Y;
      char* label;
} button;
button auto_button, freeze_button, memory_button;

void init_buttons () {
   freeze_button.x1 = 195;
   freeze_button.x2 = 239;
   freeze_button.y1 = 0;
   freeze_button.y2 = 23;
freeze_button.text_X = 34;
   freeze_button.text_Y = 2;
   strcpy ( freeze_button.label, "Freeze" );
   //
   auto_button.x1 = 195;
   auto_button.x2 = 239;
   auto_button.y1 = 69;
   auto_button.y2 = 92;
   auto_button.text_X = 34;
   auto_button.text_Y = 11;
      strcpy ( auto_button.label, "Auto" );

      memory_button.x1 = 195;
      memory_button.x2 = 239;
      memory_button.y1 = 23;
      memory_button.y2 = 46;
      memory_button.text_X = 34;
      memory_button.text_Y = 5;
      strcpy ( memory_button.label, "Memory" );




It seems that there somewhere it reuses the memory, i tried a various combination like defining button *auto_button, and then use -> for indication of pointers from pointer. But i don't get to work the string variable and the other integers value too.
The thing is that the memory is overwritten by the second button instructions.
Can someone help me a little bit.
You can test code in ccsc, print it to console - Proteus, just make a program to write all variables to console, and see what happens.
A little strange thing - is that i am displaying this to a LCD- i saw that freeze is written freeXe and X is keep changing from one character to another, not necessary readable, it is from memory overwritten i think, and the boxes are not where they were supposed to be, so i make a example and saw that the variables are reused and thats the thing.
For one button all works great , but when the second came...
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 27, 2011 8:13 pm     Reply with quote

Quote:
typedef struct {
int x1;
int x2;
int y1;
int y2;
int text_X;
int text_Y;
char* label;
} button;

button auto_button, freeze_button, memory_button;

strcpy ( auto_button.label, "Auto" );
strcpy ( freeze_button.label, "Freeze" );
strcpy ( memory_button.label, "Memory" );

Your structure only has an uninitialized pointer in it. It's probably pointing
to RAM address 0x0000. So when you use strcpy() on it, you're over-
writing whatever is at that RAM address. You didn't give your PIC, so
it could be other variables or it could be SFR registers, depending on the
PIC family (18F or 16F).

What you should do, is declare a char array in the structure instead of
the pointer. Make it be big enough to hold any string that you might put
into it. Make sure you include room for the final 0x00 byte, which is part
of every string.
noyz



Joined: 31 Jan 2009
Posts: 59

View user's profile Send private message

PostPosted: Fri May 27, 2011 11:18 pm     Reply with quote

hi there,
thanx for reply

It is about PIC 18F8527 so 18F family.

I quite don't understand what are you saying, can you write down the whole example ?
I understood the part with making my string an array instead of pointer
isn't possible to make it pointer ?
I did notice that when i use button *auto_button and strcpi auto_button->char it works, for the characters but the memory is then overridden for int x1,x2.. and so on. So there is a switch of memory override between char* and int when making button pointer or normal.
It would be very helpful if you can explain me this thing with registers and ram.
By the way can I send a "asd" constant to a pointer ?

Code:
void doString(char * c);
doString ("asd");

This doesn't work, but if i put *"asd" it compiles, but doesn't take the right string. Is it possible to make something like this to work ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Sat May 28, 2011 2:08 am     Reply with quote

Think what you have been told.

xxxx.label, is a variable designed to hold the _address_ of a string, not the string itself.

It just contains an address. The address it contains, you never set. So (say), it could be zero, or FFFF, or something random in memory.
You then perform 'strcpy', with this address as the target. The string you copy is going to go 'somewhere', but it could be anywhere.....

So you need to do three things:
1) Create an _array_ to contain the string. Either somewhere else in memory, or in the structure itself.
2) Point the label at this array (if you create the array in the structure this is already done).
3) Copy the string to the array.

Code:

//So putting the array inside the structure.
typedef struct {
int x1;
int x2;
int y1;
int y2;
int text_X;
int text_Y;
char label[10]; //here creating the array in the structure - size will have to
//suit your _maximum_ label _plus one_.
} button;

strcpy ( auto_button.label, "Auto" );

////////////////////////////////////////////////////////////

//Alternative putting the labels elsewhere
typedef struct {
int x1;
int x2;
int y1;
int y2;
int text_X;
int text_Y;
char* label;
} button;

char button_label[10];

strcpy ( button_label, "Auto" );

//Now the string is stored in 'button_label'.
auto_button.label = button_label;
//Make the _address_ stored in 'auto_button.label', point to the string.


Now, in the first case you are also taking advantage of a C shortcut. If you have an array, the array 'name' on it's own, _is_ the address of the array. So strcpy, puts the data into the array as required. In the second case, you have now stored the label into the array called 'button_label', and set the variable 'auto_button.label', to point _to _ this data.

Best Wishes
noyz



Joined: 31 Jan 2009
Posts: 59

View user's profile Send private message

PostPosted: Sat May 28, 2011 3:41 am     Reply with quote

I've already modified my program since pcm programmer reply to me, i was just asking so i can understand better Smile

Is there any way you can specify which memory to use a pointer? This was the thing that i was so curious about :D
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Sat May 28, 2011 8:16 am     Reply with quote

That is what the second example in what I post does.
It creates a separate array, then 'points' the pointer to this.
Leaving a pointer in the structure, rather than the actual array.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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