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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:53:16+00:00 2026-05-23T06:53:16+00:00

Trying to write a matrix-multiplying function for arbitrary-sized matrices in C. I’m trying the

  • 0

Trying to write a matrix-multiplying function for arbitrary-sized matrices in C. I’m trying the following for my function signature:

void matrixMult(void *A, int Xa, int Ya, void *B, int Xb, int Yb);

(it’s returning void for now while I troubleshoot it, I’ll make a proper return later once I have it working the way I want it to)

The X and Y parameters are intended to inform the function of the dimensions of the incoming arrays (from my understanding, that’s necessary, since in C arrays don’t know their own size). So I’m passing in two void pointers A and B, as well as their dimensions.

My question: Once I’m in the function, how do I go about casting the void pointers back to int arrays so I can read them? I tried the following:

(int)*A[someX][someY]

, but I get a compiler error about “invalid use of void pointer”.


EDIT

The following is my full function for now (updating as I’m troubleshooting it):

#pragma warning(disable: 8057)
#include <stdio.h>

void matrixMult(int **A, int Xa, int Ya, int **B, int Xb, int Yb);

int main(int argc, char *argv[]) {
    //   [x][y] x--> row; y-->cols
    int a[3][5] = {
          {  1,  2,  3,  4,  5 }
        , { 10, 20, 30, 40, 50 }
        , {  4,  8, 15, 16, 23 }
    };
    int b[5][7] = {
          {  1,  2,  3,  4,  5,  6,  7 }
        , { 10, 20, 30, 40, 50, 60, 70 }
        , {  4,  8, 15, 16, 23, 42,  0 }
        , {  1,  2,  3,  4,  5,  6,  7 }
        , { 10, 20, 30, 40, 50, 60, 70 }
    };

    matrixMult((int **)a, 3, 5, (int **)b, 5, 7);

    printf("done\n");
    return 0;
}

void matrixMult(int **A, int Xa, int Ya, int **B, int Xb, int Yb) {
    printf("starting matrix mult\n");
    int i, j;

    for (i=0; i<Xa; i++) {
        for (j=0; j<Ya; j++) {
            //printf("%i, %i:\t", i, j);
            printf("%i\t", A[Ya*i+j]);
        }
        printf("\n");
    }
}

EDIT 2 Thanks for the tips and resources guys! The version posted above works as I expect it to now, but I’m afraid it’s an ugly bastardization of how pointers are SUPPOSED to be used. For now I’m off to do some more reading on pointers (thanks cnicutar!), and I’ll revisit this later. Any further criticism of my latest version is appreciated, but I’m marking this answered and moving on to do some further reading for now. Thanks again!

  • 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-23T06:53:17+00:00Added an answer on May 23, 2026 at 6:53 am

    If I understand you question correctly you could do something like

    int **a = A;
    

    Why are you using void * and not int **A straight away ?

    EDIT 1

    In light of comment below: see this FAQ entry (f3 is what you want). And also this one.

    Edit 2

    Declare and initialize them (something) like this:

    int **a;
    
    a = malloc(sizeof(*a) * 3);
    for (i = 0; i < 3; i++) {
        a[i] = malloc(sizeof(int) * 5);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program with a function which returns a matrix with
I keep getting the following error when trying to write a template function: main.cpp|17|error:
I am trying to write a trivial Matrix class, using C++ templates in an
currently I am trying to write to an 20x4 LCD Dot-Matrix Module using Perle's
Im trying to write a function for adding category: function addCategory() { $cname =
I'm trying to write a regex function that will identify and replace a single
I am trying to write a simple matrixMultiplication application that multiplies two square matrices
I'm trying to write a program in Java that when given an MxN matrix
I'm trying to write a matrix into a text file. In order to do
I am trying to write a function that produces a single solution to an

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.