Thread: Noob question (redeclaring a array)

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    15

    Question Noob question (redeclaring a array)

    My question is same as my topic how to i rediclare a array ....

    thx for the future help

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    If you have not statically declared the array, you'll be able to realloc() the pointer.

    See the malloc(), realloc(), and free() man pages.

  3. #3
    Registered User S0n1C's Avatar
    Join Date
    Oct 2006
    Posts
    12
    It seems that you don't know much about arrays to begin with, so somebody trying to explain it to you for the first time would take a while. I suggest that you visit: http://www.cprogramming.com/tutorial/c/lesson8.html
    http://www.exforsys.com/content/view/1995/364/

    The first link just touches arrays, but the second link should explain things a little better for you. After reading these post back with any questions that you may have.

    Good Luck
    S0n1C!

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    I know what a array is because I am good at vb.net in that I can just say redim nameofarray(size) I just don't know how to do it in c I will read the tutorial thx

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Demon.killer
    I know what a array is because I am good at vb.net in that I can just say redim nameofarray(size) I just don't know how to do it in c
    Well, you can't. Perhaps that is the reason you got the last reply.
    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.*

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    "Well, you can't. Perhaps that is the reason you got the last reply."

    ?????

    What u mean i guess what I ment was resize a array (redeclaring a array)

    I read the tutorial it doesn't give me what I want

    any help would be better :lol:

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Demon.killer
    What u mean i guess what I ment was resize a array (redeclaring a array)
    You can't resize or redeclare a static array. You need to use dynamic allocation.
    Quote Originally Posted by Kennedy
    If you have not statically declared the array, you'll be able to realloc() the pointer.

    See the malloc(), realloc(), and free() man pages.
    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.*

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    15
    ohhhhh okay thx

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    This one way.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       int *array; /* the "array" */
       int size = 50; /* its size */
       /*
        * Dynamically allocate the array.
        */
       array = malloc(size * sizeof *array);
       if ( array != NULL )
       {
          int *temp;  /* for use with resizing */
          /*
           * Do stuff.
           */
          array[size - 1] = size;
          printf("array[%d] = %d\n", size - 1, array[size - 1]);
    
          /*
           * Resize the "array".
           */
          size = 100;
          temp = realloc(array, size * sizeof *array);
          if ( temp != NULL )
          {
             array = temp; /* successfully changed size */
          }
          else
          {
             free(array); /* failed - free original memory */
             /* exit or whatever (don't accidentally free it a second time later on) */
          }
    
          /*
           * Do stuff.
           */
          array[size - 1] = size;
          printf("array[%d] = %d\n", size - 1, array[size - 1]);
    
          /*
           * Clean up.
           */
          free(array);
       }
       return 0;
    }
    
    /* my output
    array[49] = 50
    array[99] = 100
    */
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Array of Structs question
    By WaterNut in forum C++ Programming
    Replies: 10
    Last Post: 07-02-2004, 02:58 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM