Thread: Basic program design, passing pointer to a function

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Note that simply calling exit from a function is discouraged. The function should generally return success or failure and the caller should take appropriate action.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    44
    The function should generally return success or failure and the caller should take appropriate action.
    Although it is often better to return failure to the calling code if it's not a completely catastrophic failure.
    Thanks, I'll try to move that stuff to main().

    Do you understand what static actually does?
    I think it makes sure that the source of whatever kind cannot be altered. Or was that const ... But I haven't yet rtfm of either static or const.

    edit:
    In computer programming, static variables data value persists for the life of the running process and typically have a broader scope than other variables. Their values may be set at run-time or may change subtly during program execution.
    Reads somewhat like global variables. But anywho, I'm of to the documentation.
    Last edited by heras; 04-01-2008 at 07:00 AM.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    static on variables make them act like globals except with more limited scope (if they are global in the file, they will only be visible within the file, if the static is in a function then the variable is only visible in the function - so no other function can "mess" with the variable).

    But for functions, it has the same meaning as static on a file-global variable: it hides the function from other modules in the project. Since this is a single-file-project, it makes little difference here - I was just curious as to why you decided to make one static and the other one not.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    44
    Quote Originally Posted by matsp View Post
    Since this is a single-file-project, it makes little difference here - I was just curious as to why you decided to make one static and the other one not.
    Thanks for the explanation, I think I understand
    Actually, the next thing I wanted to try is putting the functions in separate files, so by then it may be more relevant to consider this.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    44
    And after asking twice, I notice that I still haven't answered your question, sorry:
    I was just curious as to why you decided to make one static and the other one not.
    My implementation comes from this FAQ without fully understanding it.
    I think I understand
    Turns out I'm wrong. The below code produces an "interesting" compiler output:
    Code:
    heras@vhbh:~/code/c/readfile$ gcc -Wall readfile.c open_file.c read_line.c -o read
    open_file.h:1: warning: ‘open_file’ used but never defined
    open_file.c:7: warning: ‘open_file’ defined but not used
    /tmp/ccMsUG3J.o: In function `main':
    readfile.c:(.text+0x61): undefined reference to `open_file'
    collect2: ld returned 1 exit status
    Code:
    /*
    		readfile2.3.c
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "open_file.h"
    #include "read_line.h"
    
    int main(int argc, char *argv[])
    {
    	if (argc != 2) {
    		fprintf(stderr, "%s usage: <address>\n", argv[0]);
    		exit (EXIT_FAILURE);
    	}
    
    	FILE *open_fd;
    	open_fd = open_file(argv[1], "r");
    
    	int line_length = 73;
    	char *line_pointer;
    	line_pointer = (char *)malloc(line_length);
    	while (read_line(open_fd, line_length, line_pointer) != EOF) {
    		printf("%s\n", line_pointer); //FIXME: does not print last line
    	}
    	free(line_pointer);
    	return (EXIT_SUCCESS);
    }
    open_file.h
    Code:
    static FILE *open_file(char *, char *);
    open_file.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include "open_file.h"
    
    static FILE *open_file(char *file, char *mode)
    {
    	FILE *fp = fopen(file, mode);
    	if (fp == NULL) {
    		perror("fopen");
    		exit (EXIT_FAILURE); //FIXME: return failure
    	}
    	return (fp);
    }
    read_line.h
    Code:
    int read_line(FILE *, int, char *);
    read_line.c
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include "read_line.h"
    
    //FIXME: static int read_line() ?
    int read_line(FILE *file, int line_length, char *line_pointer)
    {
    	int i = 0, c;
    	while ((c = fgetc(file) ) != EOF) {
    		if (isalnum(c) != 0 || ispunct(c) != 0 || c == ' ') {
    			*line_pointer = c;
    			line_pointer++;
    		}
    		if (i == (line_length - 2)) {
    			break;
    		}
    		i++;
    	}
    	*line_pointer = '\0';
    
    	return (c);
    }
    Removing static from both open_file.h and open_file.c makes the "interesting" part go away.
    Note1 that some of the badness has not been fixed, but it will be.
    Note2 that matsp rules (together with all the other helpfull folks in these parts).

    ps:I don't have a question about this code, it's just a follow-up.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is EXACTLY what the expected effect of static gives - it hides the function from other compilation units (that is, other .c files).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Replies: 9
    Last Post: 12-25-2007, 05:01 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM