Thread: scanf and arrays

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    48

    scanf and arrays

    hi i am trying to get t his to work, but i dont know why it is not working, what is the problem with this?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE 3
    
    int main(int argc, char** argv) {
        
        int board[SIZE][SIZE];
        int i,j;
        
        for(i=0;i<SIZE;i++)
            for(j=0;j<SIZE;j++)
                scanf("%d",&board[i][j]);
        
        return (EXIT_SUCCESS);
    }
    Last edited by yukapuka; 08-23-2012 at 11:08 PM.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    "it is not working" is virtually useless information. What do you expect it to do? What is it actually doing?
    Last edited by oogabooga; 08-23-2012 at 11:16 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    48
    sorry i got it to work
    i just had to use brackets in the second for loop

    and what i was trying to get it to do was to put a character into each array unit

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    In that case, you should be looping to (SIZE-1)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > In that case, you should be looping to (SIZE-1)
    The loops (as written) are fine.

    > and what i was trying to get it to do was to put a character into each array unit
    So exactly what are you typing in?

    If you're typing in say hello, then it isn't going to work - you're reading in integers with the %d format.

    Now if you type in 1 2 3 4 5 6 7 8 9 you should be fine.
    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.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Click_here View Post
    In that case, you should be looping to (SIZE-1)
    That's what the for-loop condition i < SIZE does.

    Everything looks perfectly correct with the code as I see it right now (assuming no further edits after I post this).
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    That's what the for-loop condition i < SIZE does.
    Sorry everyone, I misread the statement

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scanf and Dynamic arrays
    By no_one_knows in forum C++ Programming
    Replies: 4
    Last Post: 06-17-2009, 04:04 PM
  2. arrays and scanf
    By skull_tkl in forum C Programming
    Replies: 1
    Last Post: 11-22-2008, 03:42 PM
  3. Scanf and 2D arrays
    By Killahkode in forum C Programming
    Replies: 2
    Last Post: 09-27-2006, 09:28 PM
  4. scanf error? arrays, structs
    By ericp023 in forum C Programming
    Replies: 1
    Last Post: 04-08-2003, 09:39 PM
  5. Replies: 14
    Last Post: 03-17-2003, 10:07 AM