0

Here is the code, which fills an array with input numbers and outputs it. All these previous functions are executed with pointers, but the last function, which is supposed to print the numbers in a certain row, has some kind of a bug. It doesn't print the last row of the array, since array become 3x3 sort. What is the problem and how could I fix it? And what can you say about code itself. Is it flex, maintainable and useful or vice versa?

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

int** MAC_ROS(int str,int col);
int* task(int strm,int colm);
int* getNumber(int *p,int colm);
void GET_NUMBER(int *pointer,int col,int rowDestination);
void OUT(int *p,int str,int col);
int *point=NULL;
int arr[20][20];

int main(void){
    int col,str,rowDestination;
    printf("Input columns="); scanf("%d",&col);
    printf("Input strings="); scanf("%d",&str);
    point=task(str,col);
    printf("rowDestination="); scanf("%d",&rowDestination);
    GET_NUMBER(point,col,rowDestination);
}

int* task(int strm,int colm)
{
  int *MAS_POINT;
  MAS_POINT=(int*)MAC_ROS(strm,colm);
  OUT(MAS_POINT,strm,colm);
  return MAS_POINT;
}

int** MAC_ROS(int str,int col)
{
  int a=0;
  int *point,**el;
  point=(int*)malloc(col*str*sizeof(int));
  el=(int**)point;
  for(int i=0;i<str;i++)
  {
    point=point+str*i;
    for(int j=0;j<col;j++)
    {
      scanf("%d",&a);
      *(point+j)=a;
       }
  }
  return el;
}

void OUT(int *p,int str,int col)
{
  for(int i=0;i<str;i++)
  {
    p=p+str*i;
    for(int j=0;j<col;j++)
    {
       printf("%d ",*(p+j));
    }
    printf("\n");
  }
}

void GET_NUMBER(int *pointer,int col,int rowDestination)
{
  pointer=pointer+((rowDestination-1)*col);
  for(int t=0;t<col;t++)
  {
    printf("%d ",*(pointer+t));
  }
}
2
  • All upper-case names like MAS_POINT or GET_NUMBER are certainly allowed in C, but they're seldom used for variables or functions. The names normally indicate a constant or macro, either via #define or enum or similar constructs. There are a few examples where it is used for a type name (FILE * being the best known; in POSIX contexts, DIR * is another example). Generally, other people will find your code easier to read if you reserve all upper-case names for constants or macros. Commented Nov 10, 2018 at 21:49
  • You declare getNumber() but you don't define or use it. You should remove its declaration. It's odd that task is in lower-case but the other defined functions are in upper-case. Commented Nov 10, 2018 at 21:54

1 Answer 1

1

Your code in OUT is:

void OUT(int *p,int str,int col)
{
  for(int i=0;i<str;i++)
  {
    p=p+str*i;
    for(int j=0;j<col;j++)
    {
       printf("%d ",*(p+j));
    }
    printf("\n");
  }
}

To get to the next row, you can either add one row to the previous pointer or you can add an increasing number of rows to the original pointer. You, however, add an increasing number of rows to the original pointer, which won't work.

To get to the next row, you need to add on the number of columns. If the array has 8 columns, then to get to the next row, you need to 8 to the previous row to skip over each of the 8 columns in that row.

Possible fixes:

void OUT(int *op,int str,int col)
{
  for(int i=0;i<str;i++)
  {
    int *p = op + col * i;
    for(int j=0;j<col;j++)
    {
       printf("%d ",*(p+j));
    }
    printf("\n");
  }
}

Or:

void OUT(int *p,int str,int col)
{
  for(int i=0;i<str;i++)
  {
    for(int j=0;j<col;j++)
    {
       printf("%d ",*(p+j));
    }
    printf("\n");
    p=p+col;
  }
}

Or even:

void OUT(int *p,int str,int col)
{
  for(int i=0;i<str;i++)
  {
    for(int j=0;j<col;j++)
    {
       printf("%d ",*p);
       p++;
    }
    printf("\n");
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

There is the problem in function GET_NUMBER not in OUT(sorry about forget to mark it in description). And it still prints 0 in third row of 3x3 array.
@Wage I understand why you think that, but my answer explains why your thinking is incorrect. If every function but one stores things in the wrong place, the problem will appear to be with that one function but it's not.
You mean the trouble is in MAC_ROS, where the memory is allocated? Or I just totally don't understand what are you talking about.
@Wage Yes, precisely. Think about it. Why point=point+str*i; each time? You need to skip one row, yet you add str*i. Neither str nor i is right. Why does how many rows you have matter when you want to skip one row? Why does i matter when you want to skip one row?
If an array has 8 rows and 5 columns, that means each row contains five entries. So you need to add 5 to the pointer to get to the next row. So you should be adding the number of columns to the pointer each time. Yet you add i times the number of rows! To clean this up, you should probably create some macro or function that takes a pointer to the array, the number of columns, the desired row, and the desired column and returns a pointer to the entry. Use that everywhere.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.