1. 포인터의 개념

데이터가 저장된 주기억장치의 주소만 저장 (갂단히 포인터(pointer)라고도 함)


2. 포인터와 배열의 관계 예(소스코드,실행결과)


#include <stdio.h>

#include <stdlib.h>

#define N 4


/* run this program using the console pauser or add your own getch, system("pause") or input loop */


//void print_arr(int arr[]); //3. 함수의 원형 선언 

void print_arr(int *arr);


int main(int argc, char *argv[]) {

int count[N] = {42, 37, 83, 33}; // 배열이름 = 시작주소 

//1. 출력할 함수의 이름 정의

print_arr(count); 

return 0;

}


//2. 함수 구현

//void print_arr(int arr[]) //int arr[N]배열을 다시 선언하면서 받는다. 

void print_arr(int *arr) //배열의 주소가 넘어오므로 포인터로 받는다. 

{

int i;

for(i=0;i<N;i++) 

printf("%3d", *(arr+i));

//printf("%3d", arr[i]);



3. 포인터를 이용한 문자열 교환 예제


#include <stdio.h>

#include <stdlib.h>

#define SIZE 5


/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main(int argc, char *argv[]) {


char *first = "GilDong", *last = "Hong"; // 이름 , 성 

char *temp;   // 임시 저장 포인터 변수 

   

printf("Name : %s %s \n", first, last);

    

   

   // 포인터 변수에 저장된 두 주소를 교환해 두 포인터가 가리키는 문자열 교환하기

   

temp = first;

first = last;

last = temp;

   

printf("Name : %s %s \n ", first, last); 



   return 0;

}




4. char형 배열을 이용한 문자열 교환 예제


#include <stdio.h>

#include <stdlib.h>

#define SIZE 5


/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main(int argc, char *argv[]) {


 char first[10] = "GilDong", last[10] = "Hong"; // 이름과 성

   char temp[10]; // 문자열을 임시로 저장할 배열


   printf("Name : %s %s\n", first, last);


 // 배열의 내용을 직접 교환하여 두 문자열을 교환하기

   strcpy(temp, first); // first 배열의 문자열을 temp 배열에 복사하기

   strcpy(first, last); // last 배열의 문자열을 first 배열에 복사하기

   strcpy(last, temp); // temp 배열의 문자열을 last 배열에 복사하기

   

   printf("Name : %s %s \n", first, last);



   return 0;

}




5. char형 2차원 배열을 이용한 정렬 예제


#include <stdio.h>

#include <stdlib.h>

#define SIZE 5


/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main(int argc, char *argv[]) {


char silver[SIZE][10] = {"나태희","유빈","나원빈","문건영","소지법"};

char temp[10];

int i, pass; 

printf("**은메달 리스트 ");

for(i=0;i<SIZE;i++)

printf("%s, ", silver[i]);

printf("\b\b **\n\n");

for(pass=1;pass<SIZE;pass++)

for(i=0;i<SIZE-pass;i++)

if(strcmp(silver[i], silver[i+1]) > 0 )

{

strcpy(temp, silver[i]);

strcpy(silver[i], silver[i+1]);

strcpy(silver[i+1], temp);

}

printf("**정렬한 리스트 ");

for(i=0;i<SIZE;i++)

printf("%s, ", silver[i]);

printf("\b\b **\n\n");


   return 0;

}




6. char형 포인터배열을 이용한 예제


#include <stdio.h>

#include <stdlib.h>

#define SIZE 5


/* run this program using the console pauser or add your own getch, system("pause") or input loop */


int main(int argc, char *argv[]) {


   char *gold[5] = {"한빛","성춘향","이몽룡","사공민국","황해"};

   char *temp;

   int size, i, j;

   

   size = sizeof(gold) / sizeof (gold[0]);

   

   printf("** 금메달 리스트 : ");

   for(i=0;i<size;i++)

      printf("%s, ", gold[i]);

      printf("\b\b ** \n\n");

   

   for(i=0;i<size;i++)

      for(j=i+1;j<size;j++)

         if(strcmp(gold[i], gold[j]) > 0 )

         {

            temp = gold[i];

            gold[i] = gold[j];

            gold[j] = temp;

         }

   

   printf("** 정렬한 리스트 : ");

   for(i=0;i<size;i++)

      printf("%s, ", gold[i]);

      printf("\b\b ** \n\n");


   return 0;

}




'basic > C' 카테고리의 다른 글

8. 구조체  (0) 2016.11.21
6. 함수  (0) 2016.10.31
5. 배열  (0) 2016.10.13
4. 제어문(조건문과 반복문)  (0) 2016.10.06
3. 연산자  (0) 2016.09.22

+ Recent posts