Skip to content

Instantly share code, notes, and snippets.

@skuralll
Created September 5, 2020 12:26
Show Gist options
  • Save skuralll/1bbe329d975dea0933173cbc8fe16465 to your computer and use it in GitHub Desktop.
Save skuralll/1bbe329d975dea0933173cbc8fe16465 to your computer and use it in GitHub Desktop.
string型に似たようなもの(可変長文字配列)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int len;
char str[];
} string;
string* getString(char chars[], int length){
string* ret = malloc(sizeof(string) + sizeof(char)*length);
ret->len = length;
strcpy(ret->str, chars);
return ret;
}
@skuralll
Copy link
Author

skuralll commented Sep 5, 2020

使用後はfreeでメモリを解放すること(必須)

@skuralll
Copy link
Author

skuralll commented Sep 5, 2020

https://www.jpcert.or.jp/sc-rules/c-dcl38-c.html
この構造体を用いた配列は作れないためこのコードに実用性はない

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment