Thread: User Defined Array

  1. #1
    International Freak
    Join Date
    May 2004
    Posts
    23

    User Defined Array

    How do I set the length of an array using a value from the user?

    Code:
    struct colHeads headers[colNum];
    That dosen't work^.... Any ideas? Thanx.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    5
    I think you have 2 way.
    1) declare the array more bigger than the user will input.
    For example, user will input array from 10 - 100 elements of array, So, declare the array bigger than 100.

    2) Use dynamic array.

    Code:
    #include <stdio.h>
    
    int main()
    {
      int nSIZE;
      double *ptr;
      int i,j;
      while(nSIZE != 0)
        {
          printf("enter size : \n");
          scanf("%d", &nSIZE);
    
                ptr = (double *)malloc(sizeof(double)*nSIZE*nSIZE);
    
          for(i=0;i<nSIZE;i++)
    	{
    	  for(j=0;j<nSIZE;j++)
    	    {
    	      (ptr[i]) = j;
    	      printf("%f", (ptr[i]+j));
    	    }
    	  printf("\n");
    	}
          free(ptr);
        }
    }

    array size will be user input, nSIZE.
    when user input = 0, the program will stop.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Use malloc
    Code:
    struct colHeads *headers = malloc ( colNum * sizeof *headers );
    If you don't do sizeof(headers) or &headers, then the rest of the code remains the same.
    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
    International Freak
    Join Date
    May 2004
    Posts
    23
    well thats cool, but maybe you could explain it just a little more?
    Once a Freak, Always a Freak.
    International Freaks

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always do something fun like...
    Code:
    #include <stdio.h>
    int main( void )
    {
        unsigned int arraysize;
        chat buf[BUFSIZ] = 0;
    
        printf("Enter the array size: ");
        scanf("%u", &arraysize );
    
        sprintf( buf, "gcc -o myprog myprog.c -DARRAYSIZE %u", arraysize );
        /* FAQ link for system specific child process spawning... */
    
        return 0;
    }
    And "myprog.c"...
    Code:
    #include <stdio.h>
    int main( void )
    {
        int foo[ARRAYSIZE];
        ...do stuff here...
    
        return 0;
    }


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    originally posted by Kosong
    Code:
    #include <stdio.h>
    ...
    should be
    Code:
    #include <stdio.h>
    #include <stdlib.h>

    --------------------------take it easy------------------------..................enjoy................

  7. #7
    International Freak
    Join Date
    May 2004
    Posts
    23
    so are those the easiest ways? cause they are still pretty confusing
    Once a Freak, Always a Freak.
    International Freaks

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    What's to be confused about?

    If you have
    struct colHeads headers[200];

    Replace it with
    struct colHeads *headers = malloc ( 200 * sizeof *headers );

    > so are those the easiest ways?
    Or use C++, which allows such things
    So does the new C standard (called C99), but compilers for that are not yet common.
    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.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'd probably go with malloc to allocate your array. Basicly, set up a pointer, call it whatever your array is going to be called. Use malloc to allocate the amount of memory you need. (See Salem's example.) When you're all done, you call free on the pointer to get rid of your "array". Remember, for every malloc call, you'll need a matching free some place.

    [edit]Curses, foiled again.[/edit]

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    International Freak
    Join Date
    May 2004
    Posts
    23
    ok I think that makes sense
    Once a Freak, Always a Freak.
    International Freaks

  11. #11
    International Freak
    Join Date
    May 2004
    Posts
    23

    Error

    Code:
    struct colHeads *headers = malloc( colNum * sizeof *headers );
    This gives me an error:

    Code:
    Cannot initialize 'colHeads near*' with 'void near*'
    Once a Freak, Always a Freak.
    International Freaks

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Lemme guess, you called your source file prog.cpp instead of prog.c
    Which means you're compiling your code as C++ and not as C
    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.

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Are you using a 16-bit compiler?
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  14. #14
    International Freak
    Join Date
    May 2004
    Posts
    23
    I changed it to .c and I still get the same error.
    Once a Freak, Always a Freak.
    International Freaks

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Or cut to the chase, which compiler?

    And a decent sized block of code - like more than the one line which is causing you 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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  2. User defined functions
    By alexpos in forum C Programming
    Replies: 2
    Last Post: 10-23-2005, 02:53 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. class template user defined obj
    By terracota in forum C++ Programming
    Replies: 4
    Last Post: 06-01-2004, 08:14 AM