c언어에서 문자열을 그대로 함수로 넘길수있나요?

square4fish의 이미지

int get_entry(ListType *list, int pos)
{
	 error("위치 오류");
}

char error(char *message)
{
	fprintf(stderr, "%s\n", message);
	exit(1);
}

이런식으로 만들어서 error함수에서 문자열을 그대로 받게하는게 문법상 가능한가요??

bushi의 이미지

fprintf() 에 "%s\n" 라는 문자열 넘겨주는 것에 대해선 어떻게 생각하세요 ?

shint의 이미지

http://codepad.org/EhY8yIi4

함수에 인자값을 넘길때는. 대충 이런식으로 사용합니다.

정확한 내용은 C 책과 예제를 보시기 바랍니다.
네이버와 구글에서 검색해보셔도 되구요.

#include <stdio.h>
#include <error.h>
#include <stdlib.h>
 
 
typedef struct DF_LIST
{
    int a;
}ListType;
 
char fn_error(char *message); //함수 선언
 
 
int fn_CallByReference(char *  r)
{
    printf("[IN] fn_CallByReference : %s\n", r);
    *(r+0) = 'a';
    *(r+1) = 'b';
    *(r+2) = 'c';
    *(r+3) = 'd';
}
 
 
int fn_CallByValue(char  r [10])
{
    printf("[IN] fn_CallByValue : %s\n", r);
    r[0] = '1';
    r[1] = '2';
    r[2] = '3';
    r[3] = '4';
}
 
 
int fn_CallByValueRef(char  r [10])
{
    printf("[IN] fn_CallByValueRef : %s\n", r);
    *(r+0) = '1';
    *(r+1) = '2';
    *(r+2) = '3';
    *(r+3) = '4';
}
 
 
int get_entry(ListType *list, int pos)
{
    error(0,0,"오류 발생");
    fn_error("오류 발생");
}
 
//error()
//http://man7.org/linux/man-pages/man3/error.3.html
 
//exit()
//http://man7.org/linux/man-pages/man3/exit.3.html
 
char fn_error(char *message)
{
    fprintf(stderr, "%s\n", message);
    exit(1);
    return 0;
}
 
int main()
{
    //
    char ca[10] = {"test"};
    fn_CallByReference(&ca[0]);
    printf("[OUT] main() - fn_CallByReference : %s\n", ca);
 
    //
    fn_CallByValue(&ca[0]);
    printf("[OUT] main() - fn_CallByValue : %s\n", ca);
 
    fn_CallByValueRef(&ca[0]);
    printf("[OUT] main() - fn_CallByValueRef : %s\n", ca);
 
    //
    char * p = (char*) malloc (10);
    memset(p, 0x00, 10);
    *(p+0) = 't';
    *(p+1) = 'e';
    *(p+2) = 's';
    *(p+3) = 't';
 
    fn_CallByReference(p);
    printf("[OUT] main() - fn_CallByReference : %s\n", ca);
 
    //
    memset(p, 0x00, 10);
    *(p+0) = 't';
    *(p+1) = 'e';
    *(p+2) = 's';
    *(p+3) = 't';
 
    fn_CallByValue(p);
    printf("[OUT] main() - fn_CallByValue : %s\n", p);
 
    fn_CallByValueRef(p);
    printf("[OUT] main() - fn_CallByValueRef : %s\n", p);
 
 
    free(p);
 
    //
    ListType *list;
    get_entry(list, 0);
    fn_error("위치 오류");
 
    return 0;
}

출력 결과
[IN] fn_CallByReference : test
[OUT] main() - fn_CallByReference : abcd
[IN] fn_CallByValue : abcd
[OUT] main() - fn_CallByValue : 1234
[IN] fn_CallByValueRef : 1234
[OUT] main() - fn_CallByValueRef : 1234
[IN] fn_CallByReference : test
[OUT] main() - fn_CallByReference : 1234
[IN] fn_CallByValue : test
[OUT] main() - fn_CallByValue : 1234
[IN] fn_CallByValueRef : 1234
[OUT] main() - fn_CallByValueRef : 1234
/t: 오류 발생
오류 발생

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

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