Win32api질문..

yww24의 이미지

이상하게 빌드다하고 실행하면 프로그램이 멈추네요..
잘못된 주소값을 참조해서 그런경우가 많다고 들었습니다만
어디가 잘못된건지 도저히모르겠네요;

다음은 코드내용..

#include <stdio.h>
#include <stdlib.h>
 
 
typedef struct list{
	int data;
	struct list * next;
}list;
 
void Insert(list * head, int data);
 
void Print(list* head);
 
int main(void){
	list * head = (list*)malloc(sizeof(list));
	(*head).next = NULL;
 
	Insert(head, 7);
	Print(head);
 
	return 0;
}
 
void Insert(list * head, int data){
 
//작업에 쓰일 변수들 생성//
 
	list * cursor;
	list * newlist;
	cursor = NULL;
	newlist = (list*)malloc(sizeof(list));
	(*newlist).data = data;
 
//리스트의 마지막부분을 찾아내는 작업//
 
	if ((*head).next = NULL){
		(*head).next = newlist;
		return;
	}
	else {
		for (cursor = head; (*cursor).next != NULL, cursor = (*cursor).next;){
			if ((*cursor).next == NULL)break;
		}
 
//새로운 노드를 추가함//
		(*cursor).next = newlist;
		return;
	}
}
 
void Print(list* head)           //리스트의 데이터들 출력//
{
	list * cursor = NULL;
	if (head == NULL)
	{
		printf("출력할 내용이 없습니다.\n");
	}
	else
	{
 
		printf("연결리스트 데이터 출력\n");
		for (cursor = (*head).next; (*cursor).next != NULL; cursor = (*cursor).next)
		{
		 printf("- %d",(*cursor).data);
		}
	}
	return 0;
}
raymundo의 이미지

디버거 쓰는 게 익숙하지 않으시면 printf 라도 프로그램 주요 분기점에 삽입해서 제대로 진행하고 있는지 눈으로 확인해보셨다면 최소한 폴트가 어디서 나는지는 아실 수 있었을텐데요.

void Insert(list * head, int data){
	if ((*head).next = NULL){     
// == 를 써야 할 자리에 = 를 쓰는 바람에 이 if 에 걸리지 못하고 else 로 들어갑니다. 
		(*head).next = newlist;
		return;
	}
	else {
		for (cursor = head; (*cursor).next != NULL, cursor = (*cursor).next;){
// 이 for 의 조건부가 컴마로 구분된 두개의 식이라서, 왼쪽 구문 (*cursor).next != NULL 은 전혀 의미없는 비교식이 되었고,
// (비교를 하긴 하지만 그 결과는 그냥 버려짐)
// cursor = (*cursor).next 를 수행하니 cursor 는 NULL이 되고
// for 는 이 수행의 결과( 대입 연산의 결과니까 우변값인 (*cursor).next )만 가지고
// 루프를 진입할지 말지 결정할 텐데, 이 값이 NULL이니까 통과
			if ((*cursor).next == NULL)break;
		}
 
 
		(*cursor).next = newlist;
// cursor 가 NULL인 상태니까 여기서 꽥
		return;
	}
}

저 for 루프는 다음처럼 고치는 게 낫겠네요.

for (cursor = head; cursor->next != NULL; cursor = cursor->next)
     ;
 
// 아니면
for (cursor = head; cursor->next != NULL; )
     cursor = cursor->next ;

마찬가지로 Print 함수의 for 루프도, 원하는 출력을 못해줄 것입니다. 조건부를 잘 보세요.

좋은 하루 되세요!

yww24의 이미지

친절한 답변 감사합니다! 제가좀더 자세히봐야됬네요..

댓글 달기

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