추가 인자를 받는 ...에 대해서 질문드립니다.

mydream의 이미지

printf의 마지막 매개변수에 ...이 언급되는데, 이건 추가로 언급되는 값들을 받는 파라미터라고 알고 있습니다. sprintf_s라는 것이 제대로 작동이 안되서 제가 직접 버퍼 오버플로우를 방지하는 함수를 만들고자 합니다. 그런데 한 가지 문제는 ...의 타입이 일정하지 않다는 것입니다.
...을 얻어오려면 stdarg.h 에 정의되 있는 va_list, va_start, va_arg, va_end를 사용해야 하죠.

int sprintf_s( char *buffer, size_t sizeOfBuffer, const char* format, ...)

이것이 이 함수의 원형이고요, 버퍼 오버플로우를 막기 위해 만들 함수는

int s_sprintf_s( char* buffer, size_t elelength,size_t sizeOfBuffer, const char* format, ...) {
  va_list vl;
  va_start(vl, format);
 
  while(...) {//의사코드.  서식문자 수만큼 루프를 돕니다.
    var_arg(vl, type);//여기서 type을 어떻게 지정해야 하냐는 것이죠.
  }
 
  va_end(vl);
}

가령 format이 "%d %f" 라고 할 때 %d에서는 va_arg(vl, int) %f는 va_arg(vl, float) 이라고 하면 될까요?

if(%d가 int이면) {
va_arg(vl, int);
}
else if(%f가 float이면) {
va_arg(vl, float);
}

이렇게 하는 것이 가능한가요? 나머지 서식문자 %u, %c 등은 어떻게 해야 하는지 궁금합니다.

mydream의 이미지

그냥 쓰겠습니다. 감사합니다. ㅜㅜ""

shint의 이미지

http://codepad.org/nsvPHHGp

http://codepad.org/2YL0Qoud

//--------------------------
#include <stdio.h>
 
 
void fn(int a)
{
    printf("1\n");
}
 
void fn(float a)
{
    printf("2\n");
}
 
 
int main()
{
    fn(1);
    return 0;
}
 
 
 
출력결과 - C++
1
 
 
출력결과 - C
Line 10: error: conflicting types for 'fn'
Line 5: error: previous definition of 'fn' was here
 
 
//--------------------------
 
#include <stdio.h>
 
 
void fn(int a)
{
    printf("1\n");
}
 
void fn(double a)
{
    printf("1.1\n");
}
 
 
int main()
{
    fn(1.1);
    return 0;
}
 
 
출력결과 - C++
1.1
 
 
 
//--------------------------
 
#include <stdio.h>
 
 
void fn(int a)
{
    printf("1\n");
}
 
void fn(float a)
{
    printf("2\n");
}
 
 
int main()
{
    fn(1.1);
    return 0;
}
 
출력결과 - C++
In function 'int main()':
Line 17: error: call of overloaded 'fn(double)' is ambiguous
compilation terminated due to -Wfatal-errors.

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.