Skip to content

Instantly share code, notes, and snippets.

char* s_gets(char* st, int n)
{
char* ret_val; // return value
char* find;
ret_val = fgets(st, n, stdin); // vs. scanf()
if (ret_val)
{
find = strchr(st, '\n'); // looking for newline
if (find) // if the address is not NULL
#include <stdio.h>
int main()
{
if (ptr != NULL)
{
for (int i = 0; i < n; i++) // malloc(), 초기화 해주지 않음
printf("%f ", ptr[i]);
printf("\n");
#include <stdio.h>
#include <stdlib.h> // malloc(), free()
int main()
{
double* ptr = NULL;
//void* :generic pointer.. pointer arithmetic X
ptr = (double*)malloc(30 * sizeof(double));
#include <stdio.h>
int main()
{
int row = 3, col =2;
int(*ptr2d)[2] = (int(*))[2]malloc(sizeof(int)* 2 * 3); // 2차원배열을 1차원 배열처럼
/* 동적할당메모리를 많이 씀
int(*ptr2d)[2] = (int(*))[2]malloc(sizeof(int) *2 * 3);
if (!ptr2d) exit(1);
//gets(),puts(), 배열개수 초과하면 Runtime Error 발생
//fgets(),fputs(), 읽어들이는 문자가 언제 끝날지 모른다는 것을 염두해놓음
#include <stdio.h>
#define STRLEN 81
int main()
{
char words[STRLEN] = "";
#define STRLEN 81
char *custom_string_input(char* str, int n);
int main()
{
char *name = ""; // Runtime Error, 문자열을 입력받기 위해서는 먼저 공간을 확보해야 한다
char name[128];
int result = scanf("%s", name);
#include <stdio.h>
#define MESSAGE "A symbolic string constant" // (3) 매크로 사용
#define MAXLEN 81
int main()
{
char words[MAXLEN] = "A string in an array"; // (1) 배열 사용
char *ptr = "A pointer to a string."; // (2) 포인터 사용
#include <stdio.h>
int main()
{
char *name[] = {"Aladdin", "jasmine", "Magic Carpet", "Genie"};
const int n = sizeof(name) / sizeof(char*);
for (int i = 0; i < n; i++)
printf("%s at %u\n", name[i], (unsigned)name[i]); // Use %ull in X64 build
printf("\n");
#include <stdio.h>
int main()
{
const double arr[4] = {1.0, 2.0, 3.0, 4.0};
const double *pd = arr;
printf("%f %f\n", pd[2], arr[2]); //**insight :배열처럼 오프셋을 이용하여 사용할 수 있다
pd[2] = 100; // Not allowed