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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:42:15+00:00 2026-06-02T05:42:15+00:00

I’m facing segmentation fault problem when trying to run this code: #include<stdio.h> #include<stdlib.h> int

  • 0

I’m facing segmentation fault problem when trying to run this code:

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


int mallocMatrix (int ***matrix, int A, int B) {
    int i;
    *matrix=(int**)malloc(sizeof(int*)*B);
        for(i=0;i<A;i++)
            (*matrix)[i]=(int*)malloc(sizeof(int)*A);
};

void fillMatrix (int ***matrix, int A, int B) {
    int i, j;
    for(i=0;i<B;i++)
    {
        for(j=0;j<A;j++)
            {
                 printf("Matrix[%d][%d]: ",i+1,j+1);
                 scanf("%d", &(*matrix)[i][j]);
            }
        }
};

void displayMatrix (int ***matrix, int A, int B) {
    int i, j;
    printf ("\n");
    for(i=0;i<B;i++)
    {
        for(j=0;j<A;j++)
        {
            printf ("%d ", (*matrix)[i][j]);
        }
    printf ("\n");
    }
};

int main(){
    int **matrix;
    int **matrix_two;
    int **matrix_three;
    int a, b, c, d;

    printf ("[1st matrix] number of columns:");
    scanf ("%d", &a);
    printf ("[1st matrix] number of rows:");
    scanf ("%d", &b);

        mallocMatrix (&matrix, a, b);
        fillMatrix (&matrix, a, b);

    printf ("[2nd matrix] number of columns:");
    scanf ("%d", &c);
    printf ("[2nd matrix] number of rows:");
    scanf ("%d", &d);

        if (a==d)
        {
            mallocMatrix (&matrix_two, c, d);
            fillMatrix (&matrix_two, c, d);
        }
        else
        {
            printf ("The number of columns of the 1st. matrix must be equal to the number of rows of the 2nd. matrix");
            return -1;
        }

    displayMatrix (&matrix, a, b);
    displayMatrix (&matrix_two, c, d);

    mallocMatrix (&matrix_three, b, c);

    /* multiplication */

    int i, j, k;
    for(i = 0; i < b; i++)
    {
        for(j = 0; j < c; j++)
        {
            for(k = 0; k < b; k++)
            {
                matrix_three[i][j] = matrix_three[i][j] + matrix[i][k] * matrix_two[k][j];
            }
        }
    }

    /* end of multiplication */

    displayMatrix (&matrix_three, b, c);
}

Problem pops up after code signed /multiplication/. For example:
– first matrix is 2×2,
– second 3×2.
I’m expecting result as 3×2 matrix, BUT code gives me 2×2 matrix and segmentation fault when trying to reach third column. Please, point error

  • 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-02T05:42:19+00:00Added an answer on June 2, 2026 at 5:42 am

    I think i found your mistake in mallocMatrix:

     for(i=0;i<A;i++)
                (*matrix)[i]=(int*)malloc(sizeof(int)*A);
    

    The loop condition is wrong you said that B is row count, so for every rouw you must alloc an array of A elements so, the loop should be:

    for(i=0;i<B;i++)
                    (*matrix)[i]=(int*)malloc(sizeof(int)*A);
    

    so when you try 3×2 the third row is never initialized because your loop just iterates 2 times, and when you try to fill the last row crashes

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.