Re: 2-d array using pointers
- From: Joe Estock <jestock@xxxxxxxxxxxxxxxxxxxxxx>
- Date: Sun, 29 Apr 2007 02:00:08 -0500
Flash Gordon wrote:
Bond wrote, On 28/04/07 12:27:i have written this program of entering 2-d array through
pointers.this shows number of errors.please help me out.
What errors are you being shown? Presumably your compiler is showing you these errors however since you did not specify whether it was your compiler or the binary itself all we can do is presume that they are compile-time errors. Additionally since you did not tell us what errors you are being shown all we can do is scrutinize the code and the logic.
#include<stdio.h>
#include<alloc.h>
No such header in standard C. You should use stdlib.h for the *alloc functions.
void aread(int*[],int*,int*);
void awrite(int*[],int*,int*);
void addarray(int*[],int*[],int*[],int*,int*);
void aread(int*a[],int *m,int *n)
{
int i,j;
printf("Enter array size = ");
scanf("%d%d",*m,*n);
Re-read the section of your textbook describing scanf. You are passing ints where it expects pointers to ints. You also need to check the return value.
Also re-read the C faq with regard to printf. In your call above it is not guaranteed that the output buffer will be flushed due to buffering since you did not include an end of line character '\n'. Either append a newline in your call to printf, or explicitly flush the output buffer with fflush(stdout).
for(i=0;i<*m;i++)
{
*(a+i)=(int*)malloc(sizeof(m+1));
You don't need to cast the value returned by malloc.
for(j=0;j<n;j++)
What type is n?
scanf("%d",(*(a+i)+j));
}
}
void awrite(int*a[],int *m,int *n)
{
int i,j;
printf("Array is\n");
for(i=0;i<*m;i++)
{
for(j=0;j<*n;j++)
printf("%d",*(*(a+i)+j));
printf("\n");
}
}
void addarray(int*a[],int*b[],int*c[],int *m,int *n)
{
int i,j;
for(i=0;i<*m;i++)
{
*(c+i)=(int*)malloc(sizeof(m+1));
for(j=0;j<*n;j++)
*(*(c+i)+j)=*(*(a+i)+j)+*(*(b+i)+j);
}
}
void main()
main returns an int. Always. Incinerate any book that says otherwise.
{
int *a[10],*b[10],*c[10], *m, *n;
Here you are setting aside memory to store a pointer to 10 integers for a, 10 for b, and (more importantly) 10 for c...
aread(a,m,n);
awrite(a,m,n);
aread(b,m,n);
awrite(b,n,n);
addarray(a,b,c,m,n);
....and here you are allocating memory for each element in a, b, and c (including the ones you've already set aside in your declaration). Additionally you've invoked Undefined Behavior (UB) since you never initialized "m" and your addarray function dereferences m to access it's value; which may contain junk or worse; it may appear to work as intended and then one day decide to blow up without warning.
awrite(c,m,n);
Return an int.
}
In fact, you have invoked UB in each of your functions which rely on "m" being initialized unless you are not showing us that part of your code. Judging by the code shown I would highly recommend for you to re-read the section regarding pointers in the K&R manual ("The C Programming Language" second edition, ISBN: 0-13-110362-8) or better still, re-read Chapter 5.
.
- References:
- 2-d array using pointers
- From: Bond
- Re: 2-d array using pointers
- From: Flash Gordon
- 2-d array using pointers
- Prev by Date: long integer multiplication
- Next by Date: Re: long integer multiplication
- Previous by thread: Re: 2-d array using pointers
- Next by thread: dijkstra algorithm
- Index(es):
Relevant Pages
|