This (above) is a complete type, the compiler knows what is inside a struct Queue_Struct.Code:typedef struct Queue_Struct { char * member1; int member2; } Q_type;
This (below) is an INcomplete type:
Code:typedef struct Queue_Struct Q_type;
This (above) is a complete type, the compiler knows what is inside a struct Queue_Struct.Code:typedef struct Queue_Struct { char * member1; int member2; } Q_type;
This (below) is an INcomplete type:
Code:typedef struct Queue_Struct Q_type;
Hmm...okay.
So inorder to fix this error of dereferencing I should change my Q_type's to Q_struct?
If I wanted to define (which I guess I have to) my struct Q_struct? What would I even put in it?
Correct, the way you are accessing Q you clearly want it to be a struct Q_struct, which is typedef'd to Q_struct. So Instead change it to:
It sounds however like you need to review structures and typedefs in the first place if you don't even understand why you are doing what you are doing. Did you even write this code or did you copy it from somewhere?Code:typedef struct Q_struct { ... } Q_type;
Please.. only I know the brainpower and tension it took me to come up with this code.
My instructor did provide us with this skeleton code but it didn't help me as much as it could have and since I took ages to come up with this code I really want to understand everything. Thank you
So the number of errors while compiling has decreased dramatically.
Last edited by kiwi101; 12-08-2012 at 04:57 PM.
Okay so the compiler says:
I am redefining Q_type which in the header file looks like this:
but in the Q.c file looks like this:Code:typedef struct Q_struct Q_type;
Code:typedef struct Q_struct { int contents[Q_MAX_SIZE]; int front; int count; (THIS LINE) } Q_type;
Last edited by kiwi101; 12-08-2012 at 05:01 PM.
So, how exactly am I redefining it?
By typedefing twice. Simply remove 'typedef' from the second declaration. So you have
Code:typedef struct Q_struct Q_type; ... struct Q_struct { ... };
Thank you so much with this structure stuff I know I'm confused with it but some of the confusion has disappeared
ITS COMPILED!!!
okay first thing I got a warning for this line in my main saying "assignment makes pointer from integer without a cast"
myQ = Qnew();
and now that my program is running (THANK GOD!!)
it's not printing anything or inserting
Q.h
Q.cCode:struct Q_struct; typedef struct Q_struct Q_type; /* Allocate a new Queue and initialize it to be empty. */ Q_type *Q_new(); /* Print the Queue in an attractive fashion */ void Q_print (Q_type *); /* error codes for errorflag returned by Q_add and Q_delete */ #define ERROR_FULL_QUEUE 0 #define ERROR_EMPTY_QUEUE -1 #define EVERYTHING_OK 1 /* add an item at the end of the Queue */ void Q_add(Q_type *, int, int *errorflag);
main.cCode:#include <stdio.h> #include <stdlib.h> #include "Q.h" /********************************************************* Implementation of the Queue ADT using a fixed size array. *********************************************************/ /* Queue size must be an array of size 10 */ #define Q_MAX_SIZE 10 struct Q_struct { int contents[Q_MAX_SIZE]; int front; int count; }; Q_type *Qnew() { Q_type *Q; Q = (Q_type *)malloc(sizeof(Q_type)); Q->front = 0; Q->count = 0; return Q; } void Q_add(Q_type *Q, int value, int *errorflag) { int newElementIndex; if (Q->count >= Q_MAX_SIZE) { *errorflag = ERROR_FULL_QUEUE; return; } *errorflag = EVERYTHING_OK; newElementIndex = (Q->front + Q->count) % Q_MAX_SIZE; Q->contents[newElementIndex] = value; Q->count++; } void Q_print (Q_type *Q) { int i; if (Q->front <= 0) return; if ( Q->count >= Q->front) { for(i = Q->front; i <= Q->count; i++) { printf("\n i = %d", i); printf(" %d ", Q->contents[i]); } } else { for(i = Q->front; i <Q_MAX_SIZE; i++) { printf("\n i = %d", i); printf(" %d", Q->contents[i]); } for(i = 0; i <= Q->count; i++) { printf("\n i = %d", i); printf(" %d ", Q->contents[i]); } } }
So, is it because my code is wrong or because of that warning since I tend to ignore warningsCode:#include <string.h> #include <stdio.h> #include <stdlib.h> #include "Q.h" /* Queue header file */ void show_commands() { printf("Queue Processing Commands:\n"); printf("==========================\n"); printf("n : \n"); printf("p : \n"); printf("a : \n"); printf("d : \n"); printf("v : \n"); printf("e : \n"); printf("f : \n"); } int main(void) { Q_type *myQ; myQ = Qnew(); /* create an empty Q data object */ char command; // store current command int errorflag; // possible error code int value; // value to be inserted or returned float avg; // average of queue values show_commands(); printf("Queue command? "); scanf("%c", &command); while (command != 'q') { switch (command) { case 'p': Q_print(myQ); break; case 'a': printf("Enter value to insert: "); scanf("%d", &value); Q_add(myQ, value, &errorflag); if (errorflag == ERROR_FULL_QUEUE) printf("Queue full -- insert failed.\n"); break; default: printf("Command not recognized\n"); show_commands(); } printf("Queue command? "); scanf("%c",&command); } return 0; }
When I insert the value it takes it as a command and says command not recognized whereas the value should go straight to Q_add function, why isn't it?
See post #2, where I made the mistake and written %d instead of %c, which was what I wanted to say.
What input to you give?
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Oh sorry yeah fixed that still same error
here is my output
Code:Queue Processing Commands: ========================== n : p : a : d : v : e : f : Queue command? a Enter value to insert: 12 Queue command? Command not recognized Queue Processing Commands: ========================== n : p : a : d : v : e : f :
see it's ignoring my inserted value and processing it as a command?
Read one of the tutorials on input. This is a problem with using scanf to read keys from the user. The \n is being left in the buffer so your next
call to scanf reads that and automatically moves on. This can be "fixed" by simply placing a space in front of your calls like this:
Still please read up on how to properly retrieve input from the user one of the 'fgets' tutorials.Code:scanf(" %c", &c);
I definitely know how to use fgets. I've been doing it on my other projects. I'll demonstrate it if you don't believe me
So.. It's inserting but still not printing I think there is something wrong with my command loop. Cuz this is how my output goes
after the first 'p' it should've printed itCode:Queue Processing Commands: ========================== n : p : a : d : v : e : f : Queue command? a Enter value to insert: 12 Queue command? p Queue command? p Queue command?