2014-10-28 37 views
0

我在uint8_t str[]數組中有一個字符串,我試圖在名爲uint8_t pos[]的另一個變量中存儲str內的字符位置。該代碼是寫在Code Composer Studio的VS 5.4將int值存儲在代碼作曲家studio中的uint8_t數組中vs 5.4

我試着用sprintf(),類型轉換以及改變uint8_t pos[]int pos[]類型以及unsigned int pos[]。這些都不起作用。

代碼在sprintf語句中斷,並通過到達未定義的內存位置而停止。當我在達到sprintf聲明後在彙編中運行時,它給出了一個錯誤消息,說明在位置中找不到sprint.c的源代碼。

enter image description here

#include <stdint.h> 
#include <stdbool.h> 
#include <string.h> 
#include "tm4c123gh6pm.h" 
#include <stdio.h> 

void initHw() 
{ 
. 
. 
} 

int main(void) 
{ 
    // Initialize hardware 
    initHw(); 
    char strRx[80]; 

    int count =0; 
    int count_enter=0; 
    uint8_t posStr[80]; 
    uint8_t typeStr[80]; 
    int pos=0; 
    int len; 
    unsigned int j=0, argCount=0; 

    while(1) 
    { 
     if(count == 0) 
     { 
      putsUart0("Enter characters for the string\r\n"); 
     } 
     if(count <= 80) 
     { 
      char c = getcUart0(); 
      if(c=='\b') 
       if(count>0) 
        count--; 
       else 
        break; 

      if(c>=' ') 
      { 
       strRx[count]=c; 
       count++; 
      } 
      if(count==80 || c==13)//'\r') 
      { 
        count_enter++; 
        if(count_enter==1) //count==80 before carriage return 
        { 
         len = count; 
         strRx[count]='\0'; 
         while(count!=80) 
          strRx[count++]='\0'; 

         count_enter=0; 
         putsUart0("\r\nEntered string is:\r\n"); 
         putsUart0(strRx); 
         putsUart0("\r\n"); 
        } 

        j=0; 
        //char a[10]; 
        for(pos=0; pos!=len; pos++)// strRx[pos]!='\0'; pos++) 
        { 
         char a[80]; 
         if((strRx[pos]>='A' && strRx[pos]<='Z') || (strRx[pos]>='a' && strRx[pos]<='z')) 
         { 
          typeStr[j]='a'; 
          //posStr[j]=pos; 
          a[j]=pos; 
          sprintf(a,"%u",pos); //source not found 
          //a[j]=pos; 
          //posStr[j]=sprintf("%c",a); 
          //posStr[j]=(uint8_t)a;//a; 
          while(strRx[pos]!='\0'&&((strRx[pos]>='A' && strRx[pos]<='Z') || (strRx[pos]>='a' && strRx[pos]<='z'))) 
          { 
           pos++; 
          } 
          pos--; 
          j++; 
         } 

         else if(strRx[pos]>='0' && strRx[pos]<='9') 
         { 
          typeStr[j]='n'; 
          a[j]=pos; 
          sprintf(a,"%u",pos); 
          //posStr[j]=pos;//a; 
          while(strRx[pos]!='\0'&&((strRx[pos]>='0' && strRx[pos]<='9'))) 
          { 
           pos++; 
          } 
          pos--; 
          j++; 
         } 

         else 
         { 
          while(strRx[pos]!='\0'&&((strRx[pos]<'A' && strRx[pos]>'Z') && (strRx[pos]<'a' && strRx[pos]>'z') && (strRx[pos+1]<'0' && strRx[pos+1]>'9'))) 
           pos++; 
         } 
        } 
        argCount=j; 
        while(j!=80) 
        { 
         typeStr[j++]='\0'; 
         posStr[j++]='\0'; 

        } 
       count = 0; 
      }//if(count==80 || c==13)//'\r') 
     }//if count<=80 
    }//while(1) 
}//main 
+1

如果沒有[顯示_relevant_ code](http://stackoverflow.com/help/mcve)以及更清楚地描述發生什麼事情或發生什麼事情,那麼除了猜測之外,沒有什麼可以做的。儘管如此,如果你想創建一個索引數組,「sprintf」對於這項工作來說是非常錯誤的工具。 – Notlikethat 2014-10-28 11:25:29

+0

代碼編寫器**代表什麼處理器** **有很多種處理器都有IDE的名稱,**包括'char'是16位類型的DSP(可能是也可能不是具體相關,但顯示多少已被留下未說明)。 – 2014-10-28 19:10:25

+0

@Notlikethat你能提出一些建議嗎? – techenthu 2014-10-28 19:19:48

回答

0

「無法找到sprintf.c」可能錯誤只是意味着調試器無法找到源文件,這意味着它不能顯示在調試器窗口中的源代碼。您可能沒有sprintf.c的源代碼,並且您只有預編譯的庫。或者,也許你有源代碼,調試器只是看錯了路徑。如果你有源代碼,那麼可能有辦法告訴調試器找到它的路徑。

但這只是一個調試器顯示問題 - 它不是什麼導致您的程序崩潰。這個問題可能出現在你的代碼中,但你必須分享你的代碼給我們來識別。或者這個問題可能是printf相關例程中可能出現的其他問題。

1)printf例程可以使用相對較大的堆棧空間。因此,檢查堆棧溢出並在必要時增加堆棧大小。

2)一些嵌入式庫提供了printf例程的多個實現,例如「small」,「no-float」和「full」。有限的實現使用較少的代碼空間,但不支持所有的格式說明符。所以確保你已經鏈接的sprintf的實現支持你實際使用的所有格式說明符。查看鏈接器或庫下的項目設置,以選擇使用哪個版本的printf。

+0

我在帖子中包含了代碼。你能解釋我如何檢查堆棧溢出嗎?此外,我還選擇了庫函數假設中的printf支持。謝謝。 – techenthu 2014-10-28 19:26:51

+0

我看過代碼,我不明白你打算做什麼。我很懷疑sprintf是否是你想使用的。如果您確定sprintf是您想要的,請參閱[使用printf的提示](http://processors.wiki.ti.com/index.php/Tips_for_using_printf)。 – kkrambo 2014-10-28 20:50:43

+0

檢查堆棧溢出的一種方法是預先使用模式(例如0xDEADBEEF)填充堆棧。 (例如,您可以在調試器中通過內存窗口執行此操作,然後運行代碼。然後暫停並確保圖案在整個堆棧中不被覆蓋。圖案保留的地方是堆疊的高水位標記。如果模式已被完全覆蓋,那麼你溢出了堆棧。 – kkrambo 2014-10-28 20:53:39