Skip to main content

Calculating the length of an array in mikroC

Note: This is the beginning of a new category on my blog, which it the 'Tips and Tricks' category. This is where I share small tips on all the other subjects I discuss here.

On my post about RS232 communication using microcontrollers (here), I explained how to send and receive one character (one Byte) at a time. Now I wanted to send a whole string using RS232. Since a string is essentially an array of characters in C, we just have to loop through the array and send one char at a time as usual, which is done in the following code.
 char *mystr;  
 unsigned int i;  
   
 void main(){  
   Usart_Init(9600);  
   mystr = "Testing";  
   
   for(i=0; i<5; i++){  
     Usart_Write(mystr[i]);  
     Delay_ms(500);  
   }  
 }  

This works well when you know the length of the string and only one string needs to be sent. When there are several strings, writing a loop for each of them isn't a good programming method. The better way is to write a separate method to handle string transmission.

The problem is, once in a separate method, you don't know the length of the string to iterate through it. We have to find the length of each string passed to the method. Since strings are actually char arrays, this means finding the length of the array. If we were using a language like Java, we might have been able to call something like myArray.length to get the length. But unfortunately, C doesn't have such a method. But it does have an operator named sizeof. What this operator does is, it returns an integer constant that gives in Bytes of how much memory space is used by its operand.



When applied to a type identifier, the sizeof returns the size of that type.
e.g. :
sizeof(char)    // returns 1
sizeof(int)    // returns 2
sizeof(unsigned long)    // returns 4
sizeof(float)    // returns 4

When we apply it to an array, it returns the total number of Bytes in the array.
e.g. :
int a[10];
sizeof(a);     // returns 20, which is, 10 * sizeof(int)

Now, all we need to do to get the length of an array is to divide the sizeof the array by the sizeof its type.
e.g. :
int a[10];
int length = sizeof(a)/sizeof(int);     // returns 10

So, here's a sample code that sends two strings using RS232. It has a separate method, sendString() which accepts any char array, checks its length and iterates through it properly.

 char mystring[] = "Testing 1, 2, 3";  
 char mystring2[] = "Testing 4, 5, 6";  
 unsigned int i;  
 int lengthofstr;  
 void sendString(char sendstr[]);  
   
 void main(){  
   Usart_Init(9600);  
   
   while(1){  
     sendString(mystring);  
     Delay_ms(1000);  
     sendString(mystring2);  
   }  
 }  
   
 void sendString(char sendstr[]){  
   lengthofstr = sizeof(sendstr)/sizeof(char);  
   
   for(i=0; i<lengthofstr; i++){  
     Usart_Write(sendstr[i]);  
     Delay_ms(500);  
   }  
 }  

So, I hope this explained few things on mikroC that might help you code easily. Feel free to express your ideas about this in the comments :)

Comments

  1. The new category is great Timi. It gave me some ideas as well. By the way, nice work on the code box. :)

    ReplyDelete

  2. Hi,

    I think it's easier to use the standard C function "strlen"

    Regards,

    ;)

    ReplyDelete
  3. sendString() never work correct!!!

    lengthofstr = sizeof(sendstr)/sizeof(char);

    there mistake!!! sizeof???? Author! Correct your mistake!

    ReplyDelete
  4. lengthofstr = sizeof(sendstr)/sizeof(char);

    1. why you divide by .....sizeof(char)????? it indy code! need simple lengthofstr = sizeof(sendstr);

    and

    2. Are you sure, sizeof(senderstr) return length of string???? NO!!!! You need replace sizeof to strlen.

    And now working code!!!

    Before post, check and run really code!

    ReplyDelete

Post a Comment

Popular posts from this blog

Selecting the Correct Gauge Wires for your RC Models

When we are building RC models (aircrafts, multicopters etc.), something we typically overlook is what type of wires to use to distribute power in them. Usually, we try to reduce the weight of the model, so we tend to go with smaller wires. But, we cant expect to put in small circuit-wires and have them handle the amount of power needed. If you look at the wires that comes in the output leads of a Li-Po, or the wires that comes on a standard XT Jack, you'll notice that they're quite huge. Large gauge wires on a Li-Po and XT connectors These need to be huge to handle the amount of amperage that goes through them. So, we should also consider the amperage, and the length of wire needed when selecting the size (gauge) of the wires.

Make Your Own Jupmer Wires for Electronic Breadboard

Note: After starting the 'Tips and Tricks' section I wanted to do my own 'tutorial'. So, here's my first attempt of it. Hope you all find this useful, and sorry about the low quality of the photographs, my camera is just a 2MP. If you, like me, like to experiment with electronics, then you'd probably use an electronic breadboard (also known as protoboard or project-board). These allow you to construct and test circuits without the need to permanently soldering the components. The problem with these is that you have to use wires to connect the components across the bus lines. We normally use single core circuit wires for this purpose, but they tend to bend, break and get stuck in the breadboard when used repeatedly. This may cause short circuiting the bus lines and could be the failure of your circuit. A better option would be to buy a 'Breadboard Jumper Wire' kit, such as this. A Breadboard Jumper Wire Kit These have sets of wire of differ

What do the prop size numbers mean?

Have you been trying to shop for propellers - either online or at a hobby shop - and got confused of the numbering used to denote the size of the propellers? You see things like " 10 x 4.5 CW ", or " 8055 CCW ". What do these numbers mean? A 8 x 4.5 CW and CCW Propeller Set It's actually quite simple once you figure it out. The first number denotes the diameter of the prop (length from tip-to-tip). The second number denotes the pitch of the prop. Here, the pitch is denoted as a length, not a angle. Let's see how that works.