Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

c - 1D array ----> 2D or multi-dimension array code

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)==>

");
        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("
 a Failure!!---->  Your Array Rank is not accepted !!.");
        sleep(1);
        printf("
 
 -----PLEASE!----- re-input your values ==>

");
}

        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("

 a Success!!---> Array 'A' set to rank = %ix%i i.e. (A[%i][%i]). 

",row_num,colm_num,row_num,colm_num);

x,y declared globally!

question from:https://stackoverflow.com/questions/65874514/1d-array-2d-or-multi-dimension-array-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...