ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.