0
#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 Answers 2

1

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.

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

2 Comments

how am i writing outside the array can you please elaborate further into the code flow .
The array indexes are 0 and 1. Writing to tables[2] is beyond the end of the array.
1

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

2 Comments

i called the function like storeTables(&tables[1], 0, 2); it still showing addresses instead of table of 2 .
@TanishkNagda I can not reproduce. See my appended answer.

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.