LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-16-2006, 11:10 PM   #1
pittopitto
Member
 
Registered: Sep 2005
Location: Australia
Distribution: SUSE Linux Professional 9.3
Posts: 35

Rep: Reputation: 15
Passing bidimensional arrays to function in C


Dear folks!!

I have a little problem. I can't pass bidimensional arrays to a function. I'll make an example: (code taken from a book)

Quote:
#include <stdio.h>

void printArray (const int a[][3]);

int main () {

int array1[2][3] = {{1,2,3},{4,5,6}};
int array2[2][3] = {1,2,3,4,5};
int array3[2][3] = {{1,2},{4}};

printf ("Values in array1 by row are:\n");
printArray(array1);

printf("Values in array2 by row are:\n");
printArray(array2);

printf("Values in array3 by row are:\n");
printArray(array3);

return 0;
}

void printArray (const int a[][3]) {

int i;
int j;

for (i=0; i<=1; i++) {

for (j=0; j<=2; j++) {

printf("%d ", a[i][j]);
}

}

}
Then I compile it with gcc:

Quote:
pitto@linux:~/Documents/C/Exercises> gcc -o matrix matrix.c
matrix.c: In function ‘main’:
matrix.c:12: warning: passing argument 1 of ‘printArray’ from incompatible pointer type
matrix.c:15: warning: passing argument 1 of ‘printArray’ from incompatible pointer type
matrix.c:18: warning: passing argument 1 of ‘printArray’ from incompatible pointer type
pitto@linux:~/Documents/C/Exercises>
What does it mean? I would like to pass a 2D array to a function, then modify it and then back to main.

Can you please help me???

Thanks in advance

pittopitto
 
Old 05-16-2006, 11:17 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
It's merely a warning - the code should still compile and run correctly.

A couple of interesting notes:
1. Microsoft VC++ doesn't even print the warning (Gcc does)
2. The thing it's complaining about is "const" (not the array dimensions)
3. To fix it, simply:
a) Eliminate "const" in your function signature
b) Cast it to "const" when you call your function
... or ...
c) Declare array1, array2 and array3 as "const"

Last edited by paulsm4; 05-16-2006 at 11:24 PM.
 
Old 05-16-2006, 11:20 PM   #3
leosgb
Member
 
Registered: Nov 2004
Location: Brazil
Distribution: Gentoo
Posts: 363

Rep: Reputation: 31
I am not sure why you are getting that but this is my output:

Code:
johndoe@zeus ~/bin $ gcc -Wall a.c 
a.c: In function `main':
a.c:8: warning: missing braces around initializer
a.c:8: warning: (near initialization for `array2[0]')
a.c:12: warning: passing arg 1 of `printArray' from incompatible pointer type
a.c:15: warning: passing arg 1 of `printArray' from incompatible pointer type
a.c:18: warning: passing arg 1 of `printArray' from incompatible pointer type
johndoe@zeus ~/bin $ ./a.out 
Values in array1 by row are:
1 2 3 4 5 6 Values in array2 by row are:
1 2 3 4 5 0 Values in array3 by row are:
1 2 0 4 0 0 johndoe@zeus ~/bin $
I just copy and pasted your code. No modifications.
 
Old 05-17-2006, 01:01 AM   #4
pittopitto
Member
 
Registered: Sep 2005
Location: Australia
Distribution: SUSE Linux Professional 9.3
Posts: 35

Original Poster
Rep: Reputation: 15
Thumbs up Code working

Dear folks,

I executed the code after compilation and it's working.
About the output, I just forgot to put a newline!!
Sorry!!

Pitto
 
Old 05-17-2006, 10:21 AM   #5
leosgb
Member
 
Registered: Nov 2004
Location: Brazil
Distribution: Gentoo
Posts: 363

Rep: Reputation: 31
good to hear that
 
Old 05-18-2006, 09:31 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Maybe 'const int &a[][3]' or 'const int *a[3]' would get rid of the warning...
ta0kira
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
passing structures to function b123coder Programming 3 04-20-2005 10:15 AM
perl: passing 2 individual arrays/lists as args johnMG Programming 12 04-18-2005 03:49 PM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 12:08 PM
A main can be changed by a function local without passing anything to the function? ananthbv Programming 10 05-04-2004 01:31 PM
passing a string to a function jkobrien Programming 8 11-05-2003 01:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration