Dynamic array?

This is a discussion on Dynamic array? within the C Programming forums, part of the General Programming Boards category; I have to use a dynamic array and don't have a clue how to start this. The array is empty ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    93

    Dynamic array?

    I have to use a dynamic array and don't have a clue how to start this. The array is empty unless the user decides to input a number in it. I was thinking this but I am not sure of this will will work.

    Code:
    #include <stdio.h>
    
    int main ( void )
    
    int SIZE = 0, a [ ], menu;
    
    
    printf("Press one if you would like to add an intenger\n");
    scanf("%d",&menu);
    
    if ( menu == 1 ) {
    SIZE++
    scanf("%d",&int[SIZE]
    
    )
    printf("What number would you like to delete\n");
    scanf("%d",&key);
    call linear search function
    delete key; ( not sure how to do that )
    call copy function to copy array a;

    Any ideas are greatly appreciated.

    Thanks.
    Last edited by Salem; 06-30-2008 at 12:27 PM. Reason: fix code tags, for all the good it did.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    Code:
    int *a = malloc ( howManyYouWant * sizeof *a );
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    int * a ? is my array
    how many you want ? is the elements

    elements times index times the array. Haven't gotten into pointers yet so I am not sure if
    *a is your pointer or what?

    Is malloc a function out in some C library or did you just make it up?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,307
    Haven't gotten into pointers yet
    Start learning about pointers now.

    Is malloc a function out in some C library or did you just make it up?
    It is from <stdlib.h>. You should use free(), also from <stdlib.h> when you are done, e.g., free(a).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Well, if you want a "dynamic array" (there is no real such thing in C natively), then yeah you need to learn pointers. Replace howManyYouWant with a variable or a const number that indicates how many elements should be in your "array".

    For most cases, you can then treat "a" as an array (although it is technically not an array).

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    I am doing my own homework I am just asking questions.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    The picture in all my posts is just simply a forum signature and meant for the kids that cause trouble here. It's not directed at you. Relax. You're doing fine. We'll make sure you know if you ask a stupid question.

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    93
    If a pointer points at an address and is an address how does that help me redefine the array of [ size + 1 ] elements or use my key to search for the intenger the user wishes to delete and redefine the array to [ size - 1 ] elements.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    To resize the block of memory that malloc() returns, you can use realloc().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2005, 12:13 AM
  2. need help with dynamic array syntax
    By soldyne in forum C Programming
    Replies: 3
    Last Post: 10-11-2005, 02:59 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  4. 2D dynamic array problem
    By scsullivan in forum C Programming
    Replies: 3
    Last Post: 12-30-2002, 10:02 PM
  5. Dynamic array allocation and reallocation
    By purple in forum C Programming
    Replies: 13
    Last Post: 08-01-2002, 12:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21