beginner's questions

Discussion in 'C Programming' started by gruzdnev@gmail.com, Feb 18, 2005.

  1. Guest

    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
    , Feb 18, 2005
    #1
    1. Advertising

  2. Michael Mair Guest

    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.


    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
    --
    E-Mail: Mine is a gmx dot de address.
    Michael Mair, Feb 18, 2005
    #2
    1. Advertising

  3. SM Ryan Guest

    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.
    SM Ryan, Feb 18, 2005
    #3
  4. <posted & mailed>

    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.


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



    >
    > Thanks,
    > K.Gruzdnev


    --
    Remove '.nospam' from e-mail address to reply by e-mail
    James McIninch, Feb 19, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave

    Simple beginner questions

    Dave, Aug 9, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    349
  2. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    570
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  3. Richard Thornley

    Beginner ASP.Net questions

    Richard Thornley, Feb 9, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    414
    Kevin Spencer
    Feb 9, 2005
  4. Rensjuh
    Replies:
    7
    Views:
    962
    Mabden
    Sep 2, 2004
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    211
    7stud --
    Apr 12, 2011
Loading...

Share This Page