ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have the seguent code, simplified for debug purposes...
Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
int main() {
const double help = 0.0001;
int n_pop;
int n_ind[n_pop];
int i;
printf("How many populations?\n");
scanf("%i", n_pop);
printf("NOTE: It is NOT a sex-biased simulation, so you will have 1/2 males and 1/2 females!");
i=0;
while(i < n_pop) {
printf("How many individuals for the population ", i ,"?\n");
scanf("%i", n_ind[i]);
i++;
}
return 0;
}
But seems that I can't declare an array like n_ind with a size declared by user input...
Can you please help me as my compiler (Dev-C++) doesn't find any errors but I can't execute it...
I am working on Windows XP system (Yeah I know it's crap, but I couldn't find any IDE C/C++ to download as the website brings me to wrong download sites!! )
You should probably use pointers instead. You cannot declare arrays with floating sizes on the stack as the compiler requires to know the exact size of the array. You can allocate dynamic sized arrays on the heap using the functions malloc() and free() in pure C and the new and delete operators in C++.
Code:
/* set a size for the array, say 10 or any other user inputted value */
int arraylen = 10;
..
/* Allocate a double array on the heap. */
int* array = (int*)malloc (sizeof(int)*arraylen);
/* The C++ way: int* array = new int[arraylen]; */
..
/* do something with the array */
..
..
..
/* finished with the array. now deallocate the memory */
free (array);
/* The C++ way: delete[] array; */
It's important to "free" the memory because the program won't do it automatically as it's assigned on the heap and it can lead to memory leaks if you don't deallocate memory.
Last edited by vharishankar; 08-02-2006 at 10:54 PM.
Sorry I edited it out because I realized you were using C. Yes, you should use malloc() and free() in C as the operators new and delete are C++ specific.
A couple of suggestions (presumably, you've already found and corrected them):
Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <malloc.h> /* You need this for "malloc()" */
int main() {
const double help = 0.0001;
int n_pop;
int *n_ind = NULL; /* Declare array: we'll allocate it later */
int i;
printf("How many populations?\n");
scanf("%i", &n_pop); /* Be sure to pass "addressof" n_pop */
printf("NOTE: It is NOT a sex-biased simulation, so you will have 1/2 males and 1/2 females!");
n_ind = malloc (sizeof (int) * n_pop);
if (n_ind == NULL) {
printf ("ERROR: Unable to malloc %d bytes!\n", sizeof (int) * n_pop);
return 1;
}
i=0;
while(i < n_pop) {
printf("How many individuals for the population ", i ,"?\n");
scanf("%i", &n_ind[i]); /* "addressof" n_ind[i], too */
i++;
}
free (n_ind);
return 0;
}
1. Allocate a bulk of memory like a normal array (but use x*y in the malloc statement instead of just x). Then based on the array index, you can think of the first x as belonging to the first array and the sceond x elements as belonging to the second array and so on. This is easier for an array of fixed length arrays.
2. Use a pointer to pointers. (like in int** x). Allocate each pointer in the pointer array to its own array. This is probably better when you want each array in the array of arrays to be of a different length.
In general try and avoid 3 dimensional arrays (x, y, z). It will usually lead to confusion and probably memory leaks if you don't use them properly.
( All this assuming you want to create dynamic arrays using "malloc" rather than using static arrays like in int a[30][40] )
Last edited by vharishankar; 08-03-2006 at 12:51 AM.
#include <malloc.h> /* You need this for "malloc()" */
No you don't. malloc is part of the ANSI C standard header <stdlib.h>
Quote:
Originally Posted by Harishankar
1. Allocate a bulk of memory like a normal array (but use x*y in the malloc statement instead of just x). Then based on the array index, you can think of the first x as belonging to the first array and the sceond x elements as belonging to the second array and so on. This is easier for an array of fixed length arrays.
You can actually allocate arrays of arrays if you are careful with your syntax.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.