• C

passing an array to a function

I want to calculate the sum of each row on a 2 dimension array. Also, I need to make it as a function using pointer as argument.But, the function couldn't work. Here is my code:

int **matrix; //matrix is a 2 dimension array.
void row(int **ptr,int n)

main()
{
/*another part of the code go in here to  calculate the value of matrix*/
row(matrix,n);
}

void row(int **ptr,int n)
{
int sum_row,x=0,y=0;
for (y=0; y<=n-1;y++)
{
sum_row=0;
for (x=0;x<=n-1;x++)
{
sum_row=sum_row+*(*(ptr+x)+y);
}
printf("The sum of row %d is %d\n", y+1, sum_row);
}
}

Is there anything wrong when I pass the point to the function?

Thank you very much. -Zheng
Who is Participating?

Commented:
I think, this is what most people here consider the main point:

You have two basic choices on how to arrange your 'matrix' (assuming both dimensions to be 'N').

1. You allocate one array of N*N values.
array = malloc(N*N*sizeof(int));
matrix[x*N + y] or *(matrix + x*N + y)

2. In your example you created an array of pointers pointing to row arrays. In this case both, the pointer array and each row must be allocated:
array = malloc(N*sizeof(int *));
for (i=0..N-1) array[i] = malloc(N*sizeof(int));
array[x][y] or *(*(array+x)+y)

0

Commented:
Hope the below code is correct. One problem with your code is that the lower index of the 2D array must have a value.

#include <stdio.h>

void row(int ptr[10][10],int n);

void main()
{
int n;
int a[10][10];

printf("Enter the number of integers you wish to enter > ");
scanf("%d",&n);

row(a,n);
}

void row(int ptr[10][10], int n)
{
int i,j;
int count=0;
int total=0;

printf("Enter the %d number of integers >\n",n);

for ( i=0; i<=9; i++ )
{
total=0;
for ( j=0; count<n && j<=9; j++ )
{
scanf("%d",&ptr[i][j]);
total += ptr[i][j];
count++;
}
printf("The total is for row %d is %d\n",i+1,total);
}
}

hongjun
0

Commented:
What is the problem you are getting ?
0

Commented:
the function prototype is missing a ;
n is undefined in main
an uninitialised pointer is passed to row
and // is a c++ comment, not a c comment
0

Author Commented:
Thanks to hongjun's answer.The reason to reject the answer is because I need to use point as a argument instead of a known array.

I'd like to make my question clear. I have calculated the value of each element in a n*n array. In main(), I need to calculate the sum on each row by calling a function. The pointer should be used as an argument, since n is unknown. I run the following code, it end up "0" no matter what the matrix value is. Something must be wrong in passing the pointer to the function.Where is wwrong?

int n, **matrix;

void row(int **ptr,int n);

main()
{ printf("Enter the n value here:\n");
scanf("%d",n);
/* value of *(*(matrix+x)+y)will be calculate here */
/*call function row*/
row(matrix,n);
}

void row(int **ptr,int n)
{
int sum_row,x=0,y=0;
for (y=0; y<=n-1;y++)
{
sum_row=0;
for (x=0;x<=n-1;x++)
{
sum_row=sum_row+*(*(ptr+x)+y);
}
printf("The sum of row %d is %d\n", y+1, sum_row);
}
}

0

Commented:
But, how U allocate memory for matrix?
0

Commented:
Are you initializing 'matrix' like this?

for (int x = 0; x < n; ++x) {
*(matrix+x) = malloc(n*sizeof(int));
for (int y = 0; y < n; ++y) {
*(*(matrix+x)+y) = somevalue();
}
}
0

Commented:
You need to multiply "y" by the row length to read the correct values for the array. Thus, your sum line should look like:

sum_row=sum_row+*(*(ptr+x)+(y * n));
0

Commented:
How are you using the output of
void row(int **ptr,int n)  function in main ?
Actually you could return the sum_row as a return value from row function if you are using it to calculate the sum for a row.

I feel better is you do the following.

int n,sum, **matrix;

void row(int **ptr,int n);

main()
{ printf("Enter the n value here:\n");
scanf("%d",n);
/* value of *(*(matrix+x)+y)will be calculate here */
/*call function row*/
row(matrix,n);
/* Use sum here which contains value of matrix sum or you can
have sum as a pointer argument to row() which will populate it with
value of matrix sum*/
printf("The sum of matrix is %d\n", sum);
}

void row(int **ptr,int n)
{
int sum_row,x=0,y=0;
for (y=0; y<=n-1;y++)
{
sum_row=0;
for (x=0;x<=n-1;x++)
{
sum_row=sum_row+*(*(ptr+x)+y);
}
printf("The sum of row %d is %d\n", y+1, sum_row);
sum+=sum_row;
}
}

0

Commented:
but U can't use 1D array like this?

int n, *matrix;

void row(int *ptr,int n);

main()
{
int x, y;
printf("Enter the n value here:\n");
scanf("%d",&n);
matrix = (int *)malloc(n*n*sizeof(int));
for (x = 0; x < n; ++x) {
for (y = 0; y < n; ++y) {
*(matrix+x+y*n) = somevalue();
}
}

row(matrix,n);
}

void row(int *ptr,int n)
{
int sum_row,x=0,y=0;
for (y=0; y<=n-1;y++)
{
sum_row=0;
for (x=0;x<=n-1;x++)
{
sum_row=sum_row+*(ptr+x+y*n);
}
printf("The sum of row %d is %d\n", y+1, sum_row);
}
}

0

Commented:
Are you using pointer? If yes then you may have to use malloc or alloc to allocate memory.

hongjun
0

Commented:
Since n is variable, we cannot do the proper thing and work with predefined structure types.  We have to create our own structure on-the-fly by multiplying the row number by n when we access the matrix, as was said by pjknibbs.
Here is a complete program that produces the output below when n is set to 5.

The sum of row 0 is 10
The sum of row 1 is 35
The sum of row 2 is 60
The sum of row 3 is 85
The sum of row 4 is 110

#include <stdio.h>
#include "malloc.h"

void main()
{
int  *matrix;
int  column, row, n, k;

k = 0;
printf("Enter the n value here:\n");
scanf("%d",&n);

matrix= malloc(n*n*sizeof(int));
/*fill the array with values.*/
for( row=0; row<n; row++ ){
for(column=0; column<n; column++) {
*(matrix + row*n + column) = k++;
}
}
/*call function addRow to print the sum of each row*/
}
}

{
int sum_row;
int row=0;
int column=0;

for (row=0; row<n; row++)
{
sum_row=0;
for (column=0; column<n; column++)
{
sum_row=sum_row+ *(ptr + row*n + column);
}
printf("The sum of row %d is %d\n", row, sum_row);
}
}

0

Commented:
I seem to have mixed 'array' and 'matrix' above. Sorry
0

Commented:
Hi,
He wrongly placed the fields x and y.
Find below is the program and output. I hope it will do for him.
Assumed : To calculate the sum of row values.

# include <stdio.h>
int **matrix;
void row(int **ptr,int n);

main()
{
int n = 4;
int   val1[4]={1,2,3,4},
val2[4]={10,20,30,40}, val3[4]={100,200,300,400},
val4[4]={1000,2000,3000,4000};
int   *ip[4];
ip[0] = val1;
ip[1] = val2;
ip[2] = val3;
ip[3] = val4;

matrix = ip;
/*another part of the code go in here to  calculate the value of matrix*/
row(matrix,n);
}

void row(int **ptr,int n)
{
int sum_row,x=0,y=0,old_value;
for (y=0; y<=n-1;y++)
{
sum_row=0;
old_value = 0;
for (x=0;x<=n-1;x++)
{
old_value = sum_row;
sum_row=sum_row+*(*(ptr+y)+x);
old_value = sum_row - old_value;
printf("The value is %d \n",old_value);
}
printf("The sum of row %d is %d\n", y+1, sum_row);
}
}

Output :
----------
The value is 1
The value is 2
The value is 3
The value is 4
The sum of row 1 is 10
The value is 10
The value is 20
The value is 30
The value is 40
The sum of row 2 is 100
The value is 100
The value is 200
The value is 300
The value is 400
The sum of row 3 is 1000
The value is 1000
The value is 2000
The value is 3000
The value is 4000
The sum of row 4 is 10000
0

Author Commented:
My question is very well answered by many people.
Thanks,
-Zheng
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.