Results 1 to 8 of 8
okay.. i'm not sure if the errors somewhere in this code are due to the fact that it was written at 5am EST without the aid of coffee or nicotine ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 01-29-2004 #1Linux Engineer
- Join Date
- Sep 2003
- Location
- Knoxhell, TN
- Posts
- 1,078
SegFaults in a useless program.
okay.. i'm not sure if the errors somewhere in this code are due to the fact that it was written at 5am EST without the aid of coffee or nicotine or if i just failed to understand something.. either way, i can't seem to figure out what's causing the segfault every time the program is run... and here is the (lousy) source:
also, any suggestions on how to improve the elegance of this code would be appreciated.. i don't want to get into bad habits early on..Code:#include <stdio.h> #define MAX 12 void assign(char *array[]); void printarray(char *array[]); int main() { char array[MAX][MAX]; assign(array); printarray(array); return 0; } void assign(char *array[]) { int x; int y; for(x=0;x<MAX;x++) { for(y=0;y<MAX;y++) { if((x==0)||!(x%2)) { if((y==0)||!(y%2)) { (char)array[x][y]='X'; //problem here } else { (char)array[x][y]=' '; //i'm assuming here, too } } else { if(y%2) { (char)array[x][y]='X'; //most likely same thing } else { (char)array[x][y]=' '; //i think you get the idea by now.. } } } } } void printarray(char *array[]) { int x,y; for(x=0;x<MAX;x++) { for(y=0;y<MAX;y++) { printf("%c",(char)array[x][y]); //oh, look, another one. if(y==(MAX-1)) { printf("\n"); } } } }
edit: i ran it through gdb and the problem has something to do with the way the array is passed to the functions and the assigning of literal character values to an array element... just not sure what's wrong with the array(s)....
edit (again): adding some comments at problem linesTheir code will be beautiful, even if their desks are buried in 3 feet of crap. - esr
- 01-29-2004 #2Linux User
- Join Date
- Sep 2003
- Posts
- 254
Hi
Could u give us the entire error message from gcc or an other compiler?
Thx
- 01-29-2004 #3Linux Engineer
- Join Date
- Sep 2003
- Location
- Knoxhell, TN
- Posts
- 1,078
Code:gridx.c: In function `main': gridx.c:13: warning: passing arg 1 of `assign' from incompatible pointer type gridx.c:14: warning: passing arg 1 of `printarray' from incompatible pointer type
that's the output of gcc -Wall -g -o ./bin/gridx gridx.c (i adjusted the line numbers because i didn't paste the comment at the top of the file)Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr
- 01-29-2004 #4Linux Engineer
- Join Date
- Sep 2003
- Location
- Knoxhell, TN
- Posts
- 1,078
it was in the function prototypes and declarations.. they needed to be this:
it was expecting a different argument than a multi-dimensional array...Code:void assign(char (*array)[12]); void printarray(char (*array)[12]);
Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr
- 01-29-2004 #5Linux User
- Join Date
- Sep 2003
- Posts
- 254
If I understand quite well it was simply expecting a little memory (this was the reason of the segfaultà
Originally Posted by lordnothing
Am I right?
Could u explain this line:I don't understand the meaning of the modulo why is it use here?Code:if((x==0)||!(x%2))
thx
- 01-29-2004 #6Linux Guru
- Join Date
- Oct 2001
- Location
- Täby, Sweden
- Posts
- 7,578
In any case, the whole thing is because you seem to have misunderstood multidimensional arrays in C. A `char [12][12]' is not the same as a `char **', but in fact a `char *'. A multidimensional array in C is in fact a one-dimensional array, except that the compiler treats it differently in the same scope where it is defined. You can do this, for example:
As a reference, in the same scope where they are defined, multidimensional array lookup is treated like this:Code:int main(void) { char a[12][12]; function(a, 1, 2); } void function(char *a, int x, int y) { printf("The element at (%i, %i) is %c\n", x, y, a[x + (y * 12)]); }
A little pseudo-code there... did you understand what I mean?Code:type array[maxy][maxx]; array[x][y] == ((type *)array)[x + (y * maxx)];
- 01-29-2004 #7Linux User
- Join Date
- Sep 2003
- Posts
- 254
Me I don't understand
I'm trying but here
really nothing
- 01-29-2004 #8Linux Engineer
- Join Date
- Sep 2003
- Location
- Knoxhell, TN
- Posts
- 1,078
Gnux: the if((x==0)||!(x%2)) was so that the statements directly below it would only execute if x==0 or x is even... since x%2 returns 0 on an even number, if i tel it !(x%2) it will read the 0 as a TRUE condition....
Dolda: i understand it a bit.. it all has to do with how the memory is allocated and where the actual data in the array is...Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr


Reply With Quote
