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 8541103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:45:14+00:00 2026-06-11T11:45:14+00:00

I am trying to allocate and print a 2d array in C and my

  • 0

I am trying to allocate and print a 2d array in C and my methods seem to work but I get a segmentation fault when printing my array. Any ideas?

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

#define WALL 1;
#define EMPTY 0;

int rows;
int columns;

int startRow, startCol;

void getPuzzleParams();
void printMaze();
int solve(char** puzzle, int, int);
void findStartAndEnd(char** puzzle);

main() {
getPuzzleParams();

char **puzzle;
puzzle = (char **)malloc(sizeof(char *)*rows);
int y;
for(y = 0; y < rows;y++)
    puzzle[y] = (char *)malloc(sizeof(char)*columns);

FILE *fptr;
char c;
char file_name[20];
int i,j;

printf("Type in the name of the file containing the Field\n");
scanf("%s",file_name);
fptr=fopen(file_name,"r");
for (i=0; i<rows; i++)
    for (j=0; j<columns; j++){
        c=fgetc(fptr); 
        while ( !((c == '1')||(c =='0')) ) c=fgetc(fptr);
        puzzle[i][j]=c;
    }
fclose(fptr);

for (i=0; i<rows; i++) {
    for (j=0; j<columns; j++)  {
        if (j == 0) printf("\n");                
        printf("%c  ",puzzle[i][j]);
    }
}
printf("\n");

printf("print");
//printMaze(puzzle);
printf("find");
findStartAndEnd(puzzle);
printMaze(puzzle);
solve(puzzle, 1, 2);
}

void getPuzzleParams() {
printf("Enter the dimensions of the puzzle which need to be between 5 and 100\n");
printf("Enter the desired number of Rows: ");
scanf("%d", &rows); 
printf("Enter the desired number of Columns: ");
scanf("%d", &columns);
printf("Rows: %d, Columns: %d\n", rows, columns);
if(rows > 100 || rows < 5 || columns > 100 || columns < 5) {
    getPuzzleParams();
}

}

void printMaze(char** puzzle) {
int i,j;
for (i=0; i<rows; i++)
    for (j=0; j<columns; j++)  {
        if (j == 0) printf("\n");                
        printf("%c  ",puzzle[i][j]);
    }
printf("\n");
printf("sdfsdF");
}

void findStartAndEnd(char** puzzle) {
int foundEnterence = 0;
int i;
printf("start");
for(i = 0; i < columns; i++) {
    printf("top row");
    if(puzzle[0][i]=='0') {
        if(!foundEnterence) {
            foundEnterence = 1;
            startRow = 0;
            startCol = i;
            puzzle[0][i] = 'S';
        } else {
            puzzle[0][i] = 'G';
        }
    }
}
for(i = 0; i < rows; i++) {
    if(puzzle[i][rows]=='0') {
        if(!foundEnterence) {
            foundEnterence = 1;
            startRow = i;
            startCol = rows;
            puzzle[i][rows] = 'S';
        } else {
            puzzle[i][rows] = 'G';
        }
    }
}
for(i = columns; i >= 0; i--) {
    if(puzzle[rows][i]=='0') {
        if(!foundEnterence) {
            foundEnterence = 1;
            startRow = rows;
            startCol = i;
            puzzle[rows][i] = 'S';
        } else {
            puzzle[rows][i] = 'G';
        }
    }
}
for(i = rows; i >= 0; i++) {
    if(puzzle[i][0]=='0') {
        if(!foundEnterence) {
            foundEnterence = 1;
            startRow = i;
            startCol = 0;
            puzzle[i][0] = 'S';
        } else {
            puzzle[i][0] = 'G';
        }
    }
}
}

int solve(char** puzzle, int x, int y) {
printf("%c",puzzle[x][y]);

}

It may be a problem with an improper use of malloc, but I’ve tried a few different initializations with no success.

Edit: The comand line output is:

Enter the dimensions of the puzzle which need to be between 5 and 100
Enter the desired number of Rows: 12
Enter the desired number of Columns: 10
Rows: 12, Columns: 10
Type in the name of the file containing the Field
maze.txt

1  1  1  1  1  1  1  0  1  1  
1  1  0  0  1  0  0  0  0  1  
1  0  0  0  1  0  0  1  1  1  
1  0  0  0  0  0  0  0  0  1  
1  0  1  0  0  1  0  1  1  0  
1  1  0  0  0  1  0  1  1  1  
1  1  0  1  1  0  0  0  0  1  
1  0  1  0  0  1  0  1  0  1  
1  1  1  0  1  0  1  0  1  1  
1  0  0  1  0  1  0  0  0  1  
1  0  0  0  1  1  1  0  1  1  
1  1  1  1  1  1  1  1  1  1  
Segmentation fault
  • 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-11T11:45:15+00:00Added an answer on June 11, 2026 at 11:45 am

    You’re crashing in code you haven’t pasted. The print completes without error. As you can see, the entire maze has been printed. You aren’t seeing print or find because they’re in the output buffer which you haven’t flushed — you’re crashing before your code gets a chance to flush it.

    Use a good debugger to see where the fault is occurring.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to allocate memory for a array of structures, but after it is
I'm trying to allocate and initialize an array inside a function, but I can't
I am trying to allocate a 2D dimension array of File Descriptors... So I
I am trying to allocate a dynamic string array in the following way and
I'm trying to create a function that would dynamically allocate an array, sets the
I've been trying to get this to work for a good few hours now,
This question is in C++. I am trying to dynamically allocate an array of
I am trying to get the ZGEEV routine in Lapack to work for a
I'm trying to allocate a 2d array in a C program. It works fine
I'm trying to allocate memory for array of pointers to object. ObjectP is a

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.