Help Creating a 2D array

This is a discussion on Help Creating a 2D array within the C Programming forums, part of the General Programming Boards category; Hey guys long time reader first time poster Basically I wanted to know how to take an input from the ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    2

    Help Creating a 2D array

    Hey guys long time reader first time poster


    Basically I wanted to know how to take an input from the command line and use that input to create a 2d array,

    I am not really sure where to start but I can hard code the arrays and I dont have a problem with that but when it comes to argvc or what ever it is I just cant get my head around it and figure this out.

    I am testing it out with a user input and it still doesnt work?

    Code:
    int DIM;
    printf("DIM please: ");
    scanf("%d", &DIM);
    int array[DIM][DIM];
    Just a example of what i am trying to do.

    Apparently using malloc is really the way to go but I dont know how to use malloc instead, I have come here for some help would really appreciate it.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Yes, you need malloc(). Otherwise, just declare 2D array with MAX_SIZE and tell your user that your program only accepts only input less than MAX_SIZE.
    Btw, variable All Caps are only supposed to be for macro/constant...by convention.

    If you want to use malloc(), read through a good tutorial first.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    The issue is that the declaration has to be made by the command line input, so I wont know what the user will put in and it kinda limits the program aswell if I do that.

    Is malloc the only way to go?

    any other paths if not do you know any good basic tutorials that can help me out in this department using malloc.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Chapter 11: Memory Allocation
    Question 6.16
    http://pw1.netcom.com/~tjensen/ptr/pointers.htm

    Edit: If you are using C99, you could use VLA. But better don't use them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 08:39 PM
  2. Comparing a 2d Array (newbie)
    By Cockney in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2001, 12:15 PM
  3. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 09:41 AM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 09:05 AM

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