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

  • Home
  • SEARCH
  • 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 6645849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:20:24+00:00 2026-05-26T00:20:24+00:00

I have this part of code: for(i = 0; i < matrix->y; i++) {

  • 0

I have this part of code:

for(i = 0; i < matrix->y; i++)
{
  printf("3.%i - %i %i\n", i, matrix->y, matrix->x);//write correct "3.0 - 3 4"
  for(j = 0; j < matrix->x; j++)
  {
    printf("N - %i %i\n", matrix->y, matrix->x);//never write anything
    fscanf(input, "%i", &grid[i][j]);
  }
  printf("3.%i - %i %i\n", i, matrix->y, matrix->x);//write wrong "3.0 - 3 0"
}

first printf outputs y=3 and x=4, but it never goes inside loop, it never reach the second printf inside for-loop. When I write the same printf from the first line after for-loop, it tels me y=3 and x=0.
Where I have done mistake.

Thanks.

Edit

Code is writen as it is. No lines skipped.

Matrix definition

typedef struct matrix
{
  int x;
  int y;
  int ** grid;
} Matrix;

i and j is defined by

int i, j;

Whole function

Matrix * loadMatrix(char * filename)
{
  Matrix * matrix;
  FILE * input;
  input = fopen(filename, "r");
  if (input == NULL)
    printError (ERR_READFILE);
  else
  {
    fscanf(input, "%i", &(matrix->y));
    fscanf(input, "%i", &(matrix->x));
    int i, j;
    int grid[matrix->y][matrix->x];
    matrix->grid = grid;
    printf("2 - %i %i\n", matrix->y, matrix->x);
    for(i = 0; i < matrix->y; i++)
    {
      printf("3.%i - %i %i\n", i, matrix->y, matrix->x);
      for(j = 0; j < matrix->x; j++)
    {
    printf("N - %i %i\n", matrix->y, matrix->x);
    fscanf(input, "%i", &grid[i][j]);
    }
    printf("3.%i - %i %i\n", i, matrix->y, matrix->x);
    fclose(input);
  }
  return matrix;
}
  • 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-05-26T00:20:25+00:00Added an answer on May 26, 2026 at 12:20 am

    Your program’s behavior is undefined because you’re not allocating storage for matrix. So anything could happen.

    Add this somewhere after you have declared matrix (just before the fscanf lines seems optimal):

    matrix = malloc(sizeof(Matrix));
    

    and change the line where you declare matrix to:

    Matrix *matrix = NULL;
    

    (otherwise you’ll return garbage if the fopen fails.)

    Second problem is that you’re then also failing to allocate proper storage for matrix->grid. You can’t keep a pointer to a local variable after the function has returned.

    You’ll need to create that storage with malloc or calloc too. See this question: How do I work with dynamic mutli-dimensional arrays in C for an example of how to do that.

    You’ll also need to take care to free both of those once you’re finished using them.

    Don’t forget to #include <stdlib.h>, which is required for malloc and free.

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

Sidebar

Related Questions

I have this code to run a slideshow (this is only part of what
We have this anonymous function in our code, which is part of the jQuery's
I have this part of code that was compiling using ARMASM : /* Software
I have a problem with query in C#. I have this part of code
I have this part of a code for editing cue sheets and I don't
I have this part of code: def create @post = Post.find(params[:post_id]) @comment = @post.comments.create(params[:comment])
I have this part of code that takes a file and puts it in
i have this part of a code and i cant understund why the second
I have this part of code: ->filter('username', 'Security::xss_clean') ->filter('url', 'Security::xss_clean') ->filter('text', 'Security::xss_clean') but it
I have this part of code: NSDate *date =nil; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]

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.