Skip to content

Instantly share code, notes, and snippets.

@sooop
Created March 14, 2013 05:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sooop/5159105 to your computer and use it in GitHub Desktop.
Save sooop/5159105 to your computer and use it in GitHub Desktop.
이중포인터를 사용하여 문자열의 배열을 동적으로 할당하고 해제하는 예제.
#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