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

Array as Argument

I want to send control codes to a printer
they look like this :

	unsigned char cntrl_codes[6] = { 0x0d, 0x14, 0x1b, 0x4c, 0x0d, 0x0d};

I have made a function to send this array to serial port :
//this function takes two arguments
//    1)the array
//    2)time delay between successive codes

void print(unsigned char* c,unsigned char del)
{				
	int x,y;		
	y=strlen(c);//y++;      
	for(x=0;x<y;x++)
	{

		putchar(c[x]);
		delay(del);
		
	}
}

There has to be a more graceful way to do this !!!

Parents
  • Does the strlen call give the result you want? There is no trailing zero in the initializer of the array. You have to pass the size of the array as an extra argument to the function.
    Besides that, I can't see why this function is ungraceful. You can declare it like this:

    void print(unsigned char c[],unsigned char del)
    
    which may look better but doesn't make any difference.

    Regards,
    - Mike

Reply
  • Does the strlen call give the result you want? There is no trailing zero in the initializer of the array. You have to pass the size of the array as an extra argument to the function.
    Besides that, I can't see why this function is ungraceful. You can declare it like this:

    void print(unsigned char c[],unsigned char del)
    
    which may look better but doesn't make any difference.

    Regards,
    - Mike

Children
  • 1) strlen does not give correct result

    2) trailing zero ... to make it NULL terminated ...why?

    3) i want a generalized func that can take arrays of different lengths....

    4) so should I use 'strlen' in my main func to find size of array and then
    pass it as argument ? how would that change it ?


    Thanks
    Mandar Limaye

  • It is not clear what the problem is.

    You could define your control codes as a null terminated string if you prefer:

    unsigned char cntrl_codes = "\0x0d\0x14\0x1b\0x4c\0x0d\0x0d";
    
    Is the problem that you do not want to hold up the CPU while waiting for the delay time? To do this it is necessary to buffer the output and to have a special version of putchar() that detects a non-ASCII command character and interprets it as a wait command. This may get more complicated that you really want...

  • strlen uses the trailing zero to find the length of the string. It could be implemented like this:

    int strlen(const char* str)
    {
        int len = 0;
        while ( *str++ ) len++;
        return len;
    }
    
    The program code does not contain any runtime information about the size of the array. That's how C works. So if you want the function to work with arrays of different sizes, you need to either pass the size explicitly or use tricks like strlen, which requires trailing zero.
    Compile-time information about the size of the array is available in the form of sizeof(array), where array is the name of the array, it cannot be an argument to a function. So you don't have to use strlen, which takes time to scan the string. You can just pass sizeof(array) as an argument, that's how it's ususally done.
    - Mike

  • Try this:

    void TxSerial(unsigned char *ptr)
    {   
        while(*ptr!=0x0)
        {
           SBUF=*ptr;     //Send one character
           while(!TI)     //Wait for completion
           {}
           TI=0;          //Clear Tx flag
           ptr++;
         }
    }
    

  • while(*ptr!=0x0)
    this, of course, requires that the string is null-terminated (as any good 'C' string should be - see K&R)

    this technique is fundamental to all of the 'C' string-handling functions - they just keep going thro' the string til they hit a NUL!

  • Thanks All

    I am using a null-term array and it works fine.