Thread: Pointer problems :(

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    8

    Pointer problems :(

    I'm having a hard time finding my segmentation faults. Can anyone help me out please?

    Code:
    #include <stdlib.h>
    #include <strings.h>
    #include <stdio.h>
    
    typedef struct proc_struct proc_struct;
    
    typedef struct proc_struct * proc_ptr;
    
    proc_ptr ReadyList[6];
    
    struct proc_struct {
       proc_ptr       next_proc_ptr;
       char           name[10];
       int            priority;
    };
    
    void enqueue(proc_ptr *front, proc_ptr ptr){
    	if(*front == NULL)
    		*front = ptr;
    	else
    	{
    		proc_ptr p;
    		for(p = *front; p != NULL; p = p->next_proc_ptr){}
    		p = ptr;
    	}
    }
    
    proc_ptr dequeue(proc_ptr *front){
    	if(*front == NULL)
    	{
    		return NULL;
    	}
    	else
    	{
    		proc_ptr value =  *front;
    		*front = (*front)->next_proc_ptr;
    		return value;
    	}
    }
    
    
    main()
    {
    	int i;
    	for(i = 0; i < 6; i++)
    		ReadyList[i] = NULL;
    
       proc_struct a,b;
    	strcpy(a.name,  "hello");
    	strcpy(b.name,  "bye");
    	a.priority = 1;
    	b.priority = 1;
        a.next_proc_ptr = NULL;
        b.next_proc_ptr = NULL;
    
       enqueue( &ReadyList[1], &a );
       enqueue( &ReadyList[1], &b );
    
       printf(" ReadyList[1] contains %s \n", ReadyList[1]->name);
       printf(" ReadyList[1]'s next contains %s \n", ReadyList[1]->next_proc_ptr->name);
    
       //printf("%s\n",dequeue(&ReadyList[1])->name);
    }
    Last edited by ryptide; 06-16-2004 at 03:08 AM.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    ah the good ol to much in a for loop problem
    your writing to something that doesn't exits right here
    Code:
    for(i = 0; i < 6; i++)
    should be
    Code:
    for(i = 0; i < 5; i++)
    Woop?

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    8
    No that is correct. Since I initialized my Array with 6:

    Code:
    proc_ptr ReadyList[6];
    my for loop will only run 6 times:

    Code:
    for(i = 0; i < 6; i++)
    from 0 to 5

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    yes but arrays start at 0 not at 1 woops nevermind
    [edit] sorry its late im going to bed [/edit]
    Last edited by prog-bman; 06-16-2004 at 03:11 AM.
    Woop?

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    8
    yup. but it shouldn't matter if I use ReadyList[1] instead of ReadyList[0].. ? or should it. I was just using main to test out my queue which has a seg faults in it

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    8
    I've narrowed it down to here... What is wrong with this??

    Code:
    for(p = *front; p != NULL; p = p->next_proc_ptr){}

    thanks for your help.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ryptide
    I've narrowed it down to here... What is wrong with this??

    Code:
    for(p = *front; p != NULL; p = p->next_proc_ptr){}
    thanks for your help.
    Nothing's wrong with it, if you want p to end up NULL at the end. Since I doubt that's what you had in mind, you'll probably want to be checkign "p->next_proc_pointer" in the termination segment of the for loop also, and once "p->next_proc_pointer" is NULL, then set that to the new pointer.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jul 2002
    Posts
    8
    After you said that a lightblub went off in my head! THANKS!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. base class pointer problems
    By ... in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2003, 11:27 PM