Thread: How to use an unknown/variable number of variables.

  1. #1
    Registered User slabofguinness's Avatar
    Join Date
    Nov 2011
    Posts
    1

    How to use an unknown/variable number of variables.

    I want to allow the user of my program to load a dataset from a text file. The data will be stored in an array of doubles.

    I also want the user to specify how many datasets they wish to load into the program in total and then define the corresponding number of arrays.

    My problem is that I don't know how many arrays will be needed in advance (or their size). Does anyone know how to declare an unknown/arbitrary number of variables?

    Here is a simplified example of what I want to achieve, where I've used integers instead of arrays. Obviously I want to declare a distinct integer each time inside the loop.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int no_of_integers, i;
        char buffer[100];
        
        printf("Enter the number of integers you want to input\n");
        fgets(buffer, sizeof(buffer), stdin);
        sscanf(buffer, "%d", &no_of_integers);
        
        for (i = 1; i <= no_of_integers; i++){
        
            int integer_i;
            
            printf("Enter integer %d\n", i);
            fgets(buffer, sizeof(buffer), stdin);
            sscanf(buffer, "%d", &integer_i);
            
        }
        
    }

    (I'm actually not using arrays, but instead matrices as explained in Numerical Recipes in C 2nd Edition, Section 1.2. Although this doesn't change my problem)

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, you want to learn about dynamic memory management (in C).

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
    	int **matrix, r, c, i;
    	puts("Enter rows[tab]columns");
    	scanf("%d\t%d", &r, &c);
    	matrix = malloc(r*sizeof(int*));
    	for (i=0; i<r; i++) matrix[i] = malloc(c*sizeof(int));
    	return 0;
    }
    Dynamic Memory Allocation, Part 1
    Beware that discusses C and C++ simultaneously. The C++ stuff is not valid in C.

    Chapter 11: Memory Allocation
    Last edited by MK27; 11-18-2011 at 08:49 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need advice on umm.. variable number of variables haha.
    By ljrobison in forum C++ Programming
    Replies: 9
    Last Post: 02-21-2010, 08:32 PM
  2. A clever way to name unknown amount of variables
    By Maz in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2006, 11:59 PM
  3. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM
  4. Replies: 5
    Last Post: 02-09-2003, 10:03 AM
  5. unknown number of chars
    By Garfield in forum C Programming
    Replies: 16
    Last Post: 10-31-2001, 05:46 AM

Tags for this Thread