0

the whole code is okay until last 3 newlines but I want to check if there is a way to write it properly

the full code:

    float mat_add(float A[],float B[],float C[]);
{
     x=0, y=0;

  int row_num,colm_num,chk_row,chk_colm;

do
{
        fflush(stdout);

        printf("PLEASE! choose Array 'A' Rank (maximum 4x4)==>\n\n");
        sleep(1);
        printf(" \a 'A' rows=  ");
        chk_row =scanf("%i", &row_num);


       printf("  'A' columns=  ");
       chk_colm =scanf("%i", &colm_num);

        if( chk_row!=1||chk_colm!=1||row_num>4 || row_num<0|| colm_num>4 || colm_num<0) //restrictions for user input
{
        printf("\n \a Failure!!---->  Your Array Rank is not accepted !!.");
        sleep(1);
        printf("\n \n -----PLEASE!----- re-input your values ==>\n\n");
}

        sleep(2);
}while(  chk_row!=1||chk_colm!=1|| row_num>4 || row_num<0||  colm_num>4 || colm_num<0); //restrictions for user input
x=row_num ;
y=colm_num;
A[]=A[x][y];

printf("\n\n\t \a Success!!---> Array 'A' set to rank = %ix%i i.e. (A[%i][%i]). \n\n",row_num,colm_num,row_num,colm_num);

x,y declared globally!

4
  • 1
    Please, provide a minimal, reproducible example, see stackoverflow.com/help/minimal-reproducible-example. Commented Jan 24, 2021 at 19:02
  • CAN I DO THIS A[ ]=A[2][3] FOR EXAMPLE? THIS IS 80% OF MY QUESTION AND THANK YOU FOR RESPONSE <3 Commented Jan 24, 2021 at 21:50
  • Don't SHOUT — it annoys people. Commented Jan 25, 2021 at 7:31
  • i will do sir ty Commented Jan 26, 2021 at 15:36

1 Answer 1

1

CAN I DO THIS A[ ]=A[2][3] FOR EXAMPLE?

the signature:

float mat_add(float A[],float B[],float C[]);

Passes in pointers to three arrays. So the dimensions to the arrays must already be fixed before the call to this function.

You have not posted the declarations to those arrays so we cannot know (and as written, the code also cannot know) the dimensions to those arrays.

So when the code says:

A[]=A[x][y];

There is no way to know if the indexes X and Y are within the bounds of those arrays.

this part of the expression:

A[]

is taking the address of the array as the destination (L value) and assigning the contents of the entry in the array A[2][3] and assigning that contents to ??? A[0][0] ???

This is NOT a secure assignment. Amongst other problems, what if there are not (at least 3 rows and at least 4 columns in the array)

Also, the compiler does not know how many columns are in each row of the array, so the compiler does not know how far to index through memory to access each row (beyond the 0 row)

The result is undefined behavior

Suggest changing the signature to:

float mat_add( int rows, int columns, float A[ rows ][ columns ], etc ;

then, before using x and y, check that those (input from the user) variables are >0 and for x <rows and for y <columns

Sign up to request clarification or add additional context in comments.

1 Comment

1) yes i already restricted y ,x also before i assign them to array i asked user to assign 'x' and 'y' values , that's y i posted slightly long code sry. 2) also for the issue : arrays must be at fixed sizes and dimensions . so if i declared them in function prototype can i leave the prototype[e sth like this ==> fun(empty)? then declare my arrays when i need them later inside my function body?

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.