calling an array from mid in a multidimensional array

50 views Asked by At
#include<stdio.h>

void storeTables(int arr[][10] , int n ,int number);

int main() {
    int tables[2][10];
    storeTables(&tables[1],1,2);
    storeTables(tables,0,3);

    for(int i = 0 ; i<10 ; i++){
        printf("%d \t",(tables[0][i]));
    }
    printf("\n");

    for(int i = 0 ; i<10 ; i++){
        printf("%d \t",tables[1][i]);
    }
   return 0 ;
}

void storeTables(int arr[][10] , int n ,int number){
    for(int i = 0 ; i<10 ; i++){
        arr[n][i] = number * (i+1) ;
    }
}     

look at this code . in storeTable when i write storeTables(tables,0,2); it gives desired result . if i write storeTables(&tables[0],0,2); it still gives desired result . but when i write storeTables(&tables[1],1,2); it gives random addresses as a result . which is probablly wrong . passing &tables[1] just means passing 2nd row of 2d array . what's the problem in doing so . why the answer is coming wrong ?

i tried asking to chatGPT but its not understanding the errror . i am expecting a table of 2 and 3 as a result . if i pass pointer to 2d array i'll get the result . if i pass pointer to an array of 10 integers . i get the result but i pass pointer to 2nd array of 10 integers first my result becomes wrong start printing addresses . also things like &tables[0][1] or tables + 1 will not going to work as i have tried them all , also i have tried writing int(*arr)[10] instead of int arr[][10] in the storeTables function but i don't want use pointer do it by using only int arr[][10] function . please help me .`

2

There are 2 answers

2
Barmar On

You're writing outside the array when you do that. When you then use arr[n] in the function, n is used as an index starting from tables[1]. Since n==1, this refers to tables[2], which doesn't exist. Accessing outside an array causes undefined behavior.

2
Vlad from Moscow On

This function declaration

void storeTables(int arr[][10] , int n ,int number);

is equivalent to

void storeTables(int ( *arr )[10] , int n ,int number);

For the both calls

storeTables(tables,0,2);

and

storeTables(&tables[0],0,2);

due to the pointer arithmetic within the function the expression arr[n] that is evaluated like *( arr + n ) that is like *( arr + 0 ) you get the first "row" of the original array.

For this call

storeTables(&tables[1],1,2);

that is equivalent to

storeTables( &*( tables + 1 ),1,2);

that in turn is equivalent to

storeTables( tables + 1,1,2);

you have within the function that the expression arr[n] is equivalent to *( tables + 1 + n ) that yields *( tables + 2 ). That is the expression tryes to access memory outside the original array.

You need to call the function like

storeTables(&tables[1], 0, 2);

or like

storeTables( tables, 1, 2);

Here is your updated program.

#include <stdio.h>

void storeTables( int arr[][10], int n, int number )
{
    for ( int i = 0; i < 10; i++ )
    {
        arr[n][i] = number * ( i + 1 );
    }
}

int main( void )
{
    int tables[2][10];

    storeTables( &tables[1], 0, 2 );
    storeTables( tables, 0, 3 );

    for (int i = 0; i < 10; i++)
    {
        printf( "%d \t", ( tables[0][i] ) );
    }
    printf( "\n" );

    for (int i = 0; i < 10; i++)
    {
        printf( "%d \t", tables[1][i] );
    }
}

The program output is

3       6       9       12      15      18      21      24      27      30
2       4       6       8       10      12      14      16      18      20