#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    26
    Rep Power
    0

    Help Printing An Array


    I am making a program where you enter forward/back/up/down etc
    And the changes happen in the array
    When i call my funtion that prints out the array it doesnt print
    It out Correctly it should print like t @
    here is how it prints out t

    @

    Here is the code p.s this is not the full program its just the part where the error occurs

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include <conio.h>
    #include "Func.h"
    char Map[8][8] =
    {
        '\0','\0','\0','\0','\0','\0','\0',
        '\0','\0','\0','\0','\0','\0','\0',
        '\0','\0','\0','\0','\0','\0','\0',
        't','\0','\0','\0','\0','\0','@',
        '\0','\0','\0','\0','\0','\0','\0',
        '\0','\0','\0','\0','\0','\0','\0',
        '\0','\0','\0','\0','\0','\0','\0'
    };
    int Y=3,X=7;
    void Forward(int x);
    void PutMap(void);
    void help(void);
    int main()
    {
        cls();  //Defined in Func.h  It CLears the screen
        PutMap();
    }
    void PutMap(void)
    {
        int a;
        for(a=0;a<7;a++)
        {
            printf("\t%c %c %c %c %c %c %c %c\n",Map[a][0],Map[a][1],Map[a][2],Map[a][3],Map[a][4],Map[a][5],Map[a][6],Map[a][7]);
        }
    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,444
    Rep Power
    1872
    Well you declare an [8][8] array, but the for loop sees [7][8] and the initialiser treats it as a [7][7] array.

    Try something like
    Code:
    char Map[8][8] =
    {
        { 0 },
        { 0 },
        { 0 },
        { 't','\0','\0','\0','\0','\0','@' },
        { 0 },
        { 0 },
        { 0 },
        { 0 },
    };
    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

IMN logo majestic logo threadwatch logo seochat tools logo