Page 1 of 4 123 ... Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0

    Initializing structs.


    Hi,
    I wish to initialize the main struct in my program, namely:
    Code:
    typedef struct taxiSystem{
        Driver * driversQueue;
        Order * ordersQueue;
        int numOrder;
    }TaxiSystem;
    I have hitherto written the following but am pretty stuck with initializing the two pointer fields. I did make an attempt, albeit am nearly positive it won't work:
    Code:
    TaxiSystem* initSystem(){
    	TaxiSystem *new_node = NULL;
    	Driver* drvptr = NULL;
    	Order* ordptr = NULL;
    	new_node = (TaxiSystem*) malloc(sizeof(TaxiSystem));
    	if (new_node == NULL){
    		printf("Fatal error: memory allocation failed!\n");
    		return NULL;
    	}
    	new_node->numOrder = 0;
    	new_node->driversQueue = drvptr;
    	new_node->ordersQueue = ordptr;
    	return new_node;
    }
    I would be grateful for some assistance, please.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,088
    Rep Power
    2222
    You end up setting the two fields, driversQueue and ordersQueue, to NULL. Is that what you want? It could be what you want, since those could be pointers to data structures such as linked lists. Perhaps a few comments would be in order to make that clear.

    If that is not what you want, then you should also malloc drvptr and ordptr before assigning them to driversQueue and ordersQueue, respectively. Also, if fields within the Driver and Order structs need to be initialized, you should initialize them as you
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    Originally Posted by dwise1_aol
    You end up setting the two fields, driversQueue and ordersQueue, to NULL. Is that what you want? It could be what you want, since those could be pointers to data structures such as linked lists. Perhaps a few comments would be in order to make that clear.

    If that is not what you want, then you should also malloc drvptr and ordptr before assigning them to driversQueue and ordersQueue, respectively. Also, if fields within the Driver and Order structs need to be initialized, you should initialize them as you
    TaxiSystem is the main struct, via which one is expected to access the two other structs, Order and Driver.
    At the beginning of the program the main struct, TaxiSystem, is to be initialized, including its fields. I hope this is slightly clearer now.
    As this is the case, does it mean that during this initialization process new_node->driversQueue and new_node->ordersQueue must also be explicitly allocated (dynamically) using malloc?

    PS I have already written functions called addOrder and addDriver. All I need now is to primarily and preliminarily initialize TaxiSystem.
  6. #4
  7. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,539
    Rep Power
    2337
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,088
    Rep Power
    2222
    Well, since he's getting all his answers elsewhere, I can just move on.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    Originally Posted by dwise1_aol
    Well, since he's getting all his answers elsewhere, I can just move on.
    I actually did not get answers there, or at least such which enabled me to make headway. I would truly appreciate it if you, or anyone else here for that matter, could please assist me with this code. I am quite stuck.
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,088
    Rep Power
    2222
    Originally Posted by peripatein
    TaxiSystem is the main struct, via which one is expected to access the two other structs, Order and Driver.
    At the beginning of the program the main struct, TaxiSystem, is to be initialized, including its fields. I hope this is slightly clearer now.
    As this is the case, does it mean that during this initialization process new_node->driversQueue and new_node->ordersQueue must also be explicitly allocated (dynamically) using malloc?

    PS I have already written functions called addOrder and addDriver. All I need now is to primarily and preliminarily initialize TaxiSystem.
    As I said, what you do all depends on what you want to do with those two queue pointers. That includes whether NULL is a valid value that you will test for.

    As it stands now, we do not have the knowledge to determine what you should do. Only you have that knowledge. All we could possibly do would be to make wild guesses.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    Originally Posted by dwise1_aol
    As I said, what you do all depends on what you want to do with those two queue pointers. That includes whether NULL is a valid value that you will test for.

    As it stands now, we do not have the knowledge to determine what you should do. Only you have that knowledge. All we could possibly do would be to make wild guesses.
    I would like to initialize TaxiSystem so as to enable me to add both new drivers and new orders to their respective queues via separate functions of course. I have the following main functions and structs:
    Code:
    typedef struct driver{
        char name[101];
        int priority;
        struct driver* next;
    }Driver;
    
    typedef struct order{
        int orderID;
        int isSpecific;
        char specificDriver[101];
        struct order* next;
    }Order;
    
    typedef struct taxiSystem{
        Driver * driversQueue;
        Order * ordersQueue;
        int numOrder;
    }TaxiSystem;
    
    int addDriver(TaxiSystem * system, char * name, int priority);
    int addOrder(TaxiSystem * system, int isSpecific , char * name);
    void printSystem(TaxiSystem * system);
    TaxiSystem* initSystem();
    void freeSystem(TaxiSystem* system);
    
    TaxiSystem* initSystem(){
    	TaxiSystem *new_node = NULL;
    	new_node = (TaxiSystem*) malloc(sizeof(TaxiSystem));
    	if (new_node == NULL){
    		printf("Fatal error: memory allocation failed!\n");
    		return NULL;
    	}
    	new_node->numOrder = 0;
    	new_node->driversQueue = NULL;
    	new_node->ordersQueue = NULL;
    	return (new_node);
    }
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,088
    Rep Power
    2222
    OK, so then I would assume that NULL is a valid value for those two fields, one which means that there aren't any orders or drivers yet. In that case, I don't see anything wrong with initSystem.

    So what was your problem?
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    As soon as I try to add a new driver/order to the respective queue, the program crashes! Let me post my main() first so we may start from there:
    Code:
    int main(){
            int choice = 0, priority, specific;
    	char name[101];
    	TaxiSystem *sysptr = initSystem();
    	while (choice != 4){
    		printf("Please enter your choice: 1)Add driver 2)Add order 3)Print system 4)Exit\n");
    		scanf("%d", &choice);
    		if (choice == 1){
    			printf("Please enter name and priority\n");
    			scanf("%s", name);
    			scanf("%d", priority);
    			if (!addDriver(sysptr, name, priority))
    				break;
    		}
    		if (choice == 2){
    			printf("Is the driver specific (please enter 1/0)?\n");
    			scanf("%d", &specific);
    			if (specific){
    				printf("Please enter the driver's name\n");
    				scanf("%s", &name);
    				addOrder(sysptr, specific, name);
    			}
    			addOrder(sysptr, specific, NULL);	
    		}		
    		if (choice == 3)
    			printSystem(sysptr);
    	}
    	freeSystem(sysptr);
    	return 0;
    }
    I'd be wholeheartedly indebted if you could please help me figure out how to fix this program. I am quite certain the problem is not in main() itself. I have narrowed it down to three or four other functions which could be cause.
  20. #11
  21. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,539
    Rep Power
    2337
    Better idea, rather than another marathon thread: learn to use your debugger.

    Comments on this post

    • salem agrees : well said
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    Originally Posted by ptr2void
    Better idea, rather than another marathon thread: learn to use your debugger.
    I just love it when people throw out critical comments into the air, without first ascertaining whether the comments are justified. I have used my debugger. My problem is that I am not sure A) why it crashes where it does; B) I am not sure how to fix that.
    There's no fault nor shame in asking for guidance and assistance as a person gets stuck after trying.
  24. #13
  25. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,539
    Rep Power
    2337
    OK, so run the program in the debugger and copy and paste the stack trace that is generated when it crashes here. Make sure you've compiled the code in debug mode, so that symbols will appear in the output.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    33
    Rep Power
    0
    OK, I'd be happy too. I do know how to use the debugger itself, albeit have never tried to copy paste things from the stack.
    First of all, whilst in debug mode the program, as it crashes, gives the following message:
    Access Violation
    And just before running it gave the following warnings:
    warning #2116: Local 'priority' is used without being initialized.
    warning #2117: Old-style function definition for 'initSystem'.
    warning #2096: Missing return value.

    I am using Pelles. How may I copy-paste the stack trace?
  28. #15
  29. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,539
    Rep Power
    2337
    Fix those warnings before you go any further.

    I don't know Pelles, so I can't tell you how to copy and paste the stack trace.

    Relevant links:
    http://forum.pellesc.de/index.php?topic=4283.0
    http://cboard.cprogramming.com/c-programming/131377-help-debugging-pelles-c-ide.html
    Last edited by ptr2void; June 13th, 2013 at 12:28 PM.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
Page 1 of 4 123 ... Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo