I am working on a program that uses interrupts and a buffer to read/write characters from/to the console screen. (This is a tty.c program with ttywrite(), ttyread(), ect...)

I also have a given queue folder, which contains queue.c that has the methods:

int init_queue(Queue *q, int max_chars);  make a new empty queue, filling in *q
 int enqueue(Queue *q,char ch);                enqueue char ch on q, or return -1 if can't
 int dequeue(Queue *q);                            dequeue a char and return it, or return -1 if can't
 int queuecount(Queue *q);                       return # chars currently in queue q

Along with queue.h, which mainly contains:

typedef struct queue {      
  char ch[MAXCHARBUF];         /* char contain in queue */
  int front;                              /* front index of queue */
  int rear;                              /* rear index of queue */
  int count;                          /* current numbers of element in queue */ 
  int max;                           /* actually use length */
} Queue;

along with some of the method headers and defined variables.

In the separate file tty.c where I am hoping to replace the buffers with queues, how can I develop a queue struct atop of the program that allows me to make instances of queue.c?

I am new to C (coming from C#/Java) and am lost trying to wrap my head around the logic.

Any advice will be greatly appreciated and needed.

>>how can I develop a queue struct atop of the program that allows me to make instances of queue.c?

This phrase: instances of queue.c, is probably more correctly stated as: instances of type queue. That's because queue.c is a file that contains information about how the user declared queue type as written here: typedef struct queue, is implemented, and you declare instances of the type, not instances of the file. That's a bit nit picky, but probably worth learning.

You've already declared an instance of type queue. It's called Queue. Therefore Queue has all the properties of a queue object (instance of type queue). Since Queue was declared at the time of declaring the user defined type, it is a global object and can be used anywhere by any function in your program. You could limit the scope of a queue object by declaring it within main() or some other function. I believe the syntax of C is that all objects of a given scope need to be declared at the beginning of the function within which it is declared, and not anywhere before it is used as in C++. To declare a queue object somewhere other than at the end of the type declaration you can do this:

#include "queue.h" /*note that you include the header file, not the implementation file containing the user declaration for type queue*/

queue Q;   /*Q is an instance of type queue with global scope*/

/*S is a queue with scope limited to the function called example*/
queue example(queue S) 
{
    queue T; /*T is a queue with scope limited to the function called example*/
    /*do whatever you want to T or S or whatever here*/
    return T;
}

int main()
{
    queue R;  /*R is an instance of type queue with scope within the function main()*/

    example(R);  /*pass R to example()*/

    example(Q); /*Q has global scope*/
    example(Queue); /*Queue has global scope as it is declared in queue.h, immediately after declaring the queue type*/
    return 0;
}

I believe there is a rule that if you don't use the keyword typedef before declaring the user defined type in C that you need to use the keyword struct before each declaration of an instance of that type, unless you declare the instance immediately after the declaration of the type, as you did with Queue. But, I don't use C much, so that belief may be in error.

commented: I am suprised you still didn't got two green dots -[G] +2

This seems to be very helpful, thank you for your input.

Within the program I am making a struct of queue's that are within the struct of tty in tty.c

struct tty ttytab[NTTYS] ; /*NTTYS being 1 or 2 (tty1, tty2)*/

This is what I have so far...

#include "queue/queue.h"
	

typedef struct myq{
	queue qWobj, qEobj, qRobj;
}myq;

struct tty ttytab[NTTYS] {  //tty1 or tty2
	queue *qwrite = &qWobj;
	queue *qread = &qRobj;
	queue *qecho = &qEobj;

	write = init_queue(qwrite, 6);
	read = init_queue(qread, 6);
	echo = init_queue(qecho, 6);
}ttytab[NTTYS];        /* software params/data for each SLU dev */

I am not definite that this is correct, again im still a begginer at C. Please help if I am wrong here.

And following this within by ttyinit in tty.c I want to reference the Queue variables from queue.h

tty->write.back = 0;
  tty->write.front = 0;
  tty->write.count = 0;

  tty->read.back = 0;
  tty->read.front = 0;
  tty->read.count = 0;

Does this make sense the way I am doing this?

Or am I way off and just need something simple like this:

struct tty ttytab[NTTYS] {
	queue qwrite, qread, qecho;
};

I'm not real sure what you are trying to do, but....

typedef struct tty 
{
   queue qwrite, qread, qecho;
}ttytab[NTTYS];

This declares a user defined type called tty Each tty object has three queue objects called qwrite, qread, and qecho. In addition an array of type tty has been declared holding up to NTTYS elements. What NTTYS stands for is anybodys guess, but as long as its a constant int value, it would work.

To my knowledge, this:

struct tty ttytab[NTTYS]

is not legal because I don't think you can declare an array like that, but then I won't swear to that either.

I also don't know what you mean by this:

a struct of queue's

queue is the name of a struct type. Therefore you can't have a struct of queue's. You can have queues in another struct, like tty, you can have an array or list or queue of queues, etc, but you can't have a struct of queues.

If you want an array of type queue in a struct called tty then this should work:

struct tty{
  queue queueArray[3]; 
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.