Thread: This program compiles but crashes at runtime. What could be wrong?

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    5

    Question This program compiles but crashes at runtime. What could be wrong?

    Code:
    ///To Read and output the elements of the array using functions.
    
    
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int i,j;
    
    
    Read (int array[i][j],int size)
    {
        for (i=0;i<4;i++)
            for(j=0;j<4;j++)
        {
            printf("Enter the\t %d,%d\t element of the array\n",i,j);
            scanf("%d",&array[i][j]);
    
    
        }
    }
    Write (int array[i][j],int size)
    {
        for (i=0;i<4;i++)
            for(j=0;j<4;j++)
        {
            printf(" The \t%d,%d\t element of the array is \t%d.",i,j,array[i][j]);
        }
    }
    
    
    
    
    main()
    {
    int arr[4][4];
    
    
    Read(arr[4][4],16);
    Write(arr[4][4],16);
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > This program compiles but crashes at runtime. What could be wrong?
    That you're ignoring errors from your compiler perhaps?
    Code:
    8 : error: array bound is not an integer constant before ']' token
    Read (int array[i][j],int size)
    ^
    8 : error: array bound is not an integer constant before ']' token
    Read (int array[i][j],int size)
    Other issues
    - global variables
    - trying to use variable sized arrays (only valid in C99)
    - ignoring your size parameter
    - typeless functions

    To pass an array to a function, use
    Read(arr,16);
    When you have fixed the declaration of Read (and Write)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    According to what I know you should define functions after the main program, and before main you should prototype them. Rest was mentioned by Salem.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by nerio View Post
    According to what I know you should define functions after the main program, and before main you should prototype them. Rest was mentioned by Salem.
    It's perfectly valid to define functions before "main()" -> link.

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    47
    Since you are using 2d array a practical use could be the prototype C99. What you are doing is just passing 16 while it's useless since you need 4 for i and 4 for j.
    Code:
    void read(int size1,int size2,int array[][size2] // this is the prototype C99.  You can put if you want size1 too, but the size2 is a must

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program compiles, but wrong output
    By mysterymann in forum C Programming
    Replies: 17
    Last Post: 11-30-2011, 10:51 PM
  2. Replies: 7
    Last Post: 12-08-2010, 04:36 PM
  3. My program compiles, but does not run
    By Surfer_Rosa in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:25 AM
  4. Whats wrong with my find Circle Area program?[compiles fine]
    By Golden Bunny in forum C++ Programming
    Replies: 22
    Last Post: 06-16-2002, 02:49 PM
  5. Whats wrong with my file streaming[it compiles fine]
    By Golden Bunny in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2002, 08:43 PM

Tags for this Thread