Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7001011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:44:52+00:00 2026-05-27T20:44:52+00:00

#include <stdio.h> #include <stdlib.h> #include <time.h> #define length 100 void print_array(); int main() {

  • 0
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define length 100

void print_array();

int main()
{
    int m,n,i,j;
    int A[length][length];
    printf("Give dimensions of array (up to 100x100):\ni:\n");
    scanf("%d",&i);
    printf("j:\n");
    scanf("%d",&j);
    srand(time(NULL));
    for (m=0;m<i;m++)
    {
        for (n=0;n<j;n++)
        {
            A[m][n]=rand()%45+1;
            printf("A[%d,%d]=%d\n",m,n,A[m][n]);
        }
    }
    print_array(i,j,A);
    return 0;
}

void print_array(int i,int j,int A[][j])
{
    printf("\n");
    int m,n;
    for (m=0;m<i;m++)
        {
            for (n=0;n<j;n++)
            {
                printf("A[%d,%d]=%d\n",m,n,A[m][n]);
            }
        }
}

Hello. I am trying to print a 2d array by calling a function print but when I run the program I get:

For the first printf() the correct values:

A[0,0]=25
A[0,1]=19
A[0,2]=13
A[1,0]=4
A[1,1]=17
A[1,2]=43
A[2,0]=7
A[2,1]=37
A[2,2]=20

But when with the 2nd printf() within the function call of print_array I get:

A[0,0]=25
A[0,1]=19
A[0,2]=13
A[1,0]=0
A[1,1]=0
A[1,2]=0
A[2,0]=0
A[2,1]=0
A[2,2]=0

Seems like I miss something with pointers… Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:44:53+00:00Added an answer on May 27, 2026 at 8:44 pm

    This is C99, right?

    The problem is that you’re confusing the array size.

    The main program has int A[length][length], but then you call the function with a dynamic size for the final dimension, A[][j]. If j != length, then the function will index the array incorrectly.

    I would recommend representing the array in the function call as a bare pointer to the first element, and doing the indexing manually:

    void print_array(const int *A, size_t width, size_t height)
    {
      for(size_t i = 0; i < height; ++i)
      {
        for(size_t j = 0; j < width; ++j)
          printf("A[%zu][%zu] = %d\n", i, j, A[i * width + j]);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> #include <stdlib.h> #include <time.h> void initDeck (int deck[]); void showDeck (int deck[]);
#include<stdio.h> #include<signal.h> #include<stdlib.h> void handler(int signo) { printf(First statement); system(date); exit(EXIT_SUCCESS); } int main()
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
My C code #include <stdio.h> #include <stdlib.h> #include help.h int test(int x, P *ut)
Test the following code: #include <stdio.h> #include <stdlib.h> main() { const char *yytext=0; const
I tried to compile this example: #include <stdio.h> #include <stdlib.h> #include <stddef.h> main(){ size_t
Take the following snippet: 1 #include <stdio.h> 2 #include <stdlib.h> 3 int foo(char [6]);
I wrote this code: #include <stdio.h> #include <stdlib.h> #include <time.h> #include <sys/types.h> #include <sys/shm.h>
test.c: #include <stdint.h> #include <stdio.h> #include <stdlib.h> #define MAXFRAGMENTS 4000000 typedef struct { uint64_t

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.