c언어 구조체포인터 관련해서 질문드립니다!

leeyun1533의 이미지

각각 File_io.c,Main.c 의 소스파일인데
File_io에서 포인터를 이용해 구조체에 저장했으나 Main에서 출력하려 할경우 쓰레기 값이 들어가게 됩니다.
구조체에 저장이 안되는 것 같은데 며칠을 고민해 봐도 왜인지 모르겠습니다... 매개변수로도 넘겨보고 다른 방법도 사용해봤는데 말입니다.
연결리스트를 사용할때도 같은 문제가 발생하는데 헤더파일에 정의한 구조체가 이런 것인가요???
도움 부탁드립니다!

아래는 소스 입니다.

File_io.c
<#include
#include
#include
#include
#include
#include
#pragma warning (disable:4996)
#define _GNU_SOURCE
#define _CRT_SECURE_NO_WARNINGS

#include "D:\\이정윤\\visual studio\\SeoulSubway\\include\\SeoulSubway.h"
#include "D:\\이정윤\\visual studio\\SeoulSubway\\include\\mydef.h"
#include "File_io.h"

void File_io(Subway *head, Subway *tail, Subway *new, long weigh_data[][MAX], int *s, int *e) {

Subway *Subway_Struct = (Subway*)malloc(sizeof(Subway*));

int fp;
int res = 0;
int n;
int buf[20000];
char* Newline_p;
char *Spacing_p=(char *)malloc(sizeof(char *));
char* Newline_buf[50] = { NULL };
char* Spacing_buf[10] = { NULL };
char *weigh = NULL;
int count;
int i = 0; int j = 0; int k = 0;
char *num1,*num2;
char *str_tol;
char *str_tol2;
char *str_tol3;
char *ptr;
char *ptr2;
char *ptr3;
long value;
long value2;
long value3;
char *context = NULL;

head = NULL;
tail = NULL;

fp = open("D:\\이정윤\\visual studio\\SeoulSubway\\bin\\subway.txt", O_RDONLY);
if (fp == -1) {
printf("파일이 존재하지않습니다 새로 생성합니다\n");
fp = open("D:\\이정윤\\visual studio\\SeoulSubway\\bin\\subway.txt", O_CREAT | O_RDWR, S_IWRITE | S_IREAD);
}
n = read(fp, buf,300);

Newline_p = strtok(buf, "\n");
while (Newline_p != NULL) {
Newline_buf[i] = Newline_p;

Newline_p = strtok(NULL, "\n");
i++;
}

while (Newline_buf[k] != NULL) {

new = (Subway*)malloc(sizeof(Subway));

count = 0;

Spacing_p = strtok(Newline_buf[k], "\t");

num1 = Spacing_p;
Spacing_p = strtok(NULL, "\t");
str_tol = num1;
value = strtol(str_tol, &ptr, 10);
num2 = Spacing_p;
Spacing_p = strtok(NULL, "\t");
str_tol2 = num2;
value2 = strtol(str_tol2, &ptr2, 10);
weigh = Spacing_p;
Spacing_p = strtok(NULL, "\t");
str_tol3 = weigh;
value3= strtol(str_tol3, &ptr3, 10);
Subway_Struct->station[value] = Spacing_p;
Spacing_p = strtok(NULL, "\t");

count++;
j++;
k++;


Subway_Struct->weigh[j] = value3;
Subway_Struct->Main[j] = value;
Subway_Struct->sub[j] = value2;

printf("%d %d %d %s\n", Subway_Struct->Main[j], Subway_Struct->sub[j], Subway_Struct->weigh[j], Subway_Struct->station[j]);/>
}


}

Main.c
#include
#include
#include
#include
#include

#include "D:\\이정윤\\visual studio\\SeoulSubway\\include\\SeoulSubway.h"
#include "D:\\이정윤\\visual studio\\SeoulSubway\\include\\mydef.h"

long weight_data[MAX][MAX];

int main() {
int s = 0, e = 0;

for (s = 0; s for (e = 0; e if (s == e)
weight_data[s][e] = 0;
else
weight_data[s][e] = m;
}
}

for (s = 0; s < 10; s++) {
for (e = 0; e < 10; e++) {
printf("%d",weight_data[s][e]);
}printf("\n");
}

Subway *Subway_Struct = (Subway*)malloc(sizeof(Subway));
if (Subway_Struct == NULL) {
printf("메모리할당오류");
}


int option=0;

Subway *new = NULL;
Subway *head = NULL;
Subway *tail = NULL;
File_io(head, tail, weight_data,&s,&e);

for (s = 0; s < 10; s++) {
printf("%", Subway_Struct->weigh);
}
}

라스코니의 이미지

코드는 "<code> </code>" 로 싸주세요.

 의 이미지

Subway *Subway_Struct = (Subway*)malloc(sizeof(Subway*));

Subway가 어떻게 생긴 자료형인지는 모르겠지만, 이런 코드가 말이 되는 경우는 매우 드뭅니다. 그보다는 실수일 가능성이 더 높죠. 초보 C 프로그래머들이 대체로 이런 실수를 자주 합니다.

char *Spacing_p=(char *)malloc(sizeof(char *));

위 코드만큼은 아니어도 이 코드도 꽤 의심스러운데, 사실 애초에 변수 Spacing_p의 의미 자체가 의심스러운 상황이라 뭐라 드릴 말씀이 없네요.

아마 이 문제들을 제외하고도 다른 문제가 더 있을텐데 지금 상황에서는 코드를 읽어드리고 싶은 생각이 별로 안 드네요. 라스코니님 말씀대로 code 블럭으로 감싸주셔야 좀 더 읽기 편합니다.

댓글 달기

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