C++프로그래밍

2013.03.21

성엽이 2013. 3. 21. 15:53

 

2013.03.21.zip

5_5

#include <stdio.h>

int main()
{
  int i;
  float *fp;

  fp = &i; // 캐스팅해서 자료형을 맞춰줘야 한다.
  i = 357;
  
  *fp = *fp + 1;
  printf("%d\n", *fp);  

  return 0;
}

 

5_6

#include <stdio.h>

int main()
{
  int i = 3;
  int *ip;

  printf("%p\n", ip); // ip가 초기화되지 않은 상태에서 값을 출력
  
  *ip = 21;
  
  printf("%d\n", *ip);
  
  return 0;
}

 

5_7

// 포인터 변수를 이용한 연산

#include <stdio.h>

int main()
{
  int x = 5;
  int *xp = &x;

  x = *xp + 24// 포인터 변수를 일반 변수와 같이 사용
  printf("%d\n", x);

  return 0;
}

 

test

#include <stdio.h>

int main()
{
  float fNum = 10000.625f; // f를 끝에 붙여줘야 float형
  unsigned char *ucp;
  ucp = (unsigned char *)&fNum;

  printf("%02X\t%02X\t%02X\t%02X", *(ucp+0), *(ucp+1), *(ucp+2), *(ucp+3));//각각의 주소값 16진수로 출력
  
  return 0;
}

 

5_8

// 주소연산을 보여주는 프로그램의 예

#include <stdio.h>

int main()
{
  short snum = 5;
  short *sp = &snum;
  float fnum = 2.3f;
  float *fp = &fnum;

  printf("sp의 값 : %p\n", sp);
  printf("증가된 sp의 값 : %p\n", ++sp);
  printf("fp의 값 : %p\n", fp);
  printf("증가된 fp의 값 : %p\n", ++fp);

  return 0;
}

 

5_9

// 텍스트파일의 내용을 화면과 파일로 출력하는 프로그램

#include <stdio.h>

int main()
{
  int score;
  char name[10];  //10문자로 구성된 배열변수
  FILE *fpin;  //파일 포인터 변수의 선언
  FILE *fpout; 
  
  fpin = fopen("d123.in","r");
  fpout = fopen("d123.out","w");

  while(!feof(fpin))
  {
    fscanf(fpin, "%s %d", name, &score);  //파일로부터 읽기
    printf("%s\t%d\n", name, score);  //화면에 출력
    fprintf(fpout, "%s\t%d\n", name, score); //파일에 기록
  }
  fclose(fpin);
  fclose(fpout);
  return 0;
}