strcpy 질문

qw3709의 이미지

for(i=0;i<8;i++){
		if(min[i]==2){
			smin_list[a]=sec[i];
			strcpy(smin_team[a],team[i]);
			printf("[test] %s \n",smin_team[a]);	
			a+=1;
			printf("[test2] %s \n",smin_team[0]);
		}
	}

team배열에는 red나blule가 들어있음(1개씩)
strcpy로 smin_team으로 옮기는과정에서
2번쨰 프린트문에서 smin_team[0]에 redblue 가 나옴...
red가 나오거나 blue가 나와야하는데 왜 붙어서나오는지 아무리봐도모르겠습니다... 도와주세요..

shint의 이미지

- strcpy() 와 printf() 는. 문자열의 끝이 0x00 (헥사 값 Hex 1바이트) 이어야 문자열로 인식함
각 함수에 인자값. 리턴값. 오류값을 함수 문서에서 확인해 봐야 합니다.

http://www.cplusplus.com/reference/cstring/strcpy/
https://linux.die.net/man/3/strcpy

- strncpy() 나 memcpy() 를 사용하면. 정해진 바이트 수 만큼만 복사가 가능합니다.

- 정확한 내용은 책 예제를 참고해보세요.

http://codepad.org/PbBKqAXe

#include <stdio.h>
 
int main()
{
    //변수 선언
    int i;
    int min[8];
    int sec[8];
    int smin_list[8];
    int a = 0;
 
    //배열 선언 및 초기화
    char smin_team[8][2] = {"a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7"};
    char team[8][2] = {"b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7"};
    char team2[2][8] = {"b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7"};
    char team3[8][3] = {"b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7"};
 
    //team3[8][3] 8개의 3 바이트 문자를 선언하여 사용
 
    //team3[8][0] = 0; 로 접근해서 사용. 1개
    //team3[8][1] = 0; 로 접근해서 사용. 2개
    //team3[8][2] = 0; 로 접근해서 사용. 3개
    //team3[8][2] = 0x00; 로 문자열의 마지막을 알려줌. NULL을 사용해도 됨.
 
    //team3[8][3] = 0; 로 접근해서 사용. 4개. 메모리 공간 3개를 넘는 접근으로. 오류 발생.
 
    //문자열의 마지막에 0x00 이나 NULL 을 사용하지 않는다면.
    //1바이트 씩 출력해도 됨.
    //printf("%c", team3[0][0]);
    //printf("%c", team3[0][1]);
 
    //배열 데이터 초기화
    for(i=0;i<8;i++)
    {
        min[i] = i;
        sec[i] = i;
        smin_list[i] = i;
        team3[i][3] = 0x00;      //0x00 (헥사 값 HEX 대신). NULL 을 사용해도 될겁니다.
    }
 
    //출력 확인
    printf("%s\n", team[0]);        //b0b1b2b3b4b5b6b7
    printf("%s\n", &team[0][0]);    //b0b1b2b3b4b5b6b7
    printf("%s\n", &team[1][0]);    //  b1b2b3b4b5b6b7
    printf("%s\n", &team[0][1]);    // 0b1b2b3b4b5b6b7
    printf("\n");
    printf("%s\n", team2[0]);        //b0
    printf("%s\n", &team2[0][0]);    //b0
    printf("%s\n", &team2[1][0]);    //b1
    printf("%s\n", &team2[0][1]);    //0
    printf("\n");
 
    for(i=0;i<8;i++)
    {
        if(min[i] == 2)
        {
                //min[i] : 2    i : 2   a : 0
                printf("min[i] : %d    i : %d   a : %d\n", min[i], i, a);
 
		smin_list[a] = sec[i];
		strncpy(&smin_team[a][0], &team[i][0], 2);
		printf("[test2] %s \n", &smin_team[0][0]);        //[test2] b2a1a2a3a4a5a6a7
		a+=1;
		printf("[test1] %s \n", &smin_team[a][0]);        //[test1] a1a2a3a4a5a6a7 
 
		strncpy(&smin_team[a][0], &team3[i][0], 2);
		printf("[test2] %s \n", &smin_team[0][0]);        //[test2] b2
		a+=1;
		printf("[test1] %s \n", &smin_team[a][0]);        //[test1] a2a3a4a5a6a7 
	}
    }
 
    return 0;
}
 
 
b0b1b2b3b4b5b6b7a0a1a2a3a4a5a6a7
b0b1b2b3b4b5b6b7a0a1a2a3a4a5a6a7
b1b2b3b4b5b6b7a0a1a2a3a4a5a6a7
0b1b2b3b4b5b6b7a0a1a2a3a4a5a6a7
 
b0
b0
b1
0
 
min[i] : 2    i : 2   a : 0
[test2] b2a1a2a3a4a5a6a7 
[test1] a1a2a3a4a5a6a7 
[test2] b2 
[test1] a2a3a4a5a6a7 

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

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

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

qw3709의 이미지

와.. 답변 정말감사합니다.. 덕분에 방금 해결했습니다 NULL 이 문제엿군ㅇ요

qw3709의 이미지

if(smin_team[i] == "blue")

이구문이 제대로 작동이안됩니다.. 이구문좀 도와주실수있으실까요

세벌의 이미지

컴파일 할 때 Error 메시지 또는 Warning 메시지 나올 겁니다. 그 메시지를 읽어보세요.

qw3709의 이미지

아 strncmp 함수로 해결했습니다. 감사합니다

댓글 달기

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