Help with allocation of 2D dynamic array with unknown size

This is a discussion on Help with allocation of 2D dynamic array with unknown size within the C Programming forums, part of the General Programming Boards category; Hello, I have problem with allocating 2D dynamic array with unknown size. Firstly I would like to create a 2D ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    6

    Question Help with allocation of 2D dynamic array with unknown size

    Hello,

    I have problem with allocating 2D dynamic array with unknown size. Firstly I would like to create a 2D dynamic array, but the size I will get after the user's input.

    I heard about reallocating a dynamic array, but it didn't work to me.. :/

    For example, input should be like this:

    helloworld
    helloworld
    helloworld

    - so I need the array of size 3x10, but how can I do it, before the know of this size? :/


    Please help, I'm starting be desperate..

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,421
    I heard about reallocating a dynamic array, but it didn't work to me...
    Welcome to the forum.

    Show us what you have tried
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    Hello,

    so, firstly I made:

    Code:
    char *arr;
    printf ("enter input");
    here some for {
    here sec. for {
    scanf("%c", &arr[raw][col]);
    }
    }
    but I dont know the size of raw and col and I don't know how to find out.. I think that is the main problem..

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,421
    How much experience have you had with C? Have you learnt about malloc/free yet?
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    I have it as subjects at school. But this is something new for me and I have problem with understand this problematics.. So I'm amateur at this time.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    And I also heard about malloc and free function. I know I have to use them in this case.

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,421
    OK, so...

    What you need to do is research dynamic allocation of 2D array in C language


    Basically, you allocate the rows first and then for each row you allocate the columns using a for loop

    To free you free all the columns in each row first, and then you free each row.

    For a look at how it is written, see Dynamic allocation of 2D arrays - C - Source Code | DreamInCode.net
    Note that in this example they use casts when using the malloc ( the (int **) and (int *) before the malloc) - Delete them, as it is technically wrong to do that in C.

    [edit]
    After reading grumpy's post you should note that "technically wrong" is not the correct wording -> It should have said "unnecessary".
    [/edit]
    Last edited by Click_here; 11-29-2012 at 07:14 PM.
    Fact - Beethoven wrote his first symphony in C

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,337
    Quote Originally Posted by Click_here View Post
    Note that in this example they use casts when using the malloc ( the (int **) and (int *) before the malloc) - Delete them, as it is technically wrong to do that in C.
    It is not technically wrong to do that in C. It is technically unnecessary, and subjectively a lot of people consider it to be poor style in C.

    If it is technically necessary to cast the return of malloc(), then it's a sign of a programming error (eg forgetting to #include <stdlib.h>) or using a C++ compiler to compile C code.
    Right 98% of the time, and don't care about the other 3%.

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    Hello,

    finally a solve this problem.. Thank you for helping. Anyway now I have problem when I want to protect the input of bad character like ?!, and numbers. So I want to have on the input only the abcdefgh... etc. and "." (dot). So another characters are forbidden..

    Is here some option to do it except manual writing of all avaible characters?

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Check each input character to make sure it's in the alphabet like this: if (input >= 'a' && input <= 'z') ... it's OK. Or you can use if (isalpha(input))

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    6
    Ok, I got it. Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unknown size array
    By archriku in forum C Programming
    Replies: 14
    Last Post: 05-07-2009, 11:29 PM
  2. Allocating an array of unknown size?
    By scarlet00014 in forum C Programming
    Replies: 2
    Last Post: 09-27-2008, 09:53 AM
  3. Array size at run time unknown
    By lechat in forum C++ Programming
    Replies: 4
    Last Post: 11-15-2007, 11:25 AM
  4. Find Size Of Dynamic Memory Allocation?
    By appleGuy in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2007, 09:34 AM
  5. array of unknown size
    By richdb in forum C Programming
    Replies: 7
    Last Post: 02-25-2006, 10:48 AM

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