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