Thread: Dynamic array?

  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 11:27 AM. 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
    39,660
    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.

  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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,210
    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,210
    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,210
    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, 01:59 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04: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, 11:48 AM