Thread: Variable length arrays

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    6

    Variable length arrays

    Hello,

    I am very new on C programing and have crossed the following error out of which I can get off:

    "numbersort1.c", line 22: error #2028: expression must have a constant value
    int numbers[size];
    The code is:
    #include <stdio.h>

    Code:
    int arraysize=0;
    void bubblesortit(int *numbers){
            int arrayposition=0;
            int aux;
    
            for(arrayposition=sizeof(numbers)/sizeof(int); arrayposition > -1; arrayposition--){
                    for(int x=0; x < (arrayposition - 1); x++){
                            if (numbers[x] > numbers[x+1]){
                                    aux=numbers[x];
                                    numbers[x]=numbers[x+1];
                                    numbers[x+1]=aux;
                            }
                    }
            }
    }
    
    main(const int size){
            printf("Informe o numero de dados a inserir: ");
            scanf("%d", &arraysize)a;
            int numbers[arraysize];
            for(int x=0; x < arraysize; x++){
                    printf("Informe o numero para a posicao %d: \n", x+1);
                    scanf("%d", &numbers[x]);
            }
            bubblesortit(numbers);
            printf("Os valores informados foram\n");
            for(int x=0; x < arraysize; x++){
                    printf("%d", numbers[x]);
            }
    }
    Can anybody tell me how to set this array variable length to get this gig running?

    Note: the intent here is to make it run, I am not looking for best practices as of now.

    Thank you

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    I think your problem is here:


    Code:
    rrayposition=sizeof(numbers)/sizeof(int)
    You can't do this in C, since sizeof(numbers) gives you the size of the pointer rather than the array. if you want to do that, you have to define is as a macro instead, like

    Code:
    #define ARRAYSIZE(array) sizeof(array)/sizeof(array[0])
    of course, I could be wrong cause I'm fairly new too. Also this line

    Code:
    scanf("%d", &arraysize)a;
    What is the a?

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    6
    Thank you.

    The "a" you refer to does not exist on the actual code, it must have been something I typed accidentaly while posting it.

    The thing is I need to define the size of the array depending on the number of records the user tell the system it is going to insert on runtime, so the define option would not help me in this case. Ain't there any other approach for this?

    Thank you.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Be sure you're compiling in C99 mode (and not, say C++ mode).

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Passing main a "const int size" parameter seems pretty strange to me. That will get the value of int argc, assuming your compiler doesn't mind you using a non-standard main() declaration.

    Look closely at these expressions:
    Code:
            for(arrayposition=sizeof(numbers)/sizeof(int); arrayposition > -1; arrayposition--){
                    for(int x=0; x < (arrayposition - 1); x++){
    First of all, "arrayposition > -1" is the same as "arrayposition >= 0", which makes more sense in my mind at least. Also, if arrayposition == 0, or indeed even if arrayposition == 1, then the inner loop doesn't do anything (it loops zero times), so I think you could optimize the outer expression to avoid that.

    As you are probably aware, variable length arrays are C99 only. If you want to, you can simply use dynamic memory allocation instead. Use
    Code:
    int *numbers = malloc(sizeof(*numbers) * arraysize);
    to declare the "array" and
    Code:
    free(numbers);
    before the program exits to free it.
    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.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    9
    i can see that the program u wrote is about a sorting for a variable length of an array so i'll write for u another code could be much easier to u:
    #include<stdio.h>
    #include<stdlib.h>
    void swap(int *a,int *b)
    {
    int t;
    t=*a;
    *a=*b;
    *b=t;
    }
    void sort(int *a,int n)
    {
    int i,j;
    for(i=n-2;i>=0;i--)
    for(j=0;j<=i;j++)
    if(*(a+j)>*(a+(j+1)))
    swap((a+j),(a+(j+1)));
    }
    void print(int *a,n)
    {
    int i;
    for(i=0;i<n;i++)
    printf("%d\t",*(a+i));
    }
    int main()
    {
    int *array,n;
    printf("Enter size of array needed:");
    scanf("%d",&n);
    array=(int *)malloc(sizeof(int)*n);
    if(array!=NULL)
    {
    int i;
    for(i=0;i<n;i++)
    {
    printf("Enter %d no:");
    scanf("%d",&array[i]);
    }
    sort(array,n);
    print(array,n);
    return 0;
    }
    else
    printf("Not enough memory");
    }

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Mostafa - when you're posting code - please use code tags

    << !! Posting Code? Read this First !! >>

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    printf("Enter %d no:");
    %d matches . . . what?

    And there's no need to cast malloc(). See the FAQ.
    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.

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    9
    sorry i forgot to write it
    it should be
    "printf("Enter %d no.:",i+1);"
    this is like a counter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird modification to string length
    By ChwanRen in forum C Programming
    Replies: 0
    Last Post: 08-17-2003, 10:45 AM
  2. How to declare global arrays of unknown length?
    By ajm218 in forum C Programming
    Replies: 3
    Last Post: 08-07-2003, 09:13 AM
  3. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  4. Need help
    By awkeller in forum C Programming
    Replies: 2
    Last Post: 12-09-2001, 03:02 PM
  5. length of string etc.
    By Peachy in forum C Programming
    Replies: 5
    Last Post: 09-27-2001, 12:04 PM