473,385 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

2-d array using pointers

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);
}

Apr 28 '07 #1
2 3111
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.
}
--
Flash Gordon
Apr 28 '07 #2
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.
Apr 29 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Isaac | last post by:
Hi mates I want to know a simple program of return array from function ? Do I need to use pointer to return the address of the first element in an array. Isaac
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
2
by: Simon Morgan | last post by:
I hope this isn't OT, I looked for a newsgroup dealing purely with algorithms but none were to be found and seeing as I'm trying to implement this in C I thought this would be the best place. I...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
5
by: nembo kid | last post by:
In the following function, s shouldn't be a pointer costant (array's name)? So why it is legal its increment? Thanks in advance. /* Code starts here */ void chartobyte (char *s) { while...
17
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.