title: int main is looping for no apparent reason? (PIC programming, XC16, MPLAB) dsPIC33EP
body: <p>Working on a program to run on a dcPIC33 (coded in c obviously) using the XC16 compiler and MPLABX v2.10 IDE. I have implemented some functionality, one function of which is to send strings of text to a uart so i can see serial output using a serial terminal. My problem is found in this line.</p>
  
  <pre><code>UART_Write_Text("Starting ADXL345 test...fdsbjugbbjkdsf");
  </code></pre>
  
  <p>Which when you look at the below code should only run once however it is forever repeated in the terminal as if the int main fer looping, even though the main has a while loop at the bottom to keep it from ending i just can't see how it is managing to loop like this.</p>
  
  <pre><code>    /* 
   * File:   main2.c
   * Author: nedu
   *
   * Created on 14 July 2014, 15:57
   */
  #include &lt;xc.h&gt;
  #include &lt;stdint.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  #if __XC16_VERSION &lt; 1011
  #warning "Please upgrade to XC16 v1.11 or newer."
  #endif
  
  #pragma config ICS = PGD3           // ICD Communication Channel Select bits (Communicate on PGEC3 and PGED3)
  #pragma config JTAGEN = OFF         // JTAG Enable bit (JTAG is disabled)
  
  // FPOR
  #pragma config ALTI2C1 = OFF        // Alternate I2C1 pins (I2C1 mapped to SDA1/SCL1 pins)
  #pragma config ALTI2C2 = OFF        // Alternate I2C2 pins (I2C2 mapped to SDA2/SCL2 pins)
  #pragma config WDTWIN = WIN25       // Watchdog Window Select bits (WDT Window is 25% of WDT period)
  
  // FWDT
  #pragma config WDTPOST = PS32768    // Watchdog Timer Postscaler bits (1:32,768)
  #pragma config WDTPRE = PR128       // Watchdog Timer Prescaler bit (1:128)
  #pragma config PLLKEN = ON          // PLL Lock Enable bit (Clock switch to PLL source will wait until the PLL lock signal is valid.)
  #pragma config WINDIS = OFF         // Watchdog Timer Window Enable bit (Watchdog Timer in Non-Window mode)
  #pragma config FWDTEN = OFF         // Watchdog Timer Enable bit (Watchdog timer enabled/disabled by user software)
  
  // FOSC
  #pragma config POSCMD = NONE          // Primary Oscillator Mode Select bits (XT Crystal Oscillator Mode)
  #pragma config OSCIOFNC = OFF       // OSC2 Pin Function bit (OSC2 is clock output)
  #pragma config IOL1WAY = OFF        // Peripheral pin select configuration (Allow multiple reconfigurations)
  #pragma config FCKSM = CSECMD       // Clock Switching Mode bits (Clock switching is enabled,Fail-safe Clock Monitor is disabled)
  
  // FOSCSEL
  #pragma config FNOSC = FRC          // Oscillator Source Selection (Internal Fast RC (FRC))
  #pragma config IESO = OFF           // Two-speed Oscillator Start-up Enable bit (Start up with user-selected oscillator source)
  
  // FGS
  #pragma config GWRP = OFF           // General Segment Write-Protect bit (General Segment may be written)
  #pragma config GCP = OFF            // General Segment Code-Protect bit (General Segment Code protect is Disabled)
  
  #define FP 40000000
  #define BAUDRATE 9600
  #define BRGVAL ((FP/BAUDRATE)/16)-1
  #define DELAY_105uS asm volatile ("REPEAT, #4201"); Nop(); // 105uS delay
  
  unsigned int i;
  char *temp[40];
  
  #include "spi.h"
  #include "ADXL345.h"
  
  void delay_ms(long ms){
      unsigned long x = 0;
      for(x = 0; x &lt;= ((FP/24000))*ms; x++)
          Nop();
  }
  void __attribute__((__interrupt__)) _U1TXInterrupt(void)
  {
      IFS0bits.U1TXIF = 0; // Clear TX Interrupt flag
  }
  
  void UART_Write(char data)
  {
      while(!U1STAbits.TRMT);
      U1TXREG = data;
  }
  
  void UART_Write_Text(char *text)
  {
      int i;
      UART_Write('\n');
      UART_Write('\r');
      for(i=0;text[i]!='\0';i++)
          UART_Write(text[i]);
  }
  
  void InitClock(){
      PLLFBD=63; // M=65
      CLKDIVbits.PLLPOST=0; // N2=2
      CLKDIVbits.PLLPRE=1; // N1=3
      // Initiate Clock Switch to FRC oscillator with PLL (NOSC=0b001)
      __builtin_write_OSCCONH(0x01);
      __builtin_write_OSCCONL(OSCCON | 0x01);
      // Wait for Clock switch to occur
      while (OSCCONbits.COSC!= 0b001);
      // Wait for PLL to lock
      while (OSCCONbits.LOCK!= 1);
  }
  
  void InitUart(){
      U1MODEbits.STSEL = 0; // 1-Stop bit
      U1MODEbits.PDSEL = 0; // No Parity, 8-Data bits
      U1MODEbits.ABAUD = 0; // Auto-Baud disabled
      U1MODEbits.BRGH = 0; // Standard-Speed mode
      U1BRG = BRGVAL; // Baud Rate setting for 9600
      U1STAbits.UTXISEL0 = 0; // Interrupt after one TX character is transmitted
      U1STAbits.UTXISEL1 = 0;
      IEC0bits.U1TXIE = 1; // Enable UART TX interrupt
      RPOR4bits.RP43R = 1;    //RP43/RB11 as U1TX
      RPINR18bits.U1RXR = 42; //RP42/RB10 as U1RX
      U1MODEbits.UARTEN = 1; // Enable UART
      U1STAbits.UTXEN = 1; // Enable UART TX
      /* Wait at least 105 microseconds (1/9600) before sending first char */
      DELAY_105uS
  }
  
  int main(void)
  {
      InitClock();
      InitUart();
  
      spi_init();
      UART_Write_Text("Starting ADXL345 test...fdsbjugbbjkdsf");
      enable_adxl345();
      int id = adxl345_devID();
      sprintf(temp, "Device ID is: 0x%02x", id);
      UART_Write_Text(temp);
      UART_Write_Text("Done");
  
  
      /*
      //Go into standby mode to configure the device.
      setPowerControl(0x00);
  
      //Full resolution, +/-16g, 4mg/LSB.
      setDataFormatControl(0x0B);
  
      //3.2kHz data rate.
      setDataRate(ADXL345_3200HZ);
  
      //Measurement mode.
      setPowerControl(0x08);
      */
      int* readings[3] = {0,0,0};
      getOutput(readings);
      int inter = 7;
  
      while(1)
      {
          delay_ms(1000);
          /*UART_Write_Text("Starting ADXL345 test...");*/
          sprintf(temp, "%i, %i, %i", inter, (int) readings[1], (int) readings[2]);
          UART_Write_Text("WORK");
      }
      return 0;
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/aXqCG.png" alt="Terminal output received"></p>
  
  <p>I just can't figure it out, hopefully someone can point me in the right direction?</p>
  
  <p>UPDATE:
  Managed to fix my problem, the reason for the program crashing was the for loop in the function UART_Write_Text() (The one commented out below).</p>
  
  <pre><code>void UART_Write(char data)
  {
      while(!U1STAbits.TRMT);
      U1TXREG = data;
  }
  
  void UART_Write_Text(char *text)
  {
      int i;
      UART_Write('\n');
      UART_Write('\r');
  
      for(i=0;i&lt;25;i++)
          UART_Write(text[i]);
      /*
      for(i=0;text[i]!='\0';i++)
          UART_Write(text[i]);
      */
  }
  </code></pre>
  
  <p>The for loop condition text[I]!='\0', I used '\0' to stop looping at the end of the char[] however I was under the assumption that '\0' meant a char that = NULL when for the condition to actually be met, there actually had to = '\0'.</p>
  
  <p>Although no answer managed to catch this I would like to thank everyone who took their time to try and help me out on this and I have upvoted you all as all your answers helped in my understanding of C as a whole, since I have the opportunity to mark a question as answered im going to mark "gmch"'s answer as his explanation on the difference between an array of variables and an array of pointers to said variables is stellar in my opinion :).</p>