Oliver K
Oliver K

Reputation: 33

VS code problems suggestion in C

I am writing a HW for school, where I should implement a circular buffer and I ran into 2 things. VS Code says that:

  1. too few arguments in function call [8,21]
  2. expected a ';' [9,5]

But I'am quite sure, I have not made any mistake so far. I also don't know how to compile it, GCC won't take that. Makefile provided by school throws some error, but none regarding this issue.

I've got C/C++ extension form Microsoft [v1.2.2]. Are errors/problems handled by that one?

Here is the code queue.c:

#include "queue.h"

// TODO - your code
queue_t* create_queue(int capacity){
    queue_t * q;
    q->capacity = capacity;
    q->count = 0;
    q->arr = malloc(capacity*sizeof(int));
    if(q->arr == NULL){
        fprintf(stderr, "ERROR: cannot alocate enough memory!\n"); // here is the er#1
    }
    q->arr_end =(int*)q->arr + capacity * sizeof(int);
    return q; // er#2 occurs here
}

And here queue.h

#ifndef __QUEUE_H__
#define __QUEUE_H__

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

/* Queue structure which holds all necessary data */
typedef struct {
   // TODO - Include your data structure here
   int capacity; // the max # of elemetns, that can be stored
   int count; // # of elements in Q
   int * arr; // the array itself
   int * arr_end; // pointer to the end of arr (ie: *(arr+int*len))
   int * read; // position to read from; ie: HEAD
   int * write; // position to write form; ie: TAIL
} queue_t;

/* creates a new queue with a given size */
queue_t* create_queue(int capacity);

// ... 

#endif /* __QUEUE_H__ */

Output of the GCC for gcc queue.c

/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status

And this is the main.c as is from school:

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#include "queue.h"

/* allocate new integer with value a and add it to the queue */
void add(int a, queue_t *queue)
{
   int *p = (int*)malloc(sizeof(int));
   *p = a;
   bool ret = push_to_queue(queue, (void*)p);
   if (!ret) {
      // free memory on failure
      free(p);
   }
}

/* print the int value on pointer p */
void print_int(void *p)
{
   if (p != NULL) {
      printf("%d\n", *((int*)p));
   } else {
      printf("NULL\n");
   }
}

/* pop from the queue, print and free the element */
void pop(queue_t *queue)
{
   void *p = pop_from_queue(queue);
   print_int(p);
   free(p);
}

/* get i-th element and print it (do not remove them) */
void get(int idx, queue_t *queue)
{
   print_int(get_from_queue(queue, idx));
}

/*
 * TEST PROGRAM
 * - reads commands from stdin and executes them in the queue
 */
int main(int argc, char *argv[])
{
   int n;
   /* the tested queue */
   queue_t *queue;

   // read the size of the queue
   scanf("%d", &n);
   // create queue
   queue = create_queue(n);

   while (true) {
      char s[2];
      // read one command
      int ret = scanf("%1s", s);
      if (ret != 1) {
         break;
      }

      // add command
      if (s[0] == 'a') {
         int a;
         // read the argument of the command
         ret = scanf("%d", &a);
         if (ret != 1) {
            break;
         }
         add(a, queue);
         // remove command
      } else if (s[0] == 'r') {
         pop(queue);
         // get command
      } else if (s[0] == 'g') {
         int a;
         // read the argument of the command
         ret = scanf("%d", &a);
         if (ret != 1) {
            break;
         }
         get(a, queue);
      }
   }

   // remove rest of the elements in the queue
   while (get_queue_size(queue)) {
      void *p = pop_from_queue(queue);
      free(p);
   }

   // free memory
   delete_queue(queue);
   queue = NULL;

   // return 0 on succes
   return 0;
}

Upvotes: 2

Views: 178

Answers (1)

David Ranieri
David Ranieri

Reputation: 41017

You forget to reserve space for the queue:

queue_t * q = malloc(sizeof *q);

if (q != NULL)
{
    q->capacity = capacity;
    ...

Also

q->arr_end =(int*)q->arr + capacity * sizeof(int);

here you want (assuming that you want a pointer to the last element):

q->arr_end = q->arr + capacity - 1;

pointer arithmetic is done in terms of elements (not bytes)

Regarding your compile error, it seems that you forget to include the unit containing main, try with

gcc main.c queue.c

Upvotes: 2

Related Questions