Re: 2-d array using pointers



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



Relevant Pages

  • Re: A taxonomy of types
    ... I am describing how to represent the representation (and, ... if the system follows static typing rules (such as in a compiler), ... so, the hardware sees pointers and pointer arithmetic, but the compiler ... "Besides the char types, up to three sizes of integer, declared short int, ...
    (comp.lang.misc)
  • Re: Matrix optimization
    ... With unrolling, it seems like the gcc compiler likes the ... time ./matrix_add pointers ... The compiler does a pretty bad job of unrolling index loops! ... template <typename T, int Rows, int Cols> ...
    (comp.lang.cpp)
  • Why cant I initialize member pointers to 0 in class declaration?
    ... I'm fairly new to C++ and I've gathered that uninitialized pointers is a BAD ... So why won't the compiler let me initialize private member pointers ... WpgFrame(const wxString& title, int xpos, int ypos, int width, int ... Obviously it works if I just leave out the zeroes but why does the compiler ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Few Good Interview Questions
    ... Does the compiler generate frame pointers on the stack? ... int x; ...
    (comp.unix.solaris)
  • Re: segmentation fault writing to array elements in structure
    ... I am starting to learn about pointers and would like to understand ... int main ... Use at least 1 space to separate ... Use a C compiler to compile C, ...
    (comp.lang.c)