2-d array using pointers

B

Bond

i have written this program of entering 2-d array through
pointers.this shows number of errors.please help me out.
#include<stdio.h>
#include<alloc.h>
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);
for(i=0;i<*m;i++)
{
*(a+i)=(int*)malloc(sizeof(m+1));
for(j=0;j<n;j++)
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()
{
int *a[10],*b[10],*c[10], *m, *n;
aread(a,m,n);
awrite(a,m,n);
aread(b,m,n);
awrite(b,n,n);
addarray(a,b,c,m,n);
awrite(c,m,n);
}
 
F

Flash Gordon

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.
#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.
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;
aread(a,m,n);
awrite(a,m,n);
aread(b,m,n);
awrite(b,n,n);
addarray(a,b,c,m,n);
awrite(c,m,n);

Return an int.
 
J

Joe Estock

Flash said:
Bond wrote, On 28/04/07 12:27:

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...

....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.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,787
Messages
2,569,627
Members
45,328
Latest member
66Teonna9

Latest Threads

Top