Skip to content

Commit

Permalink
Add vla61.c to demonstrate FAM of fixed size strings
Browse files Browse the repository at this point in the history
  • Loading branch information
jleffler committed Dec 21, 2023
1 parent a17e093 commit 998012d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/flex-array-members/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
famcopy
famsize
gcc-fam
vla61
27 changes: 27 additions & 0 deletions doc/flex-array-members/vla61.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* Demonstrating a FAM of fixed size strings */
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct VLA
{
int num;
char vla[][32];
} VLA;

int main(void)
{
int num = 10;
VLA *vla = malloc(sizeof(VLA) + num * sizeof(vla->vla[0]));
assert(vla != 0); // Lousy test mechanism!
for (int i = 0; i < num; i++)
strcpy(vla->vla[i], "Arbitrary medium length string");
vla->num = num;

for (int i = 0; i < vla->num; i++)
printf("%d: [%s]\n", i, vla->vla[i]);

free(vla);
return 0;
}

0 comments on commit 998012d

Please sign in to comment.