void example( int length )
{
int test[length];
}
#include <stdio.h>
#include <alloca.h>
#include <ctype.h>
// method 7 - like method 3
// return new string
//
// reentrant, caller must free string
// alloca() is equivalent to C99 variable arrays and while not
// necessarily portable more portable than C99 at this point.
char * capitalize_7(char const* src) {
char * dst = (char *) alloca(strlen(src) + 1);
strcpy(dst, src);
if (strlen(dst) > 0)
*dst = toupper(*src);
return strdup(dst);
}
#include "gc.h"
// method 8 - like method 3
// return new garbage collected string
//
// reentrant, caller need NOT free string
char * capitalize_8(char const* src) {
char * dst = GC_MALLOC_ATOMIC(strlen(src) + 1);
strcpy(dst, src);
if (strlen(dst) > 0)
*dst = toupper(*src);
return dst;
}
int main() {
char a[6] = "hello";
char * sptr;
sptr = capitalize_7(a);
printf("7: %s %s\n",a, sptr);
free(sptr);
sptr = capitalize_8(a);
printf("8: %s %s\n",a, sptr);
return 0;
}
#include <stdio.h>
#include <alloca.h>
#define VAR_ARRAY(X,Y,Z) X * Y = (X *) alloca(Z * sizeof(X))
// pre C99 variable arrays
// return copy of string
//
char * copy(char const* src, int len) {
VAR_ARRAY(char, dst, len);
strcpy(dst, src);
return strdup(dst);
}
// pre C99 variable arrays
// allocate an integer array and print it
//
void fill_n_print_array(int len) {
int i;
VAR_ARRAY(int, b, len);
for (i=0; i<len; i++)
b[i] = i;
for (i=0; i<len; i++)
printf("%d ", b[i]);
printf("\n");
}
int main() {
char a[6] = "hello";
char * sptr = copy(a, strlen(a)+1);
printf("copy: %s %s\n",a, sptr);
free(sptr);
fill_n_print_array(5);
fill_n_print_array(20);
return 0;
}