배열과 포인터 때문에 질문 드립니다!.

n4u9h7의 이미지

암보드 프로그래밍하다 궁금사항이있어서 이렇게 질문글 올립니다.

static uint8_t TxBuffer [BUFFER_SIZE];

TxBuffer가 요렇게 정의되어 있구요 (BUFFER_SIZE 는 2048)

요 TxBuffer 안에 0부터 FF까지 꽉 채워넣는 함수가

void Fill_Buffer(u8 *pBuffer, u16 BufferLenght, u32 Offset)
{
u16 IndexTmp = 0;

/* Put in global buffer same values */
for (IndexTmp = 0; IndexTmp < BufferLenght; IndexTmp++ )
{
pBuffer[IndexTmp] = IndexTmp + Offset;
}
}

요렇게 있습니다.

이 TxBuffer에 어떠한 문자열을 넣기위해 생각하고 있는데.

원체 배열과 포인터 부분엔 약한지라... 예를 들어 TxBuffer에 "hello world!"라는 문자열을 집어넣기 위해선
(물론 2048byte 데이터중 hello world! 외에는 전부 쓰레기값이 들어가겠지요)

어떻게 해야할지 궁금하네요. 저 위에 Fill_Buffer 함수를 변경만 하면 될 것 같은데

정확히 어떻게 해야할지 잘 몰라서 이렇게 질문글 올립니다.

beta의 이미지

ㅋㅋ 질문의 요지를 잘 못 봤네요. 수정합니다.

static uint8_t TxBuffer [BUFFER_SIZE];
 
void Fill_Buffer(u8 *pBuffer, u16 BufferLenght, u32 Offset)
{
  u16 IndexTmp = 0;
 
/* Put in global buffer same values */
   for (IndexTmp = 0; IndexTmp < BufferLenght; IndexTmp++ )
   {
        TxBuffer[IndexTmp+Offset] = pBuffer[IndexTmp] ;//이렇게 데이터 넣고 싶다는 이야기죠?
   }
}

아래와 같이 사용하면 됩니다..

offset = 10 ;
 
Fill_Buffer(TxBuffer, strlen("hello world!"), offset ) ;
 
or 
 
if(offset < BUFFER_SIZE)
    strncpy(TxBuffer+offset, "hello world!", BUFFER_SIZE-offset) ; 
else
    invalid offset value!

발 담갔다. 이제 익숙해 지는길만이..

n4u9h7의 이미지

궁금했던게 바로 풀리네요 ㅎ 감사합니다!!!

혹시 다른분들 다른 방법 알고계신분도 댓글 남겨주시면 더 도움 될 듯 합니다!

부탁드려요~ 감사합니다!

bushi의 이미지

sprintf 계열도 있습니다.

strncpy 는 주의깊게 써야 되는 함수입니다.
man 에서 일부 발췌합니다.

       char *strncpy(char *dest, const char *src, size_t n);
 
       If the length of src is less than n, strncpy() pads  the  remainder  of
       dest with null bytes.
 
       Some programmers consider strncpy() to be inefficient and error  prone.
       If  the  programmer knows (i.e., includes code to test!)  that the size
       of dest is greater than the length of src, then strcpy() can be used.
 
       If there is no terminating null byte in the first n characters of  src,
       strncpy()  produces  an unterminated string in dest.  Programmers often
       prevent this mistake by forcing termination.
익명 사용자의 이미지

함수를 변경할게 아니라..
있는 표준함수를 쓰시면됩니다.

strcpy(TxBuffer, "hello world!" );

왜 궂이 어렵게...

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.