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 support@ccsinfo.com

[SOLVED]CCS C- Structures with Pointers

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



Joined: 16 Aug 2019
Posts: 52

View user's profile Send private message

[SOLVED]CCS C- Structures with Pointers
PostPosted: Mon Sep 02, 2019 4:37 am     Reply with quote

Hello all,
Again me Smile This time when I load this code to my dsPIC, I can't see my car's model and colour. I exactly get this output from RS232:

First car is :
The color of it :
Max. Speed is : 320
The motor type: 4.19

I want to use pointers. I can see the integers but not strings.

Here is my code:
Code:

#include <./33EP64MC206.h>

#use delay(internal=42MHz)     
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOJTAG                   //JTAG disabled
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES FRC                     
#FUSES NOOSCIO


#use rs232(baud=9600,parity=N,stop=1,xmit=PIN_G7 ,rcv=PIN_G6)
 
struct vehicle
{
    char model[10];
    char color[10];
    int max_speed;
    float motor;
   
};
void show(struct vehicle *car)
{
    printf("First car is : %s \r\n",(*car).model[8]);
    printf("The color of it : %s \r\n",(*car).color[5]);
    printf("Max. Speed is : %d\r\n",(*car).max_speed);
    printf("The motor type: %f \r\n",(*car).motor);
   
}
void main()
{
   setup_oscillator(OSC_INTERNAL,42000000)
   
   struct vehicle first;
   struct vehicle *pointer;
   pointer=&first;
   (*pointer).model[8]="mercedes";
   (*pointer).color[5]="black";
   (*pointer).max_speed=320;
   (*pointer).motor=4.20;
   
   output_toggle(pin_b5);
   delay_ms(100);
   output_toggle(pin_b5);
   delay_ms(100);
   output_toggle(pin_b5);
   delay_ms(100);
   output_toggle(pin_b5);
   delay_ms(100);

   
   while(1)
    {
       
      show(pointer);
      output_toggle(pin_b5);
      delay_ms(3000);
       
    }
}


Last edited by camleot23 on Mon Sep 02, 2019 5:19 am; edited 2 times in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Sep 02, 2019 5:06 am     Reply with quote

First. to use a structure pointer, look at the -> operator. Much easier to
use in your function.
Then though the big problem is the initialisation. You can't copy a string
with =. This is generic in C. You have to use strcpy, to copy the actual
'string' into the target array. This is why the strings are blank.
camleot23



Joined: 16 Aug 2019
Posts: 52

View user's profile Send private message

PostPosted: Mon Sep 02, 2019 5:18 am     Reply with quote

Thank you very much for your time Ttelmah. It's working:)
Code:

#include <./33EP64MC206.h>

#use delay(internal=42MHz)     
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOJTAG                   //JTAG disabled
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES FRC                     
#FUSES NOOSCIO


#use rs232(baud=9600,parity=N,stop=1,xmit=PIN_G7 ,rcv=PIN_G6)
 
struct vehicle
{
    char model[10];
    char color[10];
    int max_speed;
    float motor;
   
};
void show(struct vehicle *car)
{
  printf("First car is : %s \r\n",car->model);
    printf("The color of it : %s \r\n",car->color);
    printf("Max. Speed is : %d\r\n",car->max_speed);
    printf("The motor type: %f \r\n",car->motor);
   
}
void main()
{
   setup_oscillator(OSC_INTERNAL,42000000)
   
   struct vehicle first;
   struct vehicle *pointer;
  pointer=&first;
   strcpy(pointer->model,"mercedes");
   strcpy(pointer->color,"black");
   pointer->max_speed=320;
   pointer->motor=4.20;
   
   output_toggle(pin_b5);
   delay_ms(100);
   output_toggle(pin_b5);
   delay_ms(100);
   output_toggle(pin_b5);
   delay_ms(100);
   output_toggle(pin_b5);
   delay_ms(100);

   
   while(1)
    {
       
      show(pointer);
      output_toggle(pin_b5);
      delay_ms(3000);
       
    }
}
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