Find the answer to your Linux question:
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.
  1. #1
    Linux 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:

    Code:
    #include <stdio.h>
    
    #define MAX 12
    
    void assign&#40;char *array&#91;&#93;&#41;;
    void printarray&#40;char *array&#91;&#93;&#41;;
    
    int main&#40;&#41;
    &#123;
    	char array&#91;MAX&#93;&#91;MAX&#93;;
    	
    	assign&#40;array&#41;;
    	printarray&#40;array&#41;;
    
    	return 0;
    &#125;
    
    void assign&#40;char *array&#91;&#93;&#41;
    &#123;
    	int x;
    	int y;
    
    	for&#40;x=0;x<MAX;x++&#41;
    	&#123;
    		for&#40;y=0;y<MAX;y++&#41;
    		&#123;
    			if&#40;&#40;x==0&#41;||!&#40;x%2&#41;&#41;
    			&#123;
    				if&#40;&#40;y==0&#41;||!&#40;y%2&#41;&#41;
    				&#123;
    					&#40;char&#41;array&#91;x&#93;&#91;y&#93;='X';  //problem here
    				&#125;
    				else
    				&#123;
    					&#40;char&#41;array&#91;x&#93;&#91;y&#93;=' ';  //i'm assuming here, too
    				&#125;
    			&#125;
    			else
    			&#123;
    				if&#40;y%2&#41;
    				&#123;
    					&#40;char&#41;array&#91;x&#93;&#91;y&#93;='X';  //most likely same thing
    				&#125;
    				else
    				&#123;
    					&#40;char&#41;array&#91;x&#93;&#91;y&#93;=' ';  //i think you get the idea by now..
    				&#125;
    			&#125;
    		&#125;
    	&#125;
    
    &#125;
    
    void printarray&#40;char *array&#91;&#93;&#41;
    &#123;
    	int x,y;
    
    	for&#40;x=0;x<MAX;x++&#41;
    	&#123;
    		for&#40;y=0;y<MAX;y++&#41;
    		&#123;
    			printf&#40;"%c",&#40;char&#41;array&#91;x&#93;&#91;y&#93;&#41;; //oh, look, another one.
    			if&#40;y==&#40;MAX-1&#41;&#41;
    			&#123;
    				printf&#40;"\n"&#41;;
    			&#125;
    		&#125;
    	&#125;
    &#125;
    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..

    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 lines
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  2. #2
    Linux User
    Join Date
    Sep 2003
    Posts
    254
    Hi
    Could u give us the entire error message from gcc or an other compiler?
    Thx

  3. #3
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    Code:
    gridx.c&#58; In function `main'&#58;
    gridx.c&#58;13&#58; warning&#58; passing arg 1 of `assign' from incompatible pointer type
    gridx.c&#58;14&#58; warning&#58; 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

  4. #4
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    it was in the function prototypes and declarations.. they needed to be this:
    Code:
    void assign&#40;char &#40;*array&#41;&#91;12&#93;&#41;;
    void printarray&#40;char &#40;*array&#41;&#91;12&#93;&#41;;
    it was expecting a different argument than a multi-dimensional array...
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  5. #5
    Linux User
    Join Date
    Sep 2003
    Posts
    254
    Quote Originally Posted by lordnothing
    it was expecting a different argument than a multi-dimensional array...
    If I understand quite well it was simply expecting a little memory (this was the reason of the segfaultā
    Am I right?
    Could u explain this line:
    Code:
       if&#40;&#40;x==0&#41;||!&#40;x%2&#41;&#41;
    I don't understand the meaning of the modulo why is it use here?
    thx

  6. #6
    Linux 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:
    Code:
    int main&#40;void&#41;
    &#123;
        char a&#91;12&#93;&#91;12&#93;;
        
        function&#40;a, 1, 2&#41;;
    &#125;
    
    void function&#40;char *a, int x, int y&#41;
    &#123;
        printf&#40;"The element at &#40;%i, %i&#41; is %c\n", x, y, a&#91;x + &#40;y * 12&#41;&#93;&#41;;
    &#125;
    As a reference, in the same scope where they are defined, multidimensional array lookup is treated like this:
    Code:
    type array&#91;maxy&#93;&#91;maxx&#93;;
    
    array&#91;x&#93;&#91;y&#93; == &#40;&#40;type *&#41;array&#41;&#91;x + &#40;y * maxx&#41;&#93;;
    A little pseudo-code there... did you understand what I mean?

  7. #7
    Linux User
    Join Date
    Sep 2003
    Posts
    254
    Me I don't understand
    I'm trying but here really nothing

  8. #8
    Linux 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •