array of unknown size

This is a discussion on array of unknown size within the C Programming forums, part of the General Programming Boards category; Hi, How would I use an array that doesn't have a size in its declaration? I want this array to ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    100

    array of unknown size

    Hi,
    How would I use an array that doesn't have a size in its declaration? I want this array to be assigned an unknown number of values over the course of the program , but trying to declare an array of 0 size can't be done.

    Code:
    double valid_rates[VALID_PAY_RATES] = {3.50, 4.00, 4.50, 4.75, 5.00, 5.50, 5.75, 6.00},
    invalid_rates[UNKNOWN_VALUE],
    rate;
    
    int hours;
    
    
            printf("\nPlease enter the number of hours: ");
    	scanf("%d" , &hours);
    
    	printf("\nPlease enter the rate of pay: ");
    	scanf("%lf" , &rate);
    
    for(individual_rates=0; individual_rates<VALID_PAY_RATES; ++individual_rates)
    
    		if(rate == valid_rates[individual_rates]) 
    		
    		{
    			if(hours > 40)
    			
    				{
    					hours_overtime = hours - 40;
    				    overtime_pay = hours_overtime * (1.5 * rate);
    					regular_pay = rate * 40;	
    				}
    		
    			else
    		
    				regular_pay = rate * hours;
    									
    			
    			printf("\nThe regular pay is %2.2f", regular_pay);
    			printf("\nThe overtime pay is %2.2f", overtime_pay);
    			printf("\nThe gross pay is %2.2f\n\n", overtime_pay + regular_pay);
    
    			grand_total_salaries += overtime_pay + regular_pay;
    			++num_of_employees;
    			overtime_pay=0;
    
    			printf("\nDo you want to process another employee?(1 for yes, 0 for no): ");
    			scanf("%d" , &response);
    			break;		
    			
    		}
    			
    	
    		else
    		
    			if((rate != valid_rates[individual_rates]) && (individual_rates == VALID_PAY_RATES-1))
    		
    			{
    		
    				printf("\nERROR:INVALID PAY RATE, valid rates are: ");	
    				printf("\n$3.50 $4.00 $4.50 $4.75 $5.00 $5.50 $5.75 $6.00 -- Please try again\n\n");
    				
    			  		invalid_rates[UNKNOWN_VALUE] = rate;
    
                                /*each time there is an invalid rate entered, it    
                                       should be stored in invalid_rates[](somehow)*/
    
    		        }
    any help would be appreciated.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Arrays must have a defined size at compile time. If you're looking for something that can change in run time, you'll need dynamic allocation (and perhaps reallocation).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Not really sure what I would need for this. Haven't learned the language beyond arrays yet.
    There will be an unknown number of invalid pay rates depending on how many users are processed and how many enter invalid rates. (The whole thing will be enclosed in a while loop).

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by richdb
    There will be an unknown number of invalid pay rates depending on how many users are processed and how many enter invalid rates. (The whole thing will be enclosed in a while loop).
    If an invalid rate is entered, don't accept it and make the user take another shot at it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    Thats what the program does now, I just want to be able to display the incorrect entries when the program ends.

  6. #6
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    You need to use the functions malloc(), realloc() and free(). There's also calloc(). These are declared in stdlib.h however if you say that you haven't learned anything beyond arrays then I am guessing you haven't gotten used to pointers yet and if that is the case I wouldn't advise trying to handle dynamic memory as it can be quite tricky. I'd recommend to just make your arrays really big for now and when you feel confident about using pointers you can return to your old code and try to implement dynamic memory for those arrays instead.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    100
    I had a feeling it was something like that I had thought of making the array huge, but wanted something more elegant than that. Guess Ill have to read on.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Look at this program:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        int *a = 0, *p, n = 0, x, i;
    
        printf("Enter some numbers, 0 to stop\n");
    
        while(scanf("%i", &x) && x) {
            p = realloc(a, sizeof(int) * (n+1));
            if(!p) {
                perror("Out of memory");
                free(a);
                return 1;
            }
            a = p;
    
            a[n++] = x;
        }
    
        for(i = 0; i < n; i ++) {
            printf("%i\n", a[n]);
        }
    
        free(a);
    
        return 0;
    }
    I hope it helps.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocating an array of unknown size?
    By scarlet00014 in forum C Programming
    Replies: 2
    Last Post: 09-27-2008, 10:53 AM
  2. Replies: 1
    Last Post: 09-10-2005, 07:02 AM
  3. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 04:06 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Maximum array size?
    By code2big in forum C Programming
    Replies: 2
    Last Post: 05-25-2004, 09:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21