Thread: pointers to arrays

  1. #1
    Registered User
    Join Date
    Jan 2010
    Location
    on some of the worst place on earth
    Posts
    105

    pointers to arrays

    Hi friends, This is a program i posted before to calculate the sum of two tables.There is a little change in these codes than the before one.

    I have used pointers to arrays here rather than arrays of pointers as I had done before.The previous program was corrected by using arrays of pointers and then using malloc for assigning the reqd. bytes for the respective arrays.But here I am using pointers to arrays and not using malloc.I compiled the program in Code::Blocks and run it.It runs but in the middle it crashes.So I thought if there was any mistake in my codes.I tried to find mistakes if any in the program but none.But when i tried in Turbo C++ compiler, it was compiled and run quite fine without any problem and correct answer.So I want to know why the same program runs on one compiler and not on other(i.e.,crashes).I also want to know if my program was deficient or not good in using any codes.Please comment..........


    The program is:
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #define COL 20
    void readinput(int (*x)[COL],int,int);//function prototypes
    void computesum(int (*x)[COL],int (*y)[COL],int (*z)[COL],int,int);
    void printout(int (*x)[COL],int,int);
    int main()
    {
    	int row,nor,noc;//nor=no of rows noc=no of columns
    	int (*a)[COL],(*b)[COL],(*c)[COL];//THIS STATEMENT DECLARES THAT a,b,c are pointers to arrays of dimensions 20
    	printf("\nHOW MANY ROWS?  ");
    	scanf("%d",&nor);
    	printf("\nHOW MANY COLUMNS?  ");
    	scanf("%d",&noc);
    	printf("\nREAD INPUT OF FIRST TABLE:\n");
    	readinput(a,nor,noc);//inputting first table(function call)
    	printf("\nREAD INPUT OF SECOND TABLE:");
    	readinput(b,nor,noc);//inputting second table(function call)
    	computesum(a,b,c,nor,noc);//computing the sum of the tables(function call)
    	printf("\nPRINTING OUTPUT\n");
    	printout(c,nor,noc);//showing the result(function call)
    	getch();
    	return 0;
    }
    void readinput(int (*x)[COL],int r,int c)
    {
    	int i,j;
    	for(i=0;i<r;i++)
    	{
    		printf("\nENTER THE ELEMENTS OF THE ROW NO. %d:\n",i+1);
    		for(j=0;j<c;j++)
    		{
    			scanf("%d",(*(x+i)+j));//reading each element of the table
    		}
    	}
    }
    void computesum(int (*x)[COL],int (*y)[COL],int (*z)[COL],int r,int c)
    {
    	int i,j;
    	for(i=0;i<r;i++)
    	{
    		for(j=0;j<c;j++)
    		{
    			*(*(z+i)+j) = *(*(x+i)+j) + *(*(y+i)+j);//adding corresponding elements
    		}
    	}
    }
    void printout(int (*x)[COL],int r,int c)
    {
    	int i,j;
    	for(i=0;i<r;i++)
    	{
    		for(j=0;j<c;j++)
    		{
    			printf("%-5d",*(*(x+i)+j));//printing out the numbers
    			if(j==c-1)
    			printf("\n");
    		}
    	}
    }
    Thanks......

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > So I want to know why the same program runs on one compiler and not on other(i.e.,crashes)
    Pure dumb luck on your part.

    The code is broken, since you don't allocate any space for you arrays.
    The fact that one compiler fails to crash just makes you lucky, not good.

    If you increased the size of the arrays, you'll probably find your luck running out.

    Before you call any of the functions, you need (for all your pointers)
    Code:
    a = malloc ( nor * sizeof *a );
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with returning arrays using pointers
    By cuba06 in forum C Programming
    Replies: 9
    Last Post: 11-23-2007, 10:40 AM
  2. Array of Pointers to Arrays
    By Biozero in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 02:31 PM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  4. pointers and arrays..
    By ahming in forum C Programming
    Replies: 1
    Last Post: 04-24-2004, 03:12 AM
  5. Stack functions as arrays instead of node pointers
    By sballew in forum C Programming
    Replies: 8
    Last Post: 12-04-2001, 11:13 AM