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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:34:06+00:00 2026-06-12T23:34:06+00:00

I am studying pointers in C/C++ and as my little project to learn this

  • 0

I am studying pointers in C/C++ and as my little project to learn this problematic I am trying to develop a simple application doing operations with matrixies. I want to ask if this way I am using pointers and dynamic allocation in is correct or I am making it wrong. Thanks 🙂

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

int numberRows;
int numberCollumns;
void getSize()
{
   printf("Write down size of matrix in format Rows Collumns: ");
   scanf("%i %i", &numberRows, &numberCollumns);
}

void getMatrix(int *m[])
{
   int x = 0;
   int y = 0;
   while(x<numberRows)
   {
      while(y<numberCollumns)
      {
      scanf("%i", &m[x][y]);
      y++;        
      }
   x++;
   y = 0;
   }
}

void writeMatrix(int *m[])
{
   int x = 0;
   int y = 0;
   while(x<numberRows)
   {
      while(y<numberCollumns)
      {
      printf("%i ", m[x][y]);
      y++;        
      }
   printf("\n");
   x++;
   y = 0;
   }
}

int main()
{
   getSize();
   int **matrix;
   matrix = (int**)malloc(numberRows*sizeof(int*));
   int x = 0;
   while(x < numberCollumns)
   {
      matrix[x] = (int*)malloc(numberCollumns*sizeof(int));
      x++;
   }
   getMatrix(matrix);
   printf("\n\nMatrix: \n");
   writeMatrix(matrix);


   return 0;
}
  • 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-12T23:34:08+00:00Added an answer on June 12, 2026 at 11:34 pm

    I found a problem:

    while(x < numberCollumns)
    {
       matrix[x] = (int*)malloc(numberCollumns*sizeof(int));
       x++;
    }
    

    Your loop condition is wrong. What if the number of rows and columns do not match up?
    Also, I do not seeing any corresponding calls to free.

    As for code style, your while loops would look better as for loops:

    for(int x = 0; x < numberRows; ++x)
    {
        matrix[x] = (int*)malloc(numberCollumns*sizeof(int));
    }
    

    Side Note

    The code is pure C (well except for the casting of the return from malloc). C++ code would look a lot different.

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

Sidebar

Related Questions

I am studying pointers and I encountered this program: #include <stdio.h> void swap(int *,int
After studying the basics of Linux Application Programming, the next step is to learn
Studying MS Exam 70-536 .Net Foundation I've got to Chapter 11 Application Security and
I'm currently self-studying C for mastering an university project. In that project we have
Im studying some ruby code and I see this varx variable being used with
While studying for the SCJP 6 exam, I ran into this question in a
I was studying Ray Tracing on http://www.devmaster.net/articles/raytracing_series/part1.php when I came across this piece of
Studying some sources I often came to see this: (Part of the real source)
I started studying smart pointers of C++11 and I don't see any useful use
Hey guys, I'm studying for a midterm right now and am working on trying

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.