Thread: passing char array index as pointer

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    204

    passing char array index as pointer

    Hi I am trying to write a light weight printf style function.

    I have got this far:

    Code:
    void println(const char *txData){
    	LOG(__PRETTY_FUNCTION__);
    	UARTPuts (LPC_UART0, txData);
    }
    void miniPrint(const char *format, ...)
    {
    	unsigned int index = 0;
    	va_list argptr;
    
    	va_start(argptr, format);
    
    	while(format[index] != '\0')
    	{
    		if(format[index] == '%')
    		{
    			index++;
    
    			if(format[index] == '\0')
    				break;
    
    			switch(format[index])
    			{
    				case 'd':
    					send_int(va_arg(argptr, uint32_t));
    					break;
    				case 'c':
    					transmit(va_arg(argptr, uint32_t));
    					break;
    				case 's':
    					sendString(va_arg(argptr, char *));
    					break;
    				default:
    					println(&format[index]);
    			}
    		}
    		else {
    			println(&format[index]);
    		}
    
    		index++;
    	}
    
        va_end(argptr);
    }
    (its for an embedded platform)

    If I do:
    Code:
    miniPrint("hello\n");
    in main, I get:

    hello
    ello
    llo
    lo
    o
    I understand why I think. When I am passing the reference to the array possion it is outputting everything up to the next /0. So my question is how do I stop it?

    I dont have much choice as to how the output wants it:
    Code:
    void UARTPuts(LPC_UART_TypeDef *UARTx, const void *str)
    Thats library code, so I dont want to change it. I.e I have to pass an address into println.

    Thanks
    Alex

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Apparently, the UARTPuts() function keeps outputting data, starting at the address it receives, and keeps going until it finds a '\0' character.

    So, assuming you supply a format string "Hello", the first time through YOUR loop, the address of the 'H' is passed to UARTSPuts(), which then outputs "Hello" (every character starting at the 'H' until the '\0' after the 'o').

    The second time through the loop, the address of the 'e' is passed, so in effect UARTSPuts() receives the string "ello".

    If you want to stop that behaviour, you either need to use something other than UARTSPuts() - after all, it does what it does - or use another array. Put whatever you need into that array, followed by a '\0' character, and send that array to UARTPuts().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why can't a char array use and int index?
    By anna87 in forum C Programming
    Replies: 4
    Last Post: 03-02-2013, 09:05 PM
  2. Array index vs. direct pointer
    By dtkokovoko in forum C Programming
    Replies: 15
    Last Post: 01-08-2013, 06:57 PM
  3. Replies: 8
    Last Post: 04-04-2012, 09:03 PM
  4. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  5. Trouble passing char/pointer array
    By -Prime- in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2007, 03:38 AM