konebeta

C/C++

C const char* -> string, hello world

comi-soft
comi-soft
2025-05-24 09:46:25
조회 2104 · 좋아요 0

그냥 웹에서 타자 작성

#include <unistd.h>
#define tem_con(T, name) \
typedef struct { \
T* d; unsigned long long int size; \
} name
tem_con(char, string);
#define create_string(c_str) \
(string){c_str, sizeof(c_str)}
#define _for_(T, val, arr) \
for(T* val = arr.d; val != (char*)arr.d + arr.size; val++)
int main(int argc, char** argv) {
  string str = create_string("hello_world");
  _for_(char, it, str ) {
    write(1, it, 1);
  }
  return 0;
}


#include <unistd.h>

#define template_one_container(T, name) \
struct { \
    T* d; \
    unsigned long long int size; \
} name

#define template_container(T, name) \
typedef template_one_container(T, name)

template_container(char, std_string);

#define create_string(c_str) \
  (std_string){ c_str, sizeof(c_str) }

#define create_array(format) \
  { format, sizeof(format) }

#define define_array(T, name, count) \
  T __CA__##name[count]; \
  template_one_container(T, name) = \
  create_array(__CA__##name)

#define _for_(T, val, arr) \
  for(T* val = arr.d; (char*)val != (char*)(arr.d) + arr.size; val++ )

int toString(int number, std_string str)
{
    char* str_ptr = str.d+str.size;
    char* write_ptr = str.d;
    if(number == 0) {
        str.d[0] = '0';
        return 1;
    } else if(number < 0) {
        *(write_ptr++) = '-';
        number = -number; // 최소값은 저는 모르는거에욧
    }
    while(--str_ptr != str.d -1 && number != 0)
      *str_ptr = number % 10 + '0', number /= 10;
    while(str_ptr != str.d+str.size)
      *(write_ptr++) = *(str_ptr++);
    *(write_ptr++) = '\0';
    return (int)(write_ptr - str.d);
}

int main(int argc, char** argv)
{
    std_string string = create_string("hello world\n");
    define_array(int, intInArray, string.size);
    char* str_ptr = string.d;
    _for_(int, it, intInArray) {
        if(str_ptr == string.d + string.size) break;
        *it = *(str_ptr++);
    }
    char __buffer__[32] = {0};
    std_string buffer = create_string(__buffer__);
    _for_(int, it, intInArray) {
        int size = toString(*it, buffer);
        buffer.d[size++] = ' ';
        write(1, buffer.d, size);
    }
    
    return 0;
}

숫자 hello world

0

댓글 0

default_user_icon
0/500자