Re: beginner's questions

From: SM Ryan (wyrmwif_at_tango-sierra-oscar-foxtrot-tango.fake.org)
Date: 02/18/05


Date: Fri, 18 Feb 2005 11:12:41 -0000

gruzdnev@gmail.com wrote:
# Hi all,
#
# I've started to program in C not long ago, and I've got some questions:
# (I work on Linux 2.4.22/Debian)
#
# 1. Why the "**var" construct is used? What are the cases when it is
# commonly needed? I'd like to read more about it, but there's nothing in
# K&R on this theme, AFAIR.

Simulate pass by name.
        int afgets(char **linevar,int size,FILE *fp) {
          char *line = malloc(size);
          if (fgets(line,size,fp)) {*linevar = line; return 1;}
          else {free(line); *linevar = 0; return 0;}
        }
        ...
        char *line;
        while (afgets(&line,20,stdin)) {
          doWahDiddy(line); free(line);
        }

Pointer chasing in linked lists. You'll need to draw some pictures to understand this
        typedef struct T{struct T *link; int data;} T;

        T *removeAll(T *head0,int data) {
          T *head = head0; T **current = &head;
          while (*current) {
            if ((*current)->data==data) {
              T *sacrafice = *current;
              *current = sacrafice->link;
              free(sacrafice);
            }else {
              current = &((*current)->link);
            }
          }
          return head;
        }
        
# 2. Suppose, I want to see the source code of the "fopen" function used
# by my system. Where do I have to look to learn it? Is it some header

Depends on your vendor. Some (like gnu) supply the source code, some don't.

# 3. Why is "___P" used in declaraion of functions like "main __P((int,
# char *[]));"? What is the sense of it? Does using it have any pluses?
# Where do I read about it (online, preferably)?

Probably somewhere it defines
        #define __P(x) x
or
        #define __P(x)

The first #define converts those declarations into ANSI-C function prototypes. The
second #define converts them into old style K+R function declarations. It lets you
use the same header file for two different styles of compilation.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I have no idea what you just said.
I get that alot.


Relevant Pages

  • Compiler error creating managed c++ directx app
    ... I'm trying to create a simple program using managed C++ and DirectX. ... Here is the source code I am using: ... did you forget to include a header file? ... "Microsoft.DirectX" to References in Solution Explorer, ...
    (microsoft.public.win32.programmer.directx.managed)
  • Re: [Lit.] Buffer overruns
    ... #defining the symbols within the source code you control you can inhibit ... any preprocessor overrides of those symbols. ... I prefer that the conflict be ... You can even automate it by creating a header file such as define_C.h ...
    (sci.crypt)
  • Re: #ifdef
    ... It may be defined by the user, or somewhere in a header file, ... out of a larger context such as the OS source code ... C code after preprocessing but before compiling. ... in tracking down issues with preprocessor symbols. ...
    (comp.lang.c)
  • Re: Thoughts on file organisation
    ... If your heather file mylib.h depends ... When you have a header file defining some project specific types and you use those types in function declarations, people will expect you include the header file defining them in your header file containing the function declarations. ...
    (comp.lang.c)
  • Suppose: All code in header files?
    ... logical necessity and actual worth of header file. ... believe C++ programs could be written without dividing source code between ... file with a function 'main' to seed the compilation process. ...
    (comp.lang.cpp)