This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Arrays

As a fairly new student in C I am having diffictly with arrays.

I have a string of numbers that I am tring to split up into smaller strings to convert them to integers.

I have made the code as basic as possible but still get the errors.

#include <stdio.h>
#include <stdlib.h>
#include <reg52.h>

xdata char Temperatures1[9];
xdata char Temperatures2[12];
xdata char Temperatures3[9];

void main(void)
{

  char a[36] = "<<123456789012345678901234567890>>";
  int s;

    SCON = 0x52;
    TMOD = 0x20;
    TCON = 0x69;
    TH1  = 0xF5;

    for(s=0; s < 9; s++)
    {
     Temperatures1[s] = a[s+2];
    }

    for(s=0; s < 12; s++)
    {
     Temperatures2[s] = a[s+11];
    }

    for(s=0; s < 9; s++)
    {
     Temperatures3[s] = a[s+23];
    }

    printf("Temperatures1: %s\n", Temperatures1);
    printf("Temperatures2: %s\n", Temperatures2);
    printf("Temperatures3: %s\n", Temperatures3);
}
I'd expect that Temperatures1,2,3 would all be 1234567890. But Temperature1 is the entire string and Temperatures 2,3 are both incorrect as well

When I watch what is happening in the watch window of the simulator I can see the error ocurring but don't know how to fix it.

Any ideas

Cheers

Steve

Parents
  • You may want to consider using the library routines to do actually copy the strings.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <reg52.h>
    
    xdata char Temperatures1[10];
    xdata char Temperatures2[13];
    xdata char Temperatures3[10];
    
    void main(void)
    {
    
      char a[36] = "<<123456789012345678901234567890>>";
    
        SCON = 0x52;
        TMOD = 0x20;
        TCON = 0x69;
        TH1  = 0xF5;
    
        strncpy (Temperature1, &a[2], 9);
        Temperatures1[9] = 0;
    
        strncpy (Temperature2, &a[11], 9);
        Temperatures2[9] = 0;
    
        strncpy (Temperature3, &a[23], 9);
        Temperatures3[9] = 0;
    
        printf("Temperatures1: %s\n", Temperatures1);
        printf("Temperatures2: %s\n", Temperatures2);
        printf("Temperatures3: %s\n", Temperatures3);
    }

    Jon

Reply
  • You may want to consider using the library routines to do actually copy the strings.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <reg52.h>
    
    xdata char Temperatures1[10];
    xdata char Temperatures2[13];
    xdata char Temperatures3[10];
    
    void main(void)
    {
    
      char a[36] = "<<123456789012345678901234567890>>";
    
        SCON = 0x52;
        TMOD = 0x20;
        TCON = 0x69;
        TH1  = 0xF5;
    
        strncpy (Temperature1, &a[2], 9);
        Temperatures1[9] = 0;
    
        strncpy (Temperature2, &a[11], 9);
        Temperatures2[9] = 0;
    
        strncpy (Temperature3, &a[23], 9);
        Temperatures3[9] = 0;
    
        printf("Temperatures1: %s\n", Temperatures1);
        printf("Temperatures2: %s\n", Temperatures2);
        printf("Temperatures3: %s\n", Temperatures3);
    }

    Jon

Children
  • "consider using the library routines"

    The library routines are written by the compiler vendor, and usually provide the most efficient implementation - certainly better than writing your own 'for' loop!

    Also, with C51, certain Library functions can take advantage of the extra DPTR(s)