I basically want to the C of equivalent of this (well, just the part with the array, I don't need the class and string parsing and all that):
public class Example
{
static int[] foo;
public static void main(String[] args)
{
int size = Integer.parseInt(args[0]);
foo = new int[size]; // This part
}
}
Pardon my C ignorance. I've been corrupted by java ;)
/* We include the following to get the prototypes for:
* malloc -- allocates memory on the freestore
* free -- releases memory allocated via above
* atoi -- convert a C-style string to an integer
* strtoul -- is strongly suggested though as a replacement
*/
#include <stdlib.h>
static int *foo;
int main(int argc, char *argv[]) {
size_t size = atoi(argv[ 1 ]); /*argv[ 0 ] is the executable's name */
foo = malloc(size * sizeof *foo); /* create an array of size `size` */
if (foo) { /* allocation succeeded */
/* do something with foo */
free(foo); /* release the memory */
}
return 0;
}
Caveat:Off the cuff stuff, without any error checking.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With