-
Variable size arrays
Hi all, I'm having trouble with arrays ( Yes I'm new to C )
Here is what I want to do in a nutshell:
#include <stdio.h>
main ()
{
printf("Please input the size of the array you want");
int arrsize;
scanf("%d",&arrsize);
int myarray [arrsize];
}
I can't do that so I've been experimenting with malloc and have come up with this:
int arrsize;
scanf("%d",&arrsize);
int *numptr;
numptr = (int *) malloc (arrsize);
This is where I'm stuck, now I've got this chunk of memory ( is it the right size? ) how can I make it so it acts as an array.
IE
myarray [0]
myarray [1]
...
myarray [arrsize]
Thanks in advance for any help.
-
numptr = (int *) malloc (sizeof (int) * arrsize);
-
thanks, will I then be able to use that memory space as an array. If so how do I access it?
I want to add numbers to each each member of the array like this ideally:
int i;
for (i=0;i<=arrsize;i++) {
myarray[i] = i;
}
Any ideas
-
Pointers and dynamic allocation etc
That's the full details anyways, but there are also plenty of examples on here if you look for them.
-
Good link thanks, I've sorted my problem now.