I wrote this header file for a another main program. This file declares a struct_buffer struct that is used by the main function (somewhere else).
What I am trying to do is that when someone initialises a buffer with the buffer_init function, a pointer to the buffer is returned. The buffer would contain, an array of pointers, the current number of pointer in the buffer(size), and the file-pointer to a file to which the pointers stored in the buffer will be dumped.
The main program will call the add_to_buffer() function to add pointers to the buffer. This in turn calls the buffer_dump() function to dump the pointers in a file specified by the buffer->fp. At the end, I will call buffer_close() to close the file. However compiling it gives me several errors that I am unable to understand and get rid of.
The following is a code of header file in C, that I am trying to compile and it is giving me errors that I can't understand.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PTRS 1000
struct struct_buffer
{
int size;
File *fp;
unsigned long long ptrs[MAX_PTRS];
};
//The struct_buffer contains the size which is the number of pointers in ptrs, fp is the file pointer to the file that buffer will write
struct struct_buffer*
buffer_init(char *name)
{
struct struct_buffer *buf = malloc(sizeof(struct struct_buffer));
buf->size = 0;
buf->fp = fopen(name,"w");
return buf;
}
//This function dumps the ptrs to the file.
void
buffer_dump (struct struct_buffer *buf)
{
int ctr=0;
for(ctr=0; ctr < buf->size ; ctr++)
{
fprintf(buf->fp, "%llx\n",buf->ptrs[ctr]);
}
}
//this function adds a pointer to the buffer
void
add_to_buffer (struct struct_buffer *buf, void *ptr)
{
if(buf->size >= MAX_PTRS)
{
buffer_dump(buf);
buf->size=0;
}
buf->ptrs[(buf->size)++] = (unsigned long long)ptr;
}
//this functions closes the file pointer
void
buffer_close (struct struct_buffer *buf)
{
fclose(buf->fp);
}
The above code on compilation gives me the following errors. I could not understand the problem. Please explain to me what the trouble is.
buffer.h:10: error: expected specifier-qualifier-list before 'File'
buffer.h: In function 'buffer_init':
buffer.h:19: error: 'struct struct_buff开发者_JAVA技巧er' has no member named 'fp'
buffer.h: In function 'buffer_dump':
buffer.h:29: error: 'struct struct_buffer' has no member named 'fp'
buffer.h:29: error: 'struct struct_buffer' has no member named 'ptrs'
buffer.h: In function 'add_to_buffer':
buffer.h:40: error: 'struct struct_buffer' has no member named 'ptrs'
buffer.h: In function 'buffer_close':
buffer.h:46: error: 'struct struct_buffer' has no member named 'fp'
File
is not defined. The correct type you are looking for is FILE
.
struct struct_buffer
{
int size;
FILE *fp; // <---------
unsigned long long ptrs[MAX_PTRS];
};
The rest of your errors appear to be a product of this error, so this should be your only fix.
精彩评论