我在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的源代碼。
#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
如果沒有[顯示_relevant_ code](http://stackoverflow.com/help/mcve)以及更清楚地描述發生什麼事情或發生什麼事情,那麼除了猜測之外,沒有什麼可以做的。儘管如此,如果你想創建一個索引數組,「sprintf」對於這項工作來說是非常錯誤的工具。 – Notlikethat 2014-10-28 11:25:29
代碼編寫器**代表什麼處理器** **有很多種處理器都有IDE的名稱,**包括'char'是16位類型的DSP(可能是也可能不是具體相關,但顯示多少已被留下未說明)。 – 2014-10-28 19:10:25
@Notlikethat你能提出一些建議嗎? – techenthu 2014-10-28 19:19:48