Segmentation fault structs

This is a discussion on Segmentation fault structs within the C Programming forums, part of the General Programming Boards category; Code: func (sARGUMENTOS *argumentos, sGUARDA_RECTANGULOS *rects) { int erro=E_NOERR, i=0; while (rects!=NULL) { rects++; i++; } strcpy(rects->.Nome, argumentos[1].arg_num); rects->rect.x = ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    4

    Exclamation Segmentation fault structs

    Code:
    func (sARGUMENTOS *argumentos, sGUARDA_RECTANGULOS *rects) {
    
            int erro=E_NOERR, i=0;
           
           
    
           
                    while (rects!=NULL) {
                            rects++;
                            i++;
                    }
           
           
                    strcpy(rects->.Nome, argumentos[1].arg_num);
                    rects->rect.x = 0;
                    rects->rect.y = 0;
                    rects->rect.tamx = argumentos[2].arg_num;
                    rects->rect.tamy = argumentos[3].arg_num;
                   
                    if (strcmp(argumentos[4].arg_num, "n") == 0) {
                            rects->roda_rectangulo = NAO;
                    }
                    else {
                            rects->roda_rectangulo = SIM;
                    }
    
                   
                    printf("%s %d %d %d %d", rects[i].Nome, rects[i].rect.x, rects[i].rect.y, rects[i].rect.tamx, rects[i].rect.tamy, rects[i].roda_rectangulo);
                   
    
    
            return erro;
    
    
    }
    any suggestions?
    Last edited by aalex; 04-10-2011 at 09:23 AM.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Code:
                    while (rects!=NULL) {
                            rects++;
                            i++;
                    }
    When does this loop stop? When rects is NULL. Thus, any time after this that you do rects->, you're accessing invalid memory, causing a seg fault.

    Code:
           
                    strcpy(rects->.Nome, argumentos[1].arg_num);
    That's not valid syntax. You can't use the -> and the . together.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    "When does this loop stop? When rects is NULL. Thus, any time after this that you do rects->, you're accessing invalid memory, causing a seg fault."

    How to solve the problem?
    objective is to insert the element at last position.


    "That's not valid syntax. You can't use the -> and the . together. "

    Yes, it's just ->

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    You're treating rects as though it's an array. You could simply pass in the current count of elements, then you would put your new data in rects[count].

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    an example with the code above?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by aalex View Post
    an example with the code above?
    Since I seem to be on a tear about helpfiles today...

    One of a programmer's most important skills is the ability to look stuff up.
    Climb into your books, tutorials, help files, man pages, whatever and take a look at how arrays work...
    See if you can come up with an answer on your own.

    Then... if you get stuck (really stuck, not lazy stuck) post your code here and I'm sure some of us will help you sort it out.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    Quote Originally Posted by CommonTater View Post
    Since I seem to be on a tear about helpfiles today...

    One of a programmer's most important skills is the ability to look stuff up.
    Climb into your books, tutorials, help files, man pages, whatever and take a look at how arrays work...
    See if you can come up with an answer on your own.

    Then... if you get stuck (really stuck, not lazy stuck) post your code here and I'm sure some of us will help you sort it out.


    I'm right back to it several hours. I think the problem is not laziness ...


    Code:
    int func_rect (sARGUMENTOS *argumentos, sGUARDA_RECTANGULOS *rects) {
    
    	int erro=E_NOERR, i=0;
    	
    	
    	/*verifica se o ultimo argumento do RECT  valido (s ou n)*/	
    	
    	if (strcmp(argumentos[4].arg_num, "n") != 0 && strcmp(argumentos[4].arg_num, "s") != 0) {
    			erro = mensagem_de_erro(E_INVARGS);
    	}
    	
    	else {
    	
    		while (rects!=NULL) {
    			rects++;
    			i++;
    		}	
    	
    		strcpy(rects[i].Nome, argumentos[1].arg_num);
    		rects[i].rect.x = 0;
    		rects[i].rect.y = 0;
    		rects[i].rect.tamx = argumentos[2].arg_num;
    		rects[i].rect.tamy = argumentos[3].arg_num;
    		
    		if (strcmp(argumentos[4].arg_num, "n") == 0) {
    			rects[i].roda_rectangulo = NAO_RODA_RECTANGULO;
    		}
    		else {
    			rects[i].roda_rectangulo = RODA_RECTANGULO;
    		}
    
    		
    		printf("%s %d %d %d %d", rects[i].Nome, rects[i].rect.x, rects[i].rect.y, rects[i].rect.tamx, rects[i].rect.tamy, rects[i].roda_rectangulo);
    
    		rects=i+1;
    		
    	}
    
    	return erro;
    
    
    }
    not already in segmentation fault

    but not back to the program, gets stuck
    Last edited by aalex; 04-10-2011 at 10:55 AM.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Did you write the func_rect function? Do you know what a function parameter is? If so, add a third parameter to that function called count. If not, read your books and Google for some tutorials. Once you understand functions, add a third parameter called count. Then, ditch that while(rect != NULL) loop, since it doesn't work anyways, and just do rects[count] instead of rects[i].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault!!
    By Necromancer in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2008, 07:03 AM
  2. Replies: 3
    Last Post: 04-19-2004, 06:42 PM
  3. Segmentation Fault printing an array of structs
    By ccoder01 in forum C Programming
    Replies: 1
    Last Post: 04-17-2004, 07:03 AM
  4. Segmentation fault with structs and char pointers
    By Keybone in forum C++ Programming
    Replies: 20
    Last Post: 01-17-2004, 12:36 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 10:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21