0
`#include<reg51.h>
#include<string.h>
#include"_LCD_R8C.c"
unsigned char c[11];
void serial_int (void) interrupt 4
{
static unsigned char chr[11];
int i,j;
if (RI==1)      
{
    RI = 0;     
        TI = 0;     
    chr[11] = SBUF;       
        for(j=0;j<1;j++)
         {
       for(i=0;i<=10;i++)
        {
      c[i]=chr;
    }
       c[11]='\0';
     }
   }
}
int main()
{
unsigned char a[2][11]={"$0016221826","$0123456789"};
int i,j;
    lcd_init();
    lcd_clear();
    SCON = 0x50;              
    TMOD = 0x20;                
    TH1  = 0xFD;                 
    ET0  = 0;                     
    TR1  = 1;                       
    RI   = 1;                   
    ES   = 1;                   
    EA   = 1;                   
    for(i=0;i<=1;i++)
{
  **j=strcmp(a,c);**
    if(j==0)
      {
      lcd_printxy(1,1,"yes");
      }
    else
      {
      lcd_printxy(1,6,"no");
      }
}
}`

the compiler is giving warning in line 55(BOLD): pointer to different objects. how about array to string conversion? is it correct? the received string should compare with the available array of strings..

5
  • The string "$0016221826" is not 11 characters, it's actually 12 characters (you forgot about the terminating '\0' character). Commented Aug 31, 2012 at 7:06
  • @Joachim Pileborg ok thank you, what about converting characters to string and comparing, if i take the characters as 12? Commented Aug 31, 2012 at 7:54
  • You don't need to co any conversions, an array degrades to a pointer, so you can use a char array everywhere a char pointer is expected. what makes an array a "string" is the type (char) and that it ends with zero. Commented Aug 31, 2012 at 8:02
  • I am not getting what you are telling, will you post the code plz? it will be helpful for me to understand Commented Aug 31, 2012 at 8:24
  • will you explain it clearly with an example Commented Aug 31, 2012 at 8:55

2 Answers 2

2

Creating an array that contains a string, like

char foo[] = "bar";

it's actually the same as the following

char foo[] = { 'b', 'a', 'r', '\0' };

or the following

const char *foo = "bar";

This variable, foo can be used either as an array (i.e. the second letter is foo[1]) or as a pointer that can be passed to a function (like strlen(foo) will return 3).

So from the point of your program, a character array or a character pointer are equivalent, with the exception that you can't assign to a character array but you can assign to the pointer. So the following is okay:

char foo[] = "bar";
char *pfoo = foo;

But this is not okay:

const char *pfoo = "bar";
char foo[] = pfoo;    /* Error! */

There is also another thing that is different between character arrays and character pointers: Using the sizeof operator on a character array returns the number of characters in the array (including the terminating zero), but on a character pointer it returns the size of the pointer.

Example:

char foo[] = "foo";
const char *bar = "bar";

printf("sizeof(foo) = %lu\n", sizeof(foo));
printf("strlen(foo) = %lu\n", strlen(foo));
printf("sizeof(bar) = %lu\n", sizeof(bar));
printf("strlen(bar) = %lu\n", strlen(bar));

If you run the above code on a 64-bit machine (where pointers are eight bytes), it will print:

sizeof(foo) = 4
strlen(foo) = 3
sizeof(bar) = 8
strlen(bar) = 3
Sign up to request clarification or add additional context in comments.

Comments

0

a is array of char pointers while c is character pointer. You would have to change your like to j=strcmp(a[i],c);

EDIT:

In your serial_int function, chr is array of chars so c[i]=chr is incorrect. I don't what your logic is, but syntactically it should be c[i]=chr[i].

Also, you are not using j of parent loop, which indicates that you are doing same thing in each iteration of inner for loop.

2 Comments

k, in the loop i i am coping all the characters into the array c[i], in the j loop i am adding '\0' at the end and j loop is iterated for one time to add one null at the end of array to make it string.
if possible please post the exact code to conver array to string. otherwise modify my code and post it plz

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.