segmentation fault오류가 뜹니다 함수에 문자 배열이 전달되지않는것같은데 어떻게하면좋을까요?

standard00의 이미지

입력받은 문자로 이진트리를 구성해서 전위순회를 확인하는 프로그램인데

#include <stdio.h>
#include <string.h>
 
void preorder(char tree[], int idx)
{
if (tree[idx] != ' ')
{
printf("%c", tree[idx]);
preorder(tree, 2*idx);
preorder(tree, 2*idx+1);
}
else
{
printf("%c", tree[idx]);
preorder(tree, 2*idx);
preorder(tree, 2*idx+1);
}
 
 
}
int main(void)
{
        char tree[100];
        int i;
        int b=0;
        printf("input data\n");
        fgets(tree,100,stdin);
        b = strlen(tree);
        for( i=0;i<=b;i++)
        {
        printf("%c",tree[i]);
        }
        preorder(tree, 1);
}
shint의 이미지

- segmentation fault는 찾지 못했습니다.
- 함수에 문자배열 넘기기

//stack overflow : char tree[10000000];
//First-chance exception in test.exe: 0xC0000005: Access Violation.

//
http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=1040101&docId=73706607&qb=c2VnbWVudGF0aW9uIGZhdWx0&enc=utf8&section=kin&rank=1&search_sort=0&spq=0&pid=R6StlU5Y7vlsscWOLysssssssud-413588&sid=UKVBgXJvLDgAAHtdFeQ

//2차원 배열함수 사용
http://cafe.naver.com/cafec/287908
http://kin.naver.com/qna/detail.nhn?d1id=1&dirId=10405&docId=160700467&qb=c2VnbWVudGF0aW9uIGZhdWx0&enc=utf8&section=kin&rank=2&search_sort=0&spq=0&pid=R6StlU5Y7vlsscWOLysssssssud-413588&sid=UKVBgXJvLDgAAHtdFeQ

// test.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
 
 
#include <memory.h>
#include <stdlib.h>
 
 
//int main(int argc, char* argv[])
//{
//	return 0;
//}
 
 
//이차원 배열
//http://cafe.naver.com/cafec/287908
 
//void myfunc(int year, char (*ptr)[10]);
//
//...
//myfunc(year,born);
//
//
//void myfunc(int year, char (*ptr)[10])
//{
//...
//}
 
 
 
 
//
#include <stdio.h>
#include <string.h>
 
void preorder(char tree[], int idx)
{
	//First-chance exception in test.exe: 0xC0000005: Access Violation.
	if(idx >= 10000000)
	{
		return ;
	}
 
//	printf("--%c %d\n", tree[idx], idx);
	if (tree[idx] != ' ')
	{
//		printf("pre-%c\n", tree[idx]);
		preorder(tree, 2*idx);
		preorder(tree, 2*idx+1);
	}
	else
	{
//		printf("aft-%c\n", tree[idx]);
		preorder(tree, 2*idx);
		preorder(tree, 2*idx+1);
	}
}
 
 
 
 
void fn(char* ptr [])
{
	//(ptr[0][0]) = 'q';
	printf("%s\n", ptr[0]);
	printf("%s\n", ptr[1]);
}
 
int main(void)
{
		//stack overflow : char tree[10000000];
        char tree[1000000];
 
        int i;
        int b=0;
 
		memset(tree, '\0', sizeof(tree));
 
 
//        printf("input data\n");
//        fgets(tree,100,stdin);
//        b = strlen(tree);
//        for( i=0;i<=b;i++)
//        {
//	        printf("%c",tree[i]);
//        }
//		printf("start--------\n");
//        preorder(tree, 1);
//		printf("end----------\n");
 
 
		char* ptr[10];
		ptr[0] = (char*) malloc (10);
		ptr[1] = (char*) malloc (10);
		strcpy(ptr[0], "abc");
		strcpy(ptr[1], "def");
 
		fn(ptr);
		fn(ptr);
 
		free(ptr[0]);
		free(ptr[1]);
 
}

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

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

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

shint의 이미지

중복글

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

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