c언어 구조체포인터 관련해서 질문드립니다!
각각 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> </code>" 로 싸주세요.
Subway *Subway_Struct =
Subway
가 어떻게 생긴 자료형인지는 모르겠지만, 이런 코드가 말이 되는 경우는 매우 드뭅니다. 그보다는 실수일 가능성이 더 높죠. 초보 C 프로그래머들이 대체로 이런 실수를 자주 합니다.위 코드만큼은 아니어도 이 코드도 꽤 의심스러운데, 사실 애초에 변수
Spacing_p
의 의미 자체가 의심스러운 상황이라 뭐라 드릴 말씀이 없네요.아마 이 문제들을 제외하고도 다른 문제가 더 있을텐데 지금 상황에서는 코드를 읽어드리고 싶은 생각이 별로 안 드네요. 라스코니님 말씀대로 code 블럭으로 감싸주셔야 좀 더 읽기 편합니다.
댓글 달기