02-02-2004, 05:20 AM
Is there an equivalent of the QB REDIM in C, so I can dynamically allocate arrays? Because I need to specify a variable, not a constant, for the array subscript upon declaration.
#include <stdlib.h>
void test(void) {
char *varString;
/* Allocate a 10 byte string */
varString = malloc(sizeof(10 * sizeof(char));
/* Reallocate varString as a 20 byte string */
varString = realloc(varString, 20 * sizeof(char));
}
char varString[10];
realloc(varString,11*sizeof(char));
Quote:1) Why do you place the times-ten multiplication inside the sizeof function? Why don't you just do varString=malloc(10 * sizeof(char));?
varString = malloc(10 * sizeof(char));
varString = malloc(10);
char *array;
/* Allocate 10 bytes */
array = malloc(10 * sizeof(char));
/* Reallocate to 20 bytes */
array = realloc(20 * sizeof(char));
/* Set and element in the array */
array[15] = 'x';
printf("array[15] = %c\n", array[15]);
#include <stdio.h>
#include <stdlib.h>
int main()
{
int array[3]={3,6,3};
realloc(array,4*sizeof(int));
array[4]=5;
printf("%d",array[4]);
return 0;
}
a% = VARPTR(array%(0))
void alterValues(int a, int b, int *sum, int *subs)
{
*sum = a + b;
*subs = a - b;
}
int main(void)
{
int a = 10, b = 5;
int s1, s2;
alterValues (a, b, &s1, &s2);
printf("%i+%i=%i; %i-%i=%i\n",a,b,s1,a,b,s2);
}