Thread: Writing to a 2D array

  1. #1
    Registered User
    Join Date
    Feb 2009

    Writing to a 2D array

    Hi, I'm slightly stuck. I have got the user to input a value which is then stored at a hard coded index within the 2D array.

    printf("Enter the 1st input value: ");
    	scanf("%d", &array[1][2]);
    After all the input values have been entered, the system applies the requested logic to the values. This results in a single interger output.

    Now, this is where I'm stuck, is there a way of writing that value to an index location in the 2D array that the user specifies.

    ie, where would you like store the value? Then user inputs the row and column index number. Usig scanf() this becomes the location of the value.

    Any help would be great, thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    You probably need to store the value in a temporary variable - unless you know the position, in which case you can of course use the same style of code you already have, but using the index the user provided [after you have validated that it's a valid position of course and not outside of the 2D array].

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    int x, y;
    printf("Enter row: ");
    scanf("%d", &x);
    printf("Enter column: ");
    scanf("%d", &y);
    my2darray[x][y] = myvalue;
    Unless I'm misinterpreting the question.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2009


    Fixed, thankyou for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  2. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM