Created
January 9, 2020 12:51
-
-
Save frankie-yanfeng/9bea13f8a0f9dc3e9b1720988cd33b05 to your computer and use it in GitHub Desktop.
Flexible array member
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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