블로그 이미지
송시혁

calendar

1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Notice

Tag

Recent Post

Recent Comment

Recent Trackback

Archive


posted by 송시혁

#include <stdio.h>
#include <process.h>
#include <stdlib.h>

typedef struct node
{
  char data;
  struct node *next;
}NODE;
void print_list (NODE *head);  
int main()
{
  FILE *fp;
  NODE * temp = NULL, *list = NULL;
  char ch;
  
  if((fp= fopen("d10-5.txt""r")) == NULL)
  {
    printf("file open error!");
    exit(-1);
  }
  fscanf(fp, "%c"&ch);
  while(!feof (fp))
  {
    temp=(NODE *) malloc(sizeof(NODE));
    temp->data = ch;
    temp->next = list;
    list = temp;
    fscanf(fp, "%c"&ch);
  }
  fclose(fp);

  temp = list;
  print_list(list);
  
  temp= list;
  while(temp != NULL)
  {
    //printf("%c ->", temp->data);
    list = temp;
    temp = temp->next;
    free(list);
    
    
    
  }
  //printf(" NULL\n");

  return 0;
}


void print_list (NODE *head)
{
  if(head == NULL)
  {
    printf("NULL\n");
  }
  else
  {
    printf("%c ==> ", head->data);
    print_list (head->next);
  }
}    
  
    
    







  

posted by 송시혁

예제 10-4


#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
  char data;
  struct node *next;
}NODE;
  
int main()
{
  NODE *list, *temp;
  list = (NODE *) malloc (sizeof(NODE));
  list->data='a';
  temp = list;
  temp->next = (NODE *) malloc (sizeof(NODE));
  temp= temp->next;
  temp ->data ='b';
  temp -> next = (NODE *) malloc (sizeof(NODE));
  temp = temp->next;
  temp->data='c';
  temp->next = NULL;
  
  temp = list;
  while(list != 0)
  {
    printf("%5c\n", list->data);
    temp = temp->next;
    free(list);
    list = temp;
  }
  return 0;
}
일단 list가 동적 할당을 받고 임의 구조체메모리를 받는다.


1. temp = list; 에서 temp 또한 malloc을 가르킨다.

2. temp= temp-> next; 에서   temp-> next는두번째 malloc을 받고 가르킨다.
    그리고 이것을 temp에 대입한다. data에 'b'를 삽입하고나서 
    temp -> next 다시 동적할당을 받는다.

3. temp= temp-> next; 에서 temp는 세 번째 malloc을 가르키게 된다.

이과정을 아래그림으로 표현 하였다.



posted by 송시혁

10- 3예제 응용


#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
  char data;
  struct node *next;
}NODE;
  
int main()
{
  NODE *list, *temp;
  list = (NODE *) malloc (sizeof(NODE));

  list->data = 'a';
  list->next =(NODE *) malloc (sizeof(NODE));
  
  list->data ='b';
  list->next->next=(NODE *) malloc(sizeof(NODE));
  
  list->next->next->data = 'c';
  list->next->next->next = NULL;
  
  temp = list;
  
  while (0!=temp)
  {
    printf("%5c\n", temp->data);
    list = list->next;
    free(temp);
    temp = list;
    
  }
  free(list);
  return 0;
}

  
while (NULL!=temp)
  {
    printf("%5c\n", temp->data);
   tmep = tmep->next;
    
    
    
  }




  




posted by 송시혁

배열의 경우를 생각하면, 배열안에 있는 배열을 삽입하거나 

삭제하기는 굉장히 힘들다. 그런점을 보완하기 위하여 

동적변수를 사용한다.


void *malloc()

가르키는 곳이 정해지지 않은 것.

주소를 가지지만, int*, short*등.... 자료형이 정해지지 않은 포인터이다.


float *pd;

void *vp=0;

vp=&a;

int a=100;

있다고 가정.

vp= pd; 둘다 주소값을 가지므로 에러는아니다.

하지만 printf문에서 *vp라고 선언한다면 에러가 뜬다.

vp는 주소값을 가지고 있으나, vp는 자료형이 없다.

즉, "float to void", * ...등의 에러가 뜬다.

그래서 casting이 중요하다.

*((int*)vp) 으로 casting 해준다면 문제없이 결과가 출력된다.


*vp, ++vp= 또한 에러를 일으키나 캐스팅을 한다면 문제없다.


int *p = malloc(sizeof(int))

메모리의 크기를 알 수 있다.

free()= 메모리가 필요없을 때 free(p); 실제로 주소를 적어야한다.

NULL = malloc()메모리가 없으면 NULL을 반환.

casting






'C언어 수업정리 > 4월 수업정리' 카테고리의 다른 글

4월 23일 연결리스트 다른방법  (0) 2013.04.23
4월 22일 연결리스트  (0) 2013.04.22
4월 17일 구조체 포인터 함수, 포인터  (0) 2013.04.17
4월 15일 package개념  (0) 2013.04.15
4월 15일 예제  (0) 2013.04.15
posted by 송시혁

typedef struct tag

{

}AA;라는 구조체가 있을때, 포인터 또한 선언 할 수 있다.


typedef struct tag

{

int inum;


}AA ;


AA *p;라고 선언했다고 가정했을때, .(도트연산자) 사용못함.


예시)

p.inum = 잘못된 예

p ->inum = 올바른 예

(*p).inum - 올바른 예  


예제 9-6, 9-7, 9-8참고


함수 (int, int, int, int) = 16byte

함수 (POINTER , POINTER)= 16byte

함수 (POINTER *p1, POINTER *p2)= 8byte

 



'C언어 수업정리 > 4월 수업정리' 카테고리의 다른 글

4월 22일 연결리스트  (0) 2013.04.22
4월 18일 void *malloc()함수, free()함수  (0) 2013.04.18
4월 15일 package개념  (0) 2013.04.15
4월 15일 예제  (0) 2013.04.15
4월 15일 구조체 변수 선언  (0) 2013.04.15
posted by 송시혁

#include <stdio.h>
#pragma pack(1)// 변수크기를 최적화.
struct SMART
{
  int a;
  char b;
  int c;
  short d;
  char e;
  int f;
  char g;
  short h;
  char i;
};


#include <stdio.h>
#pragma pack(4) // 1,2,4,8,16 사용가능.4= 8바이트가 커진다.
struct SMART1
{
  int a;
  char b;
  int c;
  short d;
  char e;
  int f;
  char g;
  short h;
  char i;
};
      

int main()
{
  
  
  
  
  printf("구조체 크기: %d\n"sizeof(struct SMART));
  printf("구조체 크기: %d\n"sizeof(struct SMART1));

  return 0;
}




posted by 송시혁

#include <stdio.h>
typedef struct 
{
  char name[20];
  int english;
  int math;
  float average;
}STUDENT;

int main()
{
  STUDENT st;
  
  printf("Please enter student name: ");
  gets(st.name);
  printf("Please enter english score : ");
  scanf("%d"&st.english);
  printf("Please enter math score : ");
  scanf("%d"&st.math);
  st.average = (float)(st.english + st.math)/2;
  printf("student name: %s\n", st.name);
  printf("average: %6.3f\n", st.average);
  return 0;
}



posted by 송시혁






1.태그를 생략한 경우.

#include <stdio.h>
struct
 
{
  int a;
  
  
}SMART;

SMART는 자료형으로 struct를 받는다. 
예를 들어 SMART a라고 선언하면 a는 smart가 자료형이 되어 struct
구조체가 된다. 러나 이 방법은 smart a, b이렇게 두가지이상을 선언하지
못한다. struct 다음에 이름을 지정해주지 않으면 변수명을
2가지 이상 할 수 없다.

#include <stdio.h>
typedef struct
 std_tag
{
  int a;
  
  
}smart;


이방법이 가장 일반적인 방법이다. smart1 a,b;이런식으로 사용가능
위의 방법보다 간결하고 변수를 여러개를 선언할 수 있다는 점때문에
구조체 선언시 가장 많이 쓰인다.
2.새로운 타입정의

#include <stdio.h>
struct
 _SMART
{
  int a;
  
  
};
typedef struct _SMART SMART;

struct _SMART = 원래 타입
SMART = 새로운 타입선언.


3.  2번타입의 변형 형태

#include <stdio.h>
struct
 _SMART
{
  int a;
  
  
}typedef struct _SMART SMART;



typedef struct _SMART
{
 
int a;
SMART;







  
  
}SMART1; 


이것은 위의 구조체선언을 하나로 합쳐버린것이다.





'C언어 수업정리 > 4월 수업정리' 카테고리의 다른 글

4월 15일 package개념  (0) 2013.04.15
4월 15일 예제  (0) 2013.04.15
4월 12일 문자열함수  (0) 2013.04.12
4월 12일 main()함수 응용  (0) 2013.04.12
4월 12일 main()함수의 인수  (0) 2013.04.12
posted by 송시혁



#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  int num;
  
  if(argc != 2)
  {
    printf("You enter the error\n");
    exit(-1);    
  }
  num = atoi(argv[1]);
  printf("Answer : %d\n", num *2);
  return 0;
}




#include <stdio.h>
int main(int argc, char *argv[])
{
  int i;
  printf("argc : %d\n", argc);
  for(i= 0; i<argc; i= i+1)
  {
    printf("argv[%d] = %s\n", i, argv[i]);

  }


  return 0;
}












'C언어 수업정리 > 4월 수업정리' 카테고리의 다른 글

4월 15일 예제  (0) 2013.04.15
4월 15일 구조체 변수 선언  (0) 2013.04.15
4월 12일 main()함수 응용  (0) 2013.04.12
4월 12일 main()함수의 인수  (0) 2013.04.12
4월 12일 구조체개념.  (0) 2013.04.12
posted by 송시혁
prev 1 2 3 4 next