Thread: Function Help

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    48

    Function Help

    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;
    }

  2. #2
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    Ok I fixed the first error with the implicit declaration. How do i fix the second error, I don't understand why Im getting it

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > util.c:20:2: warning: format not a string literal and no format arguments [-Wformat-security]
    Because
    printf(line);

    Because if line contains any % characters, then printf is going to go looking for arguments to format which don't exist.

    Use
    printf("%s",line);

    or
    puts(line);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 09-07-2012, 04:35 AM
  2. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  3. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM