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
  • Steve,
    The problem is you are trying to print null terminated strings but are using character arrays without null termination. Try terminating them;

    #include <stdio.h>
    #include <stdlib.h>
    #include <reg52.h>
    
    xdata char Temperatures1[10];
    xdata char Temperatures2[13];
    xdata char Temperatures3[10];
    
    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];
        }
        Temperatures1[s] = 0;
    
        for(s=0; s < 12; s++)
        {
         Temperatures2[s] = a[s+11];
        }
        Temperatures2[s] = 0;
    
        for(s=0; s < 9; s++)
        {
         Temperatures3[s] = a[s+23];
        }
        Temperatures3[s] = 0;
    
        printf("Temperatures1: %s\n", Temperatures1);
        printf("Temperatures2: %s\n", Temperatures2);
        printf("Temperatures3: %s\n", Temperatures3);
    }
    

    The 'C' initializer will probably clear the arrays before calling main(), in which case the last character in each array would already be a null, but you should never rely on this.
    You could also use memset() to clear all the arrays before using them.
    Best luck

Reply
  • Steve,
    The problem is you are trying to print null terminated strings but are using character arrays without null termination. Try terminating them;

    #include <stdio.h>
    #include <stdlib.h>
    #include <reg52.h>
    
    xdata char Temperatures1[10];
    xdata char Temperatures2[13];
    xdata char Temperatures3[10];
    
    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];
        }
        Temperatures1[s] = 0;
    
        for(s=0; s < 12; s++)
        {
         Temperatures2[s] = a[s+11];
        }
        Temperatures2[s] = 0;
    
        for(s=0; s < 9; s++)
        {
         Temperatures3[s] = a[s+23];
        }
        Temperatures3[s] = 0;
    
        printf("Temperatures1: %s\n", Temperatures1);
        printf("Temperatures2: %s\n", Temperatures2);
        printf("Temperatures3: %s\n", Temperatures3);
    }
    

    The 'C' initializer will probably clear the arrays before calling main(), in which case the last character in each array would already be a null, but you should never rely on this.
    You could also use memset() to clear all the arrays before using them.
    Best luck

Children