Skip to content

Instantly share code, notes, and snippets.

@frankie-yanfeng
Created January 9, 2020 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankie-yanfeng/9bea13f8a0f9dc3e9b1720988cd33b05 to your computer and use it in GitHub Desktop.
Save frankie-yanfeng/9bea13f8a0f9dc3e9b1720988cd33b05 to your computer and use it in GitHub Desktop.
Flexible array member
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFLEN 10
typedef struct example *PtrToExample;
struct example{
int a;
int p[0];
};
int main(){
PtrToExample P = (PtrToExample)malloc(sizeof(struct example)+sizeof(int) * BUFFLEN);
//memset(P,0, sizeof(struct example)+BUFFLEN);
if (P == NULL)
exit(1);
P->a = 100;
for (int i = 0; i < BUFFLEN; i++){
P->p[i] = i;
}
for (int i = 0; i < BUFFLEN; i++)
printf("%d\n", P->p[i]);
printf("%d\n", P->a);
free(P);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment