LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-02-2006, 10:41 PM   #1
pittopitto
Member
 
Registered: Sep 2005
Location: Australia
Distribution: SUSE Linux Professional 9.3
Posts: 35

Rep: Reputation: 15
A problem in declaring an array in C...


Hey folks!

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!! )

Ciao ciao

Pittopitto
 
Old 08-02-2006, 10:43 PM   #2
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
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.
 
Old 08-02-2006, 10:52 PM   #3
pittopitto
Member
 
Registered: Sep 2005
Location: Australia
Distribution: SUSE Linux Professional 9.3
Posts: 35

Original Poster
Rep: Reputation: 15
Wow that was quick!!

I modified the code, but I think the C language does not recognise the 'new' and 'delete' instructions...

I am very sorry but I am just learning the C language so not really an expert...

pittopitto
 
Old 08-02-2006, 10:55 PM   #4
pittopitto
Member
 
Registered: Sep 2005
Location: Australia
Distribution: SUSE Linux Professional 9.3
Posts: 35

Original Poster
Rep: Reputation: 15
what?? before I didn't see the C instructions and now I do!!!

pittopitto
 
Old 08-02-2006, 10:59 PM   #5
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
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.
 
Old 08-02-2006, 11:05 PM   #6
pittopitto
Member
 
Registered: Sep 2005
Location: Australia
Distribution: SUSE Linux Professional 9.3
Posts: 35

Original Poster
Rep: Reputation: 15
That's OK!

Now it's working perfectly!! Thank you very much!!
Do you know a online reference library of all the C instructions?

Many Thanks
 
Old 08-02-2006, 11:12 PM   #7
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
There are *plenty* of good books and online manuals to learn the C programming language.

Here's a site from a quick web search:
http://www-ccs.ucsd.edu/c/index.html

Take a look at the links down at the bottom of the wikipedia page:
http://en.wikipedia.org/wiki/C_programming_language

You should probably buy Kernighan & Ritchie's book "The C Programming Language" if you want to know straight from the horse's mouth.

http://www.amazon.com/gp/product/013...lance&n=283155

Last edited by vharishankar; 08-02-2006 at 11:13 PM.
 
Old 08-02-2006, 11:46 PM   #8
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

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;
}
'Hope that helps .. PSM
 
Old 08-03-2006, 12:04 AM   #9
pittopitto
Member
 
Registered: Sep 2005
Location: Australia
Distribution: SUSE Linux Professional 9.3
Posts: 35

Original Poster
Rep: Reputation: 15
Thank you all folks!! It is working...

I need the malloc function also for a 2-D and 3-D array!!!! Is that hard to do it??

Many thanks!
 
Old 08-03-2006, 12:48 AM   #10
vharishankar
Senior Member
 
Registered: Dec 2003
Distribution: Debian
Posts: 3,178
Blog Entries: 4

Rep: Reputation: 138Reputation: 138
For 2d arrays there are two strategies.

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.
 
Old 08-03-2006, 02:28 AM   #11
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 51
Quote:
Originally Posted by paulsm4
Code:
#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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault after declaring a large array. oulevon Programming 6 11-08-2005 02:41 AM
C++: Trouble declaring dynamic array Hady Programming 7 11-01-2005 01:06 AM
questions about declaring functions? alaios Programming 4 07-21-2005 02:25 AM
difference between () and = when declaring variables nadroj Programming 1 06-28-2005 01:37 PM
C: declaring void parameters jrtayloriv Programming 4 02-11-2005 02:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration