Problem within multi dimensional array

This is a discussion on Problem within multi dimensional array within the C Programming forums, part of the General Programming Boards category; hey guys i m having bit trouble with multi and int array i m trying to make a program that ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    Problem within multi dimensional array

    hey guys i m having bit trouble with multi and int array i m trying to make a program that you put ur name 3 times then it store it then when u want to output it output all 3
    Code:
    #include <stdio.h>
    int main(void)
    {
    char array[10][3];
    int i;
    for(;;)
    {
    if(++i==3)
    break;
    fputs("Please enter your name: ",stdout);
    fgets(array[i],10,stdin);
    printf("Name of array %d is %s",i,array[i+1]);
    }
    return 0;
    }
    this code crashes
    i made also other one
    Code:
    #include <stdio.h>
    int main(void)
    {
    char array[10][3];
    int i;
    for(i=0;i<=3;i++)
    {
    fputs("Please enter your name: ",stdout);
    fgets(array[i],10,stdin);
    printf("Name of array %d is %s",i,array[i+1]);
    }
    return 0;
    }
    i know how to use arrays for repeadtable tasks like 1 to 10 and doing functions with them
    but if i wanna store
    value in array i dunt quite understand
    thanks alot for your help in advanced
    and merry christmas.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In the first program, you need to initialize i to zero. BTW, this is the standard idiom, which you have almost used in your second program:
    Code:
    int x;
    for(x = 0; x < 3; x ++) {
    
    }
    Then you don't have to fiddle with breaks and so on.

    In your second program, the loop is looping too far; you should use <3 instead of <=3. And you're causing a buffer overrun in your last printf() statement; I suspect you wanted
    Code:
    #include <stdio.h>
    int main(void)
    {
    char array[10][3];
    int i;
    for(i=0;i<3;i++)
    {
    fputs("Please enter your name: ",stdout);
    fgets(array[i],10,stdin);
    printf("Name of array %d is %s",i+1,array[i]);
    }
    return 0;
    }
    where the blue lines are the lines I have changed.

    Remember, when you index an array of N elements, the indices you get are from 0 to N-1, inclusive.

    Finally, consider working on your indentation! cpwiki.sf.net/Indentation
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Second the indentation - it's awful.

    > fgets(array[i],10,stdin);
    But your array is 10 lots of char [3],
    You're treating it as 3 lots of char [10]
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    thanks alot m8 i rlly appropriate it

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    Quote Originally Posted by Salem View Post
    Second the indentation - it's awful.

    > fgets(array[i],10,stdin);
    But your array is 10 lots of char [3],
    You're treating it as 3 lots of char [10]
    i thought if goes to array 3 then break
    stupid me xD

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lolguy View Post
    i thought if goes to array 3 then break
    stupid me xD
    Yes, but you've got the 10 and 3 in your array declarion bass ackwards.

    --
    Mats
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Zero out two dimensional array
    By davo666 in forum C Programming
    Replies: 16
    Last Post: 01-08-2009, 05:28 AM
  2. multi dimensional array at runtime
    By sawer in forum C++ Programming
    Replies: 21
    Last Post: 05-10-2006, 11:59 PM
  3. Multi Dimensional Array Compare
    By guitarist809 in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2006, 06:03 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 03:28 PM

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