Thread: Define an array/vector

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    5

    Define an array/vector

    Hello, I have tried this for many hours now, but I can't get it to work.


    Lets say I want to define an array, with 10 places that are zero, then this work:

    Code:
    #include <stdlib.h>
    
    main(){
    
    
    int A[10]={0};
    
    
    }

    But lets say that the size of the array is not know. Call it x, the user will actually give x, then I have to use a code like

    Code:
    #include <stdlib.h>
    
    main(){
    int x;
    x=10;
    
    int A[x]={0};
    
    }


    testy.c: In function ‘main’:
    testy.c:7: error: variable-sized object may not be initialized
    testy.c:7: warning: excess elements in array initializer
    testy.c:7: warning: (near initialization for ‘A’)
    Why do I get this, one must be able to define an array even though you as the programmer don't know the size?


    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////QUESTION 2////////////////////////////////////////////////////////////////////////////////////////////
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    If I define an Array to be to size 10. I am still able to pick out values from A[12], A[13] etc., why is this? Have I dont something wrong? Shouldnt I get an error-message that it is out of the size of the array?



    Thank you very much for reading this. I hope you can help me.
    Last edited by kliro89; 01-26-2011 at 06:09 AM.

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Variable-length arrays are only allowed in C99, you'll need a C99 compatible compiler and to explicitly set it, I think it's --std=c99 for GCC.

    If you don't want to use C99, you should check out malloc()
    malloc - C++ Reference

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > testy.c:7: error: variable-sized object may not be initialized
    The message says it all really, you need to initialise it yourself with a loop.

    > I am still able to pick out values from A[12], A[13] etc., why is this?
    C doesn't provide a safety net against doing dumb things.

    Although some compilers can check an array in scope (and tools like Lint do it better), the problem gets much harder when you pass arrays to functions. At this point, all the size information disappears.

    > Have I dont something wrong?
    Yes, stepping off the end of the array is a bad thing to do.

    > Shouldnt I get an error-message that it is out of the size of the array?
    Using other tools (eg. valgrind), you can find array overrun problems.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    There is something wrong with your slash-key...

    The size of the array (A[x]) is not known at compile time, (x is set at runtime) so the compiler got a problem assigning things to it when compiling. Use...
    Code:
    int main(void) {
    
        int x = 10, A[x];
    
        memset(A, 0, sizeof(int) * x);
    
    }
    ...instead.

    Why would you like pick out values outside the array? If you really wanted to do that, why should you get a error message?
    Last edited by Fader_Berg; 01-26-2011 at 06:45 AM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by kliro89 View Post
    But lets say that the size of the array is not know.
    Call it x, the user will actually give x, then I have to use a code like

    Code:
    #include <stdlib.h>
    
    main(){
    int x;
    x=10;
    int A[x]={0};
    }
    Why do I get this, one must be able to define an array even though you as the programmer don't know the size?
    Generally what you need to do is wait till the user enters the x value before sizing your array...

    On c-99 compliant compilers:
    Code:
    int main (void)   // Note: This is the correct form for main!
      { int x;
         
         puts ("Enter the array size please  : ");
         scanf("%d", &x);
      
         int array[x];
    
         // do stuff
    
        return 0;
       }
    On pre-C99 compilers:
    Code:
    int main (void)
      {  int x;
          int *array;
          puts ("Enter the array size please : ");
          scanf ("%d",&x);
    
          array = calloc(x,sizeof(int));
    
          // do what you need here
    
          free(array);
         
          return 0;  
       }
    It can't allocate memory for the the array until the size is known.

    If I define an Array to be to size 10. I am still able to pick out values from A[12], A[13] etc., why is this?
    Have I dont something wrong? Shouldnt I get an error-message that it is out of the size of the array?
    No you haven't... However; C does not include range checking, so you have to do this by yourself.
    It is up to you as a programmer to always stay within array bounds with your code.
    Last edited by CommonTater; 01-26-2011 at 09:10 AM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Fader_Berg View Post
    There is something wrong with your slash-key...

    The size of the array (A[x]) is not known at compile time, (x is set at runtime) so the compiler got a problem assigning things to it when compiling. Use...
    Code:
    int main(void) {
    
        int x = 10, A[x];
    
        memset(A, 0, sizeof(int) * x);
    
    }
    ...instead.

    Why would you like pick out values outside the array? If you really wanted to do that, why should you get a error message?
    You need to look up memset() .... It doesn't do what you appear to think it does. In fact, what you suggested would probably crash the stack and cause an exception to be thrown.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by CommonTater View Post
    You need to look up memset() .... It doesn't do what you appear to think it does. In fact, what you suggested would probably crash the stack and cause an exception to be thrown.
    What do you think you're complaining about here?

    Quote Originally Posted by man memset
    Name
    memset - fill memory with a constant byte
    Synopsis

    #include <string.h>
    void *memset(void *s, int c, size_t n);

    Description
    The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.
    Return Value
    The memset() function returns a pointer to the memory area s.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Preferably
    memset(A,0,sizeof(A));
    Sure that will set all bits 0 which is OK for int. but not for pointer,float,...


    If I define an Array to be to size 10. I am still able to pick out values from A[12], A[13] etc., why is this? Have I dont something wrong? Shouldnt I get an error-message that it is out of the size of the array?
    Yes, You did do something wrong, No, you won't get any error/warning.
    It's your responsibility to make sure that you access withing array bound.
    Last edited by Bayint Naung; 01-26-2011 at 09:39 AM.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    What do you think you're complaining about here?
    Yes, it fills the memory area with whatever value you tell it to... but it does not allocate memory. Using memset on unallocated memory is a sure recipe for programming disaster...

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by CommonTater View Post
    Yes, it fills the memory area with whatever value you tell it to... but it does not allocate memory. Using memset on unallocated memory is a sure recipe for programming disaster...
    And since the memory was allocated on the previous line, the problem here is what?

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    And since the memory was allocated on the previous line, the problem here is what?
    Well, it would seem the problem here is that I completely missed the int x = 10 part of the previous line....

    Sorry guys... my bad.

  12. #12
    Bit Fiddler
    Join Date
    Sep 2009
    Posts
    79
    Quote Originally Posted by Bayint Naung View Post
    Sure that will set all bits 0 which is OK for int. but not for pointer,float,...
    What makes you think that?

  13. #13
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Use calloc() or malloc() for dynamic memory allocation.With calloc u won't need to use memset() as all the memory location allocated are initialized by 0.Ex:
    Code:
    #include <stdlib.h>
    int main()
    {
        int *p;
        p=(int*)calloc(10,sizeof(int));
    }

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > What makes you think that?
    Question 7.31
    Question 5.17
    Whilst you might get away with it on today's common hardware, it would still remain an assumption to believe that it was guaranteed to always work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Registered User
    Join Date
    Jan 2011
    Posts
    5
    Thanks guys, you have been extremely helpful. I just have three follow-up question. What also seemed to work in my program was that if the size x is unknown, I can still write:
    Code:
    int A[x];
    but not
    Code:
    int A[x]={0};  //this was the problem that you now have helped me with
    ...1...But when I try to use the first one, I think I get some random numbers in the array. It allways gives that yes?

    ....2....Someone mentioned a loop, did you mean to use this then?

    Code:
    #include <stdio.h>
    
    
    int main(){
    
    int x;
    printf("size of array; \n");
    scanf("%i", &x);
    int A[x];
    int i;
    for (i=0;i<x;i=i+1)
    A[i]=0;
    
    }
    ...3...This seems to work, I guess it is just as good as doing the calloc or malloc commands?
    Last edited by kliro89; 01-26-2011 at 05:30 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RS232 connection set up
    By cs342 in forum C Programming
    Replies: 25
    Last Post: 05-26-2010, 03:57 AM
  2. stdio.h?
    By kiros88 in forum C Programming
    Replies: 5
    Last Post: 05-21-2010, 07:09 PM
  3. Pointer within a Struct
    By Bladactania in forum C Programming
    Replies: 11
    Last Post: 04-03-2009, 10:20 PM
  4. size of an integer pointer
    By onebrother in forum C Programming
    Replies: 5
    Last Post: 07-09-2008, 11:49 AM
  5. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM