beginner's questions

G

gruzdnev

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.

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
file? (/usr/include/stdio.h contains only the pre-declaration of it) Is
it an OS-specific question?

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)?

Thanks,
K.Gruzdnev
 
M

Michael Mair

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.

What do you mean?
T **var;
declares var a pointer to a pointer to type T.
**var
yields a value of type T (if everything is correctly
initialised/assigned).
One example is
#include <stdio.h>

int main (int argc, char **argv)
{
char **p;
int i;

p = argv;

for (i=0; i<argc; i++, p++)
printf("%c\n", **p); /* print the first character of each
** parameter. */

return 0;
}
(Untested).

Another is:

char *p = NULL;
change_p (&p);

where change_p() has a char ** parameter.
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
file? (/usr/include/stdio.h contains only the pre-declaration of it) Is
it an OS-specific question?

The standard library functions often have to use system-specific,
non-portable code and often do use non-standard stuff for
speed-up. So, it depends on your compiler, library and OS.
I would indeed start with an OS-specific newsgroup or forum.

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)?

Note that leading underscores belong to the implementation's namespace,
so the "___P" probably is something you have not to care about.
Probably it is a macro.
The way it is used, it could be a macro which provides C89 type
prototypes if __STDC__ is defined and K&R-C type function declarations
else. But these are just wild guesses.


Cheers
Michael
 
S

SM Ryan

(e-mail address removed) 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.
 
J

James McIninch

<posted & mailed>

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.

**var is simply a pointer to a pointer to something.

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
file? (/usr/include/stdio.h contains only the pre-declaration of it) Is
it an OS-specific question?

You need to install the C library source code (in your case, GNU libc), if
available (in your case, it's available but you probably don't have it
installed). The header files contain no code, just macros, prototypes, and
data structures.
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)?

___P is a macro. Check the header files for a definition for it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top