Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9196277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:48:57+00:00 2026-06-17T21:48:57+00:00

I am trying to populate a two dimensional array (mapLayout) with chars from a

  • 0

I am trying to populate a two dimensional array (mapLayout) with chars from a text file.

When I use printf to output the chars as they are being read everything looks fine, however the actual line that adds the char to the array seems to be causing a crash.

#include <stdio.h>
#include <stdlib.h>

void createMap();

//height of file being read
int mapHeight, mapWidth = 20;
char mapLayout[20][20];

int main()
{
    createMap();
    return 0;
}

//read in string from file and populate mapLayout with chars
void createMap(){
    FILE *file = fopen("map.txt", "r");
    int col, row = 0;
    int c;

    if (file == NULL)
        return NULL; //could not open file

    while ((c = fgetc(file)) != EOF)
    {
        printf("%c", c);
        printf("\nx:%d, y:%d\n", col, row);

        if(c == '\n'){
            row++;
            col = 0;
        }else{
            mapLayout[col][row] = c;        //<--  This line seems to be the problem
            col++;
        }

    }

    return;
}

The file I am reading is a 20 x 20 representation of a map. Here it is:

xxxxxxxxxxxxxxxxxxxx
xA                 x
x                  x
x                  x
xxxxxxxxxxxxxxxx   x
x                  x
x                  x
x                  x
x                  x
x                  x
x    xxxxxxxxxxxxxxx
x           x      x
x           x      x
x           x      x
x           x      x
x           x      x
x     xxxxxxx      x
x                  x
x                 Bx
xxxxxxxxxxxxxxxxxxxx

Any help would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:48:58+00:00Added an answer on June 17, 2026 at 9:48 pm

    int col, row = 0; why col is not initialized to zero. If the first character in the file is \n then it will not crash, for all remaining cases crash will happen (undefined behaviour).

    Do

    int col = 0;
    int row = 0;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to populate an array with the text value of a json
I am trying to populate an array with a path to a file. <?php
What I am trying to do is populate one listview, from two different tables
Using PHP, I'm trying to populate an HTML list with data from two different
I'm trying to populate a JTree from an array of Objects where A's ObjName
I am trying to populate the list li from the LoginAction on to the
I am trying to populate an ASP MVC3 drop down list with only two
I am trying to use an sqldatasource object to populate a grid view but
Im working with two arrays, trying to work with them like a 2 dimensional
I could use some guidance on specifying a dynamic multi dimensional array in javascript.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.