By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,401 Members | 803 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,401 IT Pros & Developers. It's quick & easy.

How To Pass a 2D array to a Cfunction using Pointers?

P: 8
I have got a problem using Arrays,Functions and Pointers together.Please tell me hoe to pass a 2 D Array to a C function using Pointers.When I compile such a Program,I get an error saying "Function needs a prototype".If I give the protype ,then also it gives an error like,"Cannot convert int into int* "or something like that.If needed I can post the particular program with which i had the problem.Please help me out !
Aug 7 '07 #1
Share this Question
Share on Google+
10 Replies


gpraghuram
Expert 100+
P: 1,275
I have got a problem using Arrays,Functions and Pointers together.Please tell me hoe to pass a 2 D Array to a C function using Pointers.When I compile such a Program,I get an error saying "Function needs a prototype".If I give the protype ,then also it gives an error like,"Cannot convert int into int* "or something like that.If needed I can post the particular program with which i had the problem.Please help me out !
Hi,
You need not ost the entire code.
Post the prototypw you have declared and how are u calling it.
Raghuram
Aug 7 '07 #2

P: 8
The prototype I had declared was :

void display(int *,int,int);

The function call is as follows :

display(a,3,4);

The function definition is as follows:

void display(int *q,int row,int col)
{
}






Hi,
You need not ost the entire code.
Post the prototypw you have declared and how are u calling it.
Raghuram
Aug 7 '07 #3

gpraghuram
Expert 100+
P: 1,275
The prototype I had declared was :

void display(int *,int,int);

The function call is as follows :

display(a,3,4);

The function definition is as follows:

void display(int *q,int row,int col)
{
}
i think a in display function is a 2-d array.
If that is the case then the prototype/definition u have declared wont work.
U have to change the calloing part to pass only one dimension to this function
check this
Expand|Select|Wrap|Line Numbers
  1. int arr[10][10];
  2. display(arr[1],1,10);
  3.  
but if u want to pass the 2-d array as a whole then the display function shuld be like
[code=c]
display(int **arr,int row,int col)
[code]


Thanks
Raghuram
Aug 7 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
i think a in display function is a 2-d array.
If that is the case then the prototype/definition u have declared wont work.
U have to change the calloing part to pass only one dimension to this function
check this

Code: ( c )
int arr[10][10];
display(arr[1],1,10);


but if u want to pass the 2-d array as a whole then the display function shuld be like
Expand|Select|Wrap|Line Numbers
  1. display(int **arr,int row,int col)
  2.  
This is not correct. When you pass an array to a function you pass the name of the array. In C/C++, the name of the array is the address of element 0.

In tis example, the name arr is the address of arr[0]. However, arr[0] is itself an array of 10 int. Therefore, the name arr is address of an array of 10 int. Therefore, yoou need a pointer to an array of 10 int:

Expand|Select|Wrap|Line Numbers
  1. display(int (*arr)[10], int numelements);
  2.  
This will work and you need pass only the number of 10 int arrays.
Aug 7 '07 #5

gpraghuram
Expert 100+
P: 1,275
This is not correct. When you pass an array to a function you pass the name of the array. In C/C++, the name of the array is the address of element 0.

In tis example, the name arr is the address of arr[0]. However, arr[0] is itself an array of 10 int. Therefore, the name arr is address of an array of 10 int. Therefore, yoou need a pointer to an array of 10 int:

Expand|Select|Wrap|Line Numbers
  1. display(int (*arr)[10], int numelements);
  2.  
This will work and you need pass only the number of 10 int arrays.
Thanks for pointing out the mistake.
I fogot that when a 2-d array is passed as parameter then it becomes pointer to an array.

Raghuram
Aug 8 '07 #6

P: 8
Hi,I am Rachana, doing my MCA.I am still on the learning stage of C.Thanks for replying me,But sorry to say, that I still did not get it.I tried doing like what you told.But still somewhere I am wrong.I am posting the code.It will be very helpful for me if you please tell me where I should make the necessary changes.I am stuck with this program.I am following Let Us C book to learn C.Till I understand this ,I dont want to go further.

code:

main()
{

int a[3][4]={
1,2,3,4,
5,6,7,8,
9,0,1,6
};
display(a,3,4);
}

display(int * q,int row,int col)
{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
printf(" %d ",*(q+i*col+j) );
}
}


The output for this program is :
1 2 3 4
5 6 7 8
9 0 1 6


I am getting mainly two errors:
1) Function needs a prototype
2) cannot convert in* to int.

I thing i am wrong in my prototype declaration.But please help me out.This is the program given in Let Us C book.

Thankyou
Rachana
Aug 9 '07 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
My Paradise: Did you read my post #5??

The example in your book is incorrect.
Aug 9 '07 #8

P: 8
Thankyou very much for your reply.I read your reply and I tried also,but still I was getting the same errors.But I think I was wrong in my Prototype definition.I gave the prototype definition as:

void display(int *,int,int);

If this is wrong,then please tell me how to write the prototype definition.I am still a beginner in C,so please help me.
Aug 10 '07 #9

weaknessforcats
Expert Mod 5K+
P: 9,197
void display(int *,int,int);
This function does not take a 2D array as an argument. What it does it take an int* as an argument. An int* is a pointer to a single int. Thios single int could be the start of an array but you can't tell just from the int*. You have to assume it is the first integer of an array. This is perfectly OK so long as the assumption is correct. If it's not, then the function may not work.

To use this function with a 2D array like:
int a[3][4]={
1,2,3,4,
5,6,7,8,
9,0,1,6
};
you need to call the function with the address of an int. Like,maybe, the address of a[0][0]:

Expand|Select|Wrap|Line Numbers
  1. display(&a[0][0],3,4);
  2.  
and off you go.

Notice the memory layout of a one-dimensional array:

1 2 3 4 5 6 7 8 9 0 1 6

and your 2D array:

1 2 3 4 5 6 7 8 9 0 1 6

Kinda, the same.

So, the function can process as though there were 12 ints on a one-dimensional array or a 2D array of 4 rows of 3 ints.

One of the processing tricks for 2D arrays is to load them as though they were one-dimensional arrays and then process them as 2D arrays.
Aug 10 '07 #10

P: 8
Thankyou very very much for the explanation.You have explained it really well for me.Now I understood how to do.
Aug 17 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.