How to transfer value from char variable to char array in c

1.1k views Asked by At

I have char data variable which gives (o/p comes from pc's hyper terminal and i am comparing its inside microcontroller) me known string with known length like "ASDF".

Now I want to store this variable's o/p to a char array char dataarray[5];

And I had other char array char newdata[8]="ASDF\r\n"; so that I can compare both string using if(strcmp(dataarray[i],name2) == 0)

something like

char data;
char dataarray[5];    
char newdata[8]="ASDF\r\n";
if(strcmp(dataarray[i],newdata[j]) == 0)

So what should I need to change?


void main()
{
   char *data;    
   char *old;    
   char *match;    
   char *nomatch;       
   nomatch = "not";    
   match = "yes";    
   old = "HJB";     
   int m;

   USARTInit(25);    //UBRR = 51

   //Loop forever

   while(1)
   {            
        data=USARTReadChar();

        if(strcmp(data,old) == 0)
        {
          for(m=0;m<3;m++)
          {
            USARTWriteChar(match[m]);
          }
        }
        else{USARTWriteChar(nomatch);
    }              
}
2

There are 2 answers

0
Sourav Ghosh On

You can make use of array indexing to achieve this. Please check the below pseudo-code

 char data = 0;
 char dataarray[5] = {0};       //initialize
 for (int i = 0; i < 4 ; i++)   //you can customize, but leave space for null
 {
   data = somefunc();    //somefunc() returns a char value
   //something else, if you want. maybe some sanity on data itself
   dataarray[i] = data;         
 }
 dataarray[i] = 0;               //null terminate the array

Then, you can use dataarray for any string related operation, say strcmp(), for example.

7
Amol Saindane On

You also need to change size of dataarraywhich is having size Five, means excluding null character you can store only Four values in it. newdata is already having Six values in it, so when you will do

if(strcmp(dataarray[i],newdata[j]) == 0)

then it will always fail. You need to change that size of dataarray as well so that sometime that condition will get true and it will work properly.

EDIT

As per edited question I made this code for understanding. Code Explanation is given in comments

#define MAX_LENGTH 100    
void main()
{
   char data[MAX_LENGTH] = {'\0'}; 
   char old[] = "HJB";    
   char match[] = "yes";    
   char nomatch[] = "not";       
   int m;
   int count = 0;
   USARTInit(25);    //UBRR = 51

   //Loop forever

   while(1)
   {            
       // This method will give you only one character at a time
       // As I can see you are comparing only three characters so 
       // You need to compare when three characters you got from UART

        data[count] = USARTReadChar(); 

        if(data[count] != NULL) // check whether valid data came
          count++;

        if(count > 3)    // this will tell that you got three valid caracters  
        {
            count = 0;  // You can again start comparing  
            if(strcmp(data,old) == 0)  // Here you can use strcmp()
            {
              for(m=0;m<3;m++)
              {
                USARTWriteChar(match[m]);
              }
            }
            else
            {
                for(m=0;m<3;m++)
                {
                    USARTWriteChar(nomatch[m]);
                }
            }       
          }  
       }
    }              
}