gr******@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.