Thread: size of array

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    size of array

    i am trying to have the user chose the size of the array
    what am i doing wrong?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main (){
    int size;
    int array [size];
    
    printf("Please enter the size of the array", size);
    scanf("%i", size);
    system("pause");
    }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Well for a start 'size' is undefined when you declare the array.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Two things are wrong.

    First, it's against the "law". When you declare "array", size is uninitialized. If you want a random sized array, you should malloc a chunk of storage from the heap.

    Second, your scanf should pass the address of size, not size itself.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Main returns int.
    Size contains garbage at the point you try to allocate the array, so theoretically it could get an undefined size. Secondly, this is only allowed in C99 and not C89.
    Thirdly, C is a linear programming language. One statement is executed after the other.
    Do the array won't be created after you've asked the size, it will be created before because the line is before the input.
    Fourthly, I think you need to take another look at printf.
    And avoid system("pause"). Better to use getchar().
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. It's not part of C89 standard to use variable size arrays. To be portable to C89 compilers, you should use dynamic memory allocation.

    2. You can not declare a variable size array before the size component has been assigned. Right now, when you are saying "int size;" , size contains some "random" value, and then try to create an array of that size. [The "random" value is usually the same for the same piece of code each time, but it's random in the sense that it's not defined by any standard and it may well change if you introduce another variable before it, change your code in some other way, change the compile options, etc, etc]

    So, if you want to use the C99 feature of variable sized arrays, you should at least set the size before creating the array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main (){
    int size;
    int array [1];
    
    printf("Please enter the size of the array", size);
    scanf("&#37;i", size);
    array[size];
    system("pause");
    }
    would this work?
    would array size be what the user enters now?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It would be 1.
    As I mentioned, it's linear. It won't just jump back and set a new size.
    And array[size] just access the element size in the array.

    Fix main and system("pause"), then go learn dynamic memory, malloc & free.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i have read my book but its too complicated so i am not understanding much of it

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    thats y i am trying to get some help here

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Mainframe assembler programmer by trade. C coder when I can.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, you need to understand pointers first.
    Then you simply call malloc to allocate requested amount and when you don't need it, you call free.

    Code:
    int main()
    {
        int* pArray;
        int size;
    
        printf("Enter size: ");
        scanf("%i", &size);
        pArray = malloc(size * sizeof(*pArray));
        free(pArray);
        pArray = NULL;
    
        getchar();
        return 0;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can I also point out that allocating or sizing arrays dynamicly is OFTEN not necessary - unless your application is large and/or uses A LOT of memory, just using "sufficiently large" arrays [e.g. an int array[10000]; will use 40KB of memory, which is not very much when your system has 10000x as much memory].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    am i on the right path?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main (){
    int size;
    
    printf("Please enter the size of the array", size);
    scanf("&#37;i", size);
    
    makeAnArray(size);
    
    system("pause");
    }
    int* makeAnArray(size){
    	int* array;
    array=calloc(size,sizeof(int));
    return array;
    }
    what does error C2040: 'makeAnArray' : 'int *()' differs in levels of indirection from 'int ()' mean?
    it point to the line: int* makeAnArray(size){
    how do i fix that?

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sheesh. Start listening.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    // MAIN RETURNS INT
    int main (){
        int size;
        // YOU HAVEN'T SPECIFIED ANYTHING FOR PRINTF TO PRINT, SO REMOVE THE "size" PART
        printf("Please enter the size of the array");
        // SCANF TAKES A POINTER
        scanf("&#37;i", &size);
    
        makeAnArray(size);
        // DON'T USE SYSTEM("PAUSE")
        getchar();
    }
    
    // ALL ARGUMENTS MUST HAVE A TYPE
    int* makeAnArray(int size){
        int* array;
        array=calloc(size,sizeof(int));
        return array;
    }
    You are also missing a prototype for makeAnArray.
    And you are not freeing the return from calloc.
    Last edited by Elysia; 04-02-2008 at 08:39 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Sheesh. Start listening.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    // MAIN RETURNS INT
    int main (){
        int size;
        printf("Please enter the size of the array", size);
        // SCANF TAKES A POINTER
        scanf("%i", &size);
    
        makeAnArray(size);
        // DON'T USE SYSTEM("PAUSE")
        getchar();
    }
    
    // ALL ARGUMENTS MUST HAVE A TYPE
    int* makeAnArray(int size){
        int* array;
        array=calloc(size,sizeof(int));
        return array;
    }
    You are also missing a prototype for makeAnArray.
    And you are not freeing the return from calloc.
    ANd missing storing the return from makeAnArray().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  2. Replies: 42
    Last Post: 12-19-2004, 08:59 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM