Created
March 14, 2013 05:44
-
-
Save sooop/5159105 to your computer and use it in GitHub Desktop.
이중포인터를 사용하여 문자열의 배열을 동적으로 할당하고 해제하는 예제.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> // for calloc() | |
#include <string.h> // for strcpy() . not necessary because compiler will automatically include string.h. (with warning) | |
#define MAXNUM 50 | |
int main(void) | |
{ | |
char **arrs; // declare arrs with pointer of char pointer. | |
arrs = (char**)calloc(MAXNUM, sizeof(char*)); // allocate 50*4bytes to arrs. it holds address of each string array. | |
int i; | |
for(i=0;i<MAXNUM;i++) { | |
arrs[i] = (char*)calloc(sizeof(char),13); // allocate memory of each string | |
strcpy(arrs[i],"TEST PROGRAM"); | |
} | |
i = 0; | |
while(i<MAXNUM) { // print and free. | |
printf("%x : %s", (int)arrs[i], arrs[i]); | |
free(arrs[i]); | |
} | |
free(arrs[i]); // free arrs. | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment