I'm needing to code a function named display_lines, the parameter to this function is a char **p; I need help on getting it right, im getting implicit declarations and I don't understand why. Most of code is from my professor, a lot of it had been added by me. Mainly the read_lines and count_lines functions were part of this assignment and i believ ei have those done and correct. Heres the code and compiler errors.
util.c:17:5: warning: implicit declaration of function ‘count_lines’ [-Wimplicit-function-declaration]
util.c:20:2: warning: format not a string literal and no format arguments [-Wformat-security]
Code:
/*
Name: util.c
Purpose: Implementations of utilities declared in util.h
Author: Bill Slough
*/
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
#define INITIAL_CAPACITY 10 /* size of first string allocation */
void display_lines(char **p) {
int i;
char *line;
int numlines = count_lines(p);
for(i = 0; i <= numlines; i++) {
line = p[i];
printf(line);
}
}
int count_lines(char **p) {
int count;
int i;
for (i = 0; i != -1; i++) {
if(p[i] != NULL) {
count++;
}
else
i = -1;
}
return count;
}
char **read_lines(FILE *fp) {
int capacity;
int count;
char **lines;
char *line;
capacity = INITIAL_CAPACITY;
lines = safe_malloc(sizeof(char *) * capacity);
line = read_line(fp);
count = 0;
while(line != NULL) {
lines[count++] = line;
if (count == capacity) {
/* last available slot was used; get more space! */
capacity *= 2;
lines = safe_realloc(lines, capacity);
}
line = read_line(fp);
}
if ((count == 0)) { /* no characters read: at eof */
free(lines); /* release space held for string */
return NULL;
}
lines = safe_realloc(lines, count + 1);
lines[count] = NULL;
return lines;
}
/* Read and store one line of input from a given input file.
Return NULL if stream is at eof or there was a read error;
otherwise returns a pointer to a null-terminated string of the
correct size. The terminating newline character marking the end
of the line is not stored in the resulting string.
*/
char *read_line(FILE *fp) {
int capacity; /* maximum number of characters in the string */
int size; /* number of characters stored in the string */
int ch; /* most recent character read from the file */
int count; /* number of characters read from the file */
char *s; /* the string */
/* allocate space sufficient for "small" strings */
capacity = INITIAL_CAPACITY;
s = safe_malloc(capacity);
size = 0; /* so far, no characters have been stored */
count = 0; /* and no characters have been read from the current line */
while (((ch = getc(fp)) != '\n') && (ch != EOF)) {
count++;
s[size++] = ch;
if (size == capacity) {
/* last available slot was used; get more space! */
capacity *= 2;
s = safe_realloc(s, capacity);
}
}
if ((count == 0) && (ch == EOF)) { /* no characters read: at eof */
free(s); /* release space held for string */
return NULL;
}
/* Properly terminate the string and trim to the proper size */
s = safe_realloc(s, size + 1);
s[size] = '\0';
return s;
}
/* safely allocate a block of storage, aborting program if not possible */
void *safe_malloc(size_t size) {
void *p;
p = malloc(size);
if (p == NULL) {
fprintf(stderr, "error - out of space!");
exit(EXIT_FAILURE);
}
else
return p;
}
/* safely reallocate a block of storage, aborting program if not possible */
void *safe_realloc(void *ptr, size_t size) {
void *p;
p = realloc(ptr, size);
if (p == NULL) {
fprintf(stderr, "error - out of space!");
exit(EXIT_FAILURE);
}
else
return p;
}