Newbie needs help correcting code

Discussion in 'C Programming' started by zotkara, Jan 3, 2006.

  1. zotkara

    zotkara Guest

    Hello. I need help correcting the following C source code.

    /* coder.c */
    /* Usage: coder [filename] [action]
    [action]
    D decrypt
    C crypt
    */

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define TITLE "coder\nAbout: encrypts or decrypts file."
    #define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
    #define FILE_ERROR -2
    #define DECRYPT 'd'

    void start(int argc,char argv[]);
    int encode_character(int ch,int val);
    int decode_character(int ch,int val);

    typedef crypt
    {
    int rv; // Recovery value
    int ch; // Channel
    unsigned int ctr; // Counter
    int val; // Encrypt value
    char buffer[257]; // Buffer
    FILE *fh; // File handle
    }engine0={1,0,0,5};

    int main(int argc,char *argv[])
    {
    start(argc,argv); // Start engine

    }

    void start(int argc,char argv[])
    {
    crypt *engine1=&engine0; // Turn on engine
    if(argc!=3)
    {
    printf("%s\n%s\n",TITLE,USAGE);
    }

    /* Note that content of if(content) is case-sensitive */
    else if(argv[2]==DECRYPT)
    {
    (engine1->fh)=fopen(argv[1],"r"); // Opens file
    if((engine1->fh)<=0)
    {
    printf("\n\nError opening file...");
    (engine1->rv)=FILE_ERROR;
    }
    }
    }
    EOF

    The following is the error message output by GNU gcc compiler in Slackware
    10.2 in bash.

    error: syntax error before '{' token
    coder.c:29: error: parse error before '}' token
    coder.c:29: error: parse error before ',' token
    coder.c:29: warning: excess elements in scalar initializer
    coder.c:29: warning: (near initialization for `engine0')
    docs@pyenos:~/work_and_play/c++_learn$ gcc coder.c -o coder
    coder.c:22: error: syntax error before '{' token
    coder.c:29: error: parse error before '}' token
    coder.c:29: warning: excess elements in scalar initializer
    coder.c:29: warning: (near initialization for `engine0')
    coder.c:29: warning: excess elements in scalar initializer
    coder.c:29: warning: (near initialization for `engine0')
    coder.c:29: warning: excess elements in scalar initializer
    coder.c:29: warning: (near initialization for `engine0')
    coder.c:29: warning: data definition has no type or storage class
    coder.c: In function `main':
    coder.c:33: warning: passing arg 2 of `start' from incompatible pointer type
    coder.c: In function `start':
    coder.c:39: error: `crypt' undeclared (first use in this function)
    coder.c:39: error: (Each undeclared identifier is reported only once
    coder.c:39: error: for each function it appears in.)
    coder.c:39: error: `engine1' undeclared (first use in this function)
    coder.c:48: warning: passing arg 1 of `fopen' makes pointer from integer
    EOF

    Please help me correcting this code as I am a newbie. Thanks in advance.


    Zot
     
    zotkara, Jan 3, 2006
    #1
    1. Advertising

  2. zotkara

    Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    zotkara wrote:
    > Hello. I need help correcting the following C source code.
    >
    > /* coder.c */
    > /* Usage: coder [filename] [action]
    > [action]
    > D decrypt
    > C crypt
    > */
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > #define TITLE "coder\nAbout: encrypts or decrypts file."
    > #define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
    > #define FILE_ERROR -2
    > #define DECRYPT 'd'
    >
    > void start(int argc,char argv[]);
    > int encode_character(int ch,int val);
    > int decode_character(int ch,int val);
    >
    > typedef crypt


    what sort of type is crypt? Yes, you typedef'ed it, but what did you
    typedef it to?

    > {
    > int rv; // Recovery value
    > int ch; // Channel
    > unsigned int ctr; // Counter
    > int val; // Encrypt value
    > char buffer[257]; // Buffer
    > FILE *fh; // File handle
    > }engine0={1,0,0,5};


    [snip]

    - --

    Lew Pitcher, IT Specialist, Enterprise Data Systems
    Enterprise Technology Solutions, TD Bank Financial Group

    (Opinions expressed here are my own, not my employer's)
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.4 (MingW32)

    iD8DBQFDut5WagVFX4UWr64RArzUAJ91EcAdn+QYa3J4Z5sDGDjjMZdTWQCgv2+B
    aMnGBLwDqYvcvgvsypv5v4s=
    =Cmpy
    -----END PGP SIGNATURE-----
     
    Lew Pitcher, Jan 3, 2006
    #2
    1. Advertising

  3. zotkara

    Mike Wahler Guest

    "zotkara" <> wrote in message
    news:p...
    > Hello. I need help correcting the following C source code.
    >
    > /* coder.c */
    > /* Usage: coder [filename] [action]
    > [action]
    > D decrypt
    > C crypt
    > */
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > #define TITLE "coder\nAbout: encrypts or decrypts file."
    > #define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt
    > file\n\tc\tCrypt file"
    > #define FILE_ERROR -2
    > #define DECRYPT 'd'
    >
    > void start(int argc,char argv[]);
    > int encode_character(int ch,int val);
    > int decode_character(int ch,int val);
    >
    > typedef crypt


    typedef struct crypt

    > {
    > int rv; // Recovery value
    > int ch; // Channel
    > unsigned int ctr; // Counter
    > int val; // Encrypt value
    > char buffer[257]; // Buffer
    > FILE *fh; // File handle
    > }engine0={1,0,0,5};
    >
    > int main(int argc,char *argv[])
    > {
    > start(argc,argv); // Start engine
    >
    > }
    >
    > void start(int argc,char argv[])
    > {
    > crypt *engine1=&engine0; // Turn on engine
    > if(argc!=3)
    > {
    > printf("%s\n%s\n",TITLE,USAGE);
    > }
    >
    > /* Note that content of if(content) is case-sensitive */
    > else if(argv[2]==DECRYPT)
    > {
    > (engine1->fh)=fopen(argv[1],"r"); // Opens file
    > if((engine1->fh)<=0)
    > {
    > printf("\n\nError opening file...");
    > (engine1->rv)=FILE_ERROR;
    > }
    > }
    > }
    > EOF
    >
    > The following is the error message output by GNU gcc compiler in Slackware
    > 10.2 in bash.
    >
    > error: syntax error before '{' token
    > coder.c:29: error: parse error before '}' token
    > coder.c:29: error: parse error before ',' token
    > coder.c:29: warning: excess elements in scalar initializer
    > coder.c:29: warning: (near initialization for `engine0')
    > docs@pyenos:~/work_and_play/c++_learn$ gcc coder.c -o coder
    > coder.c:22: error: syntax error before '{' token
    > coder.c:29: error: parse error before '}' token
    > coder.c:29: warning: excess elements in scalar initializer
    > coder.c:29: warning: (near initialization for `engine0')
    > coder.c:29: warning: excess elements in scalar initializer
    > coder.c:29: warning: (near initialization for `engine0')
    > coder.c:29: warning: excess elements in scalar initializer
    > coder.c:29: warning: (near initialization for `engine0')
    > coder.c:29: warning: data definition has no type or storage class
    > coder.c: In function `main':
    > coder.c:33: warning: passing arg 2 of `start' from incompatible pointer
    > type
    > coder.c: In function `start':
    > coder.c:39: error: `crypt' undeclared (first use in this function)
    > coder.c:39: error: (Each undeclared identifier is reported only once
    > coder.c:39: error: for each function it appears in.)
    > coder.c:39: error: `engine1' undeclared (first use in this function)
    > coder.c:48: warning: passing arg 1 of `fopen' makes pointer from integer
    > EOF
    >
    > Please help me correcting this code as I am a newbie. Thanks in advance.


    OK now it compiles. Whether it does what you want I have no idea.

    /* coder.c */
    /* Usage: coder [filename] [action]
    [action]
    D decrypt
    C crypt
    */

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define TITLE "coder\nAbout: encrypts or decrypts file."
    #define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt
    file\n\tc\tCrypt file"
    #define FILE_ERROR -2
    #define DECRYPT 'd'

    //void start(int argc,char argv[]);
    void start(int argc,char *argv[]);
    int encode_character(int ch,int val);
    int decode_character(int ch,int val);

    typedef struct
    {
    int rv; // Recovery value
    int ch; // Channel
    unsigned int ctr; // Counter
    int val; // Encrypt value
    char buffer[257]; // Buffer
    FILE *fh; // File handle
    } crypt ;

    crypt engine0={1,0,0,5};

    int main(int argc,char *argv[])
    {
    start(argc,argv); // Start engine

    }

    //void start(int argc,char argv[])
    void start(int argc,char *argv[])

    {
    crypt *engine1=&engine0; // Turn on engine
    if(argc!=3)
    {
    printf("%s\n%s\n",TITLE,USAGE);
    }

    /* Note that content of if(content) is case-sensitive */
    // else if(argv[2]==DECRYPT)
    else if(*argv[2]==DECRYPT)
    {
    (engine1->fh)=fopen(argv[1],"r"); // Opens file
    if((engine1->fh)<=0)
    {
    printf("\n\nError opening file...");
    (engine1->rv)=FILE_ERROR;
    }
    }
    }

    -Mike
     
    Mike Wahler, Jan 3, 2006
    #3
  4. zotkara a écrit :
    > Hello. I need help correcting the following C source code.

    <...>
    See my comments (-ed-). I have just made your code compilable. Other
    errors may exist.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define TITLE "coder\nAbout: encrypts or decrypts file."
    #define USAGE "Usage: coder [filename]
    [action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
    #define FILE_ERROR -2
    #define DECRYPT 'd'

    /* -ed- missing 'struct'. It's an object : 'typedef' removed */
    struct crypt
    {
    int rv; // Recovery value
    int ch; // Channel
    unsigned int ctr; // Counter
    int val; // Encrypt value
    char buffer[257]; // Buffer
    FILE *fh; // File handle
    }
    engine0 =
    {
    1, 0, 0, 5
    };

    static void start(int argc, char argv[])
    {
    /* -ed- missing 'struct' */
    struct crypt *engine1 = &engine0; // Turn on engine
    if (argc != 3)
    {
    printf("%s\n%s\n", TITLE, USAGE);
    }

    /* Note that content of if(content) is case-sensitive */
    else if (argv[2] == DECRYPT)
    {
    (engine1->fh) = fopen(argv[1], "r"); // Opens file
    if ((engine1->fh) <= 0)
    {
    printf("\n\nError opening file...");
    (engine1->rv) = FILE_ERROR;
    }
    }
    }

    int main(int argc, char *argv[])
    {
    start(argc, argv); // Start engine

    }

    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Jan 3, 2006
    #4
  5. zotkara a écrit :
    > Hello. I need help correcting the following C source code.

    <...>
    See my comments (-ed-). I have just made your code compilable. Other
    errors may exist.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define TITLE "coder\nAbout: encrypts or decrypts file."
    #define USAGE "Usage: coder [filename]
    [action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
    #define FILE_ERROR -2
    #define DECRYPT 'd'

    /* -ed- missing 'struct'. It's an object : 'typedef' removed */
    struct crypt
    {
    int rv; // Recovery value
    int ch; // Channel
    unsigned int ctr; // Counter
    int val; // Encrypt value
    char buffer[257]; // Buffer
    FILE *fh; // File handle
    }
    engine0 =
    {
    1, 0, 0, 5
    };

    /* -ed- missing '*' before argv... */
    static void start(int argc, char *argv[])
    {
    /* -ed- missing 'struct' */
    struct crypt *engine1 = &engine0; // Turn on engine
    if (argc != 3)
    {
    printf("%s\n%s\n", TITLE, USAGE);
    }

    /* Note that content of if(content) is case-sensitive */

    /* -ed- missing [0] */
    else if (argv[2][0] == DECRYPT)
    {
    (engine1->fh) = fopen(argv[1], "r"); // Opens file
    if ((engine1->fh) <= 0)
    {
    printf("\n\nError opening file...");
    (engine1->rv) = FILE_ERROR;
    }
    }
    }

    int main(int argc, char *argv[])
    {
    start(argc, argv); // Start engine

    }

    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Jan 3, 2006
    #5
  6. zotkara

    zotkara Guest

    Thank you very much, MKW

    ..


    On Tue, 03 Jan 2006 20:43:55 +0000, Mike Wahler wrote:

    >
    > "zotkara" <> wrote in message
    > news:p...
    >> Hello. I need help correcting the following C source code.
    >>
    >> /* coder.c */
    >> /* Usage: coder [filename] [action]
    >> [action]
    >> D decrypt
    >> C crypt
    >> */
    >>
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >> #include <string.h>
    >>
    >> #define TITLE "coder\nAbout: encrypts or decrypts file."
    >> #define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt
    >> file\n\tc\tCrypt file"
    >> #define FILE_ERROR -2
    >> #define DECRYPT 'd'
    >>
    >> void start(int argc,char argv[]);
    >> int encode_character(int ch,int val);
    >> int decode_character(int ch,int val);
    >>
    >> typedef crypt

    >
    > typedef struct crypt
    >
    >> {
    >> int rv; // Recovery value
    >> int ch; // Channel
    >> unsigned int ctr; // Counter
    >> int val; // Encrypt value
    >> char buffer[257]; // Buffer
    >> FILE *fh; // File handle
    >> }engine0={1,0,0,5};
    >>
    >> int main(int argc,char *argv[])
    >> {
    >> start(argc,argv); // Start engine
    >>
    >> }
    >>
    >> void start(int argc,char argv[])
    >> {
    >> crypt *engine1=&engine0; // Turn on engine
    >> if(argc!=3)
    >> {
    >> printf("%s\n%s\n",TITLE,USAGE);
    >> }
    >>
    >> /* Note that content of if(content) is case-sensitive */
    >> else if(argv[2]==DECRYPT)
    >> {
    >> (engine1->fh)=fopen(argv[1],"r"); // Opens file
    >> if((engine1->fh)<=0)
    >> {
    >> printf("\n\nError opening file...");
    >> (engine1->rv)=FILE_ERROR;
    >> }
    >> }
    >> }
    >> EOF
    >>
    >> The following is the error message output by GNU gcc compiler in Slackware
    >> 10.2 in bash.
    >>
    >> error: syntax error before '{' token
    >> coder.c:29: error: parse error before '}' token
    >> coder.c:29: error: parse error before ',' token
    >> coder.c:29: warning: excess elements in scalar initializer
    >> coder.c:29: warning: (near initialization for `engine0')
    >> docs@pyenos:~/work_and_play/c++_learn$ gcc coder.c -o coder
    >> coder.c:22: error: syntax error before '{' token
    >> coder.c:29: error: parse error before '}' token
    >> coder.c:29: warning: excess elements in scalar initializer
    >> coder.c:29: warning: (near initialization for `engine0')
    >> coder.c:29: warning: excess elements in scalar initializer
    >> coder.c:29: warning: (near initialization for `engine0')
    >> coder.c:29: warning: excess elements in scalar initializer
    >> coder.c:29: warning: (near initialization for `engine0')
    >> coder.c:29: warning: data definition has no type or storage class
    >> coder.c: In function `main':
    >> coder.c:33: warning: passing arg 2 of `start' from incompatible pointer
    >> type
    >> coder.c: In function `start':
    >> coder.c:39: error: `crypt' undeclared (first use in this function)
    >> coder.c:39: error: (Each undeclared identifier is reported only once
    >> coder.c:39: error: for each function it appears in.)
    >> coder.c:39: error: `engine1' undeclared (first use in this function)
    >> coder.c:48: warning: passing arg 1 of `fopen' makes pointer from integer
    >> EOF
    >>
    >> Please help me correcting this code as I am a newbie. Thanks in advance.

    >
    > OK now it compiles. Whether it does what you want I have no idea.
    >
    > /* coder.c */
    > /* Usage: coder [filename] [action]
    > [action]
    > D decrypt
    > C crypt
    > */
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > #define TITLE "coder\nAbout: encrypts or decrypts file."
    > #define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt
    > file\n\tc\tCrypt file"
    > #define FILE_ERROR -2
    > #define DECRYPT 'd'
    >
    > //void start(int argc,char argv[]);
    > void start(int argc,char *argv[]);
    > int encode_character(int ch,int val);
    > int decode_character(int ch,int val);
    >
    > typedef struct
    > {
    > int rv; // Recovery value
    > int ch; // Channel
    > unsigned int ctr; // Counter
    > int val; // Encrypt value
    > char buffer[257]; // Buffer
    > FILE *fh; // File handle
    > } crypt ;
    >
    > crypt engine0={1,0,0,5};
    >
    > int main(int argc,char *argv[])
    > {
    > start(argc,argv); // Start engine
    >
    > }
    >
    > //void start(int argc,char argv[])
    > void start(int argc,char *argv[])
    >
    > {
    > crypt *engine1=&engine0; // Turn on engine
    > if(argc!=3)
    > {
    > printf("%s\n%s\n",TITLE,USAGE);
    > }
    >
    > /* Note that content of if(content) is case-sensitive */
    > // else if(argv[2]==DECRYPT)
    > else if(*argv[2]==DECRYPT)
    > {
    > (engine1->fh)=fopen(argv[1],"r"); // Opens file
    > if((engine1->fh)<=0)
    > {
    > printf("\n\nError opening file...");
    > (engine1->rv)=FILE_ERROR;
    > }
    > }
    > }
    >
    > -Mike
     
    zotkara, Jan 3, 2006
    #6
  7. zotkara

    zotkara Guest

    Thank you very much EMD

    ..
    On Tue, 03 Jan 2006 21:58:07 +0100, Emmanuel Delahaye wrote:

    > zotkara a écrit :
    >> Hello. I need help correcting the following C source code.

    > <...>
    > See my comments (-ed-). I have just made your code compilable. Other
    > errors may exist.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > #define TITLE "coder\nAbout: encrypts or decrypts file."
    > #define USAGE "Usage: coder [filename]
    > [action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
    > #define FILE_ERROR -2
    > #define DECRYPT 'd'
    >
    > /* -ed- missing 'struct'. It's an object : 'typedef' removed */
    > struct crypt
    > {
    > int rv; // Recovery value
    > int ch; // Channel
    > unsigned int ctr; // Counter
    > int val; // Encrypt value
    > char buffer[257]; // Buffer
    > FILE *fh; // File handle
    > }
    > engine0 =
    > {
    > 1, 0, 0, 5
    > };
    >
    > /* -ed- missing '*' before argv... */
    > static void start(int argc, char *argv[])
    > {
    > /* -ed- missing 'struct' */
    > struct crypt *engine1 = &engine0; // Turn on engine
    > if (argc != 3)
    > {
    > printf("%s\n%s\n", TITLE, USAGE);
    > }
    >
    > /* Note that content of if(content) is case-sensitive */
    >
    > /* -ed- missing [0] */
    > else if (argv[2][0] == DECRYPT)
    > {
    > (engine1->fh) = fopen(argv[1], "r"); // Opens file
    > if ((engine1->fh) <= 0)
    > {
    > printf("\n\nError opening file...");
    > (engine1->rv) = FILE_ERROR;
    > }
    > }
    > }
    >
    > int main(int argc, char *argv[])
    > {
    > start(argc, argv); // Start engine
    >
    > }
     
    zotkara, Jan 3, 2006
    #7
  8. zotkara

    zotkara Guest

    Re: Thank you very much EMD

    /* -ed- missing [0] */
    else if (argv[2][0] == DECRYPT)
    {

    Why do I need to put argv[2][0] when argv[2][0]==argv[2];? Thank you.
     
    zotkara, Jan 3, 2006
    #8
  9. Re: Thank you very much EMD

    zotkara said:

    > /* -ed- missing [0] */
    > else if (argv[2][0] == DECRYPT)
    > {
    >
    > Why do I need to put argv[2][0] when argv[2][0]==argv[2];? Thank you.


    Assuming this is the second argument to main:

    argv[2] has type char *.
    argv[2][0] has type char.
    Therefore, argv[2][0] cannot, as you claim, have the same value as argv[2].

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jan 3, 2006
    #9
  10. zotkara

    zotkara Guest

    Re: Thank you very much EMD

    On Wed, 04 Jan 2006 08:11:16 +1100, zotkara wrote:

    > /* -ed- missing [0] */
    > else if (argv[2][0] == DECRYPT)
    > {
    >
    > Why do I need to put argv[2][0] when argv[2][0]==argv[2];? Thank you.


    Sorry for multi-posting. Maybe because arv[2][(0+int line_entered)] when
    line_entered from screen after the first scan exceeds one then it becomes
    an issue, where argv[2][(0+n)]!=argv[2]. I think in this particular code
    in the context of the program code it produces no direct errors but it is
    not a correct way of coding, so I guess your point is valid.
     
    zotkara, Jan 3, 2006
    #10
  11. zotkara

    zotkara Guest

    Thank you very much LWP

    ..
    On Tue, 03 Jan 2006 15:28:06 -0500, Lew Pitcher wrote:

    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > zotkara wrote:
    >> Hello. I need help correcting the following C source code.
    >>
    >> /* coder.c */
    >> /* Usage: coder [filename] [action]
    >> [action]
    >> D decrypt
    >> C crypt
    >> */
    >>
    >> #include <stdio.h>
    >> #include <stdlib.h>
    >> #include <string.h>
    >>
    >> #define TITLE "coder\nAbout: encrypts or decrypts file."
    >> #define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
    >> #define FILE_ERROR -2
    >> #define DECRYPT 'd'
    >>
    >> void start(int argc,char argv[]);
    >> int encode_character(int ch,int val);
    >> int decode_character(int ch,int val);
    >>
    >> typedef crypt

    >
    > what sort of type is crypt? Yes, you typedef'ed it, but what did you
    > typedef it to?
    >
    >> {
    >> int rv; // Recovery value
    >> int ch; // Channel
    >> unsigned int ctr; // Counter
    >> int val; // Encrypt value
    >> char buffer[257]; // Buffer
    >> FILE *fh; // File handle
    >> }engine0={1,0,0,5};

    >
    > [snip]
    >
    > - --
    >
    > Lew Pitcher, IT Specialist, Enterprise Data Systems
    > Enterprise Technology Solutions, TD Bank Financial Group
    >
    > (Opinions expressed here are my own, not my employer's)
    > -----BEGIN PGP SIGNATURE-----
    > Version: GnuPG v1.2.4 (MingW32)
    >
    > iD8DBQFDut5WagVFX4UWr64RArzUAJ91EcAdn+QYa3J4Z5sDGDjjMZdTWQCgv2+B
    > aMnGBLwDqYvcvgvsypv5v4s=
    > =Cmpy
    > -----END PGP SIGNATURE-----
     
    zotkara, Jan 3, 2006
    #11
  12. zotkara

    zotkara Guest

    Re: Thank you very much EMD

    i think *argv[] cannot be an array since it is a pointer. so i agree
    with you if you'd said that argv[] is char* type. since both argv[2]
    and arv[2][0] point to the same address of an array, and was subjected to
    the same precondition for pointer initialization, i thought both argv[2]
    and argv[2][0] would be of char type. your thoughts RCH?

    On Tue, 03 Jan 2006 21:14:32 +0000, Richard Heathfield wrote:

    > zotkara said:
    >
    >> /* -ed- missing [0] */
    >> else if (argv[2][0] == DECRYPT)
    >> {
    >>
    >> Why do I need to put argv[2][0] when argv[2][0]==argv[2];? Thank you.

    >
    > Assuming this is the second argument to main:
    >
    > argv[2] has type char *.
    > argv[2][0] has type char.
    > Therefore, argv[2][0] cannot, as you claim, have the same value as argv[2].
     
    zotkara, Jan 3, 2006
    #12
  13. zotkara

    zotkara Guest

    Collection of fixed source of parent thread

    /* Fixed by MKW */
    /* coder.c */
    /* Usage: coder [filename] [action]
    [action]
    D decrypt
    C crypt
    */

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define TITLE "coder\nAbout: encrypts or decrypts file."
    #define USAGE "Usage: coder [filename] [action]\n\t[action]\n\td\tDecrypt
    file\n\tc\tCrypt file"
    #define FILE_ERROR -2
    #define DECRYPT 'd'

    //void start(int argc,char argv[]);
    void start(int argc,char *argv[]); // Good point Mike! ;)
    int encode_character(int ch,int val);
    int decode_character(int ch,int val);

    typedef struct
    {
    int rv; // Recovery value
    int ch; // Channel
    unsigned int ctr; // Counter
    int val; // Encrypt value
    char buffer[257]; // Buffer
    FILE *fh; // File handle
    } crypt ;

    crypt engine0={1,0,0,5};

    int main(int argc,char *argv[])
    {
    start(argc,argv); // Start engine

    }

    //void start(int argc,char argv[])
    void start(int argc,char *argv[])

    {
    crypt *engine1=&engine0; // Turn on engine
    if(argc!=3)
    {
    printf("%s\n%s\n",TITLE,USAGE);
    }

    /* Note that content of if(content) is case-sensitive */
    // else if(argv[2]==DECRYPT)
    else if(*argv[2]==DECRYPT)
    {
    (engine1->fh)=fopen(argv[1],"r"); // Opens file
    if((engine1->fh)<=0)
    {
    printf("\n\nError opening file...");
    (engine1->rv)=FILE_ERROR;
    }
    }
    }

    /* Fixed by EMD */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define TITLE "coder\nAbout: encrypts or decrypts file."
    #define USAGE "Usage: coder [filename]
    [action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
    #define FILE_ERROR -2
    #define DECRYPT 'd'

    /* -ed- missing 'struct'. It's an object : 'typedef' removed */
    struct crypt
    {
    int rv; // Recovery value
    int ch; // Channel
    unsigned int ctr; // Counter
    int val; // Encrypt value
    char buffer[257]; // Buffer
    FILE *fh; // File handle
    }
    engine0 =
    {
    1, 0, 0, 5
    };

    static void start(int argc, char argv[])
    {
    /* -ed- missing 'struct' */
    struct crypt *engine1 = &engine0; // Turn on engine
    if (argc != 3)
    {
    printf("%s\n%s\n", TITLE, USAGE);
    }

    /* Note that content of if(content) is case-sensitive */
    else if (argv[2] == DECRYPT)
    {
    (engine1->fh) = fopen(argv[1], "r"); // Opens file
    if ((engine1->fh) <= 0)
    {
    printf("\n\nError opening file...");
    (engine1->rv) = FILE_ERROR;
    }
    }
    }

    int main(int argc, char *argv[])
    {
    start(argc, argv); // Start engine

    }

    /* Fixed by EMD */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define TITLE "coder\nAbout: encrypts or decrypts file."
    #define USAGE "Usage: coder [filename]
    [action]\n\t[action]\n\td\tDecrypt file\n\tc\tCrypt file"
    #define FILE_ERROR -2
    #define DECRYPT 'd'

    /* -ed- missing 'struct'. It's an object : 'typedef' removed */
    struct crypt
    {
    int rv; // Recovery value
    int ch; // Channel
    unsigned int ctr; // Counter
    int val; // Encrypt value
    char buffer[257]; // Buffer
    FILE *fh; // File handle
    }
    engine0 =
    {
    1, 0, 0, 5
    };

    /* -ed- missing '*' before argv... */
    static void start(int argc, char *argv[])
    {
    /* -ed- missing 'struct' */
    struct crypt *engine1 = &engine0; // Turn on engine
    if (argc != 3)
    {
    printf("%s\n%s\n", TITLE, USAGE);
    }

    /* Note that content of if(content) is case-sensitive */

    /* -ed- missing [0] */
    else if (argv[2][0] == DECRYPT)
    {
    (engine1->fh) = fopen(argv[1], "r"); // Opens file
    if ((engine1->fh) <= 0)
    {
    printf("\n\nError opening file...");
    (engine1->rv) = FILE_ERROR;
    }
    }
    }

    int main(int argc, char *argv[])
    {
    start(argc, argv); // Start engine

    }
     
    zotkara, Jan 3, 2006
    #13
  14. Re: Thank you very much EMD

    zotkara said:

    > i think *argv[] cannot be an array since it is a pointer.


    argv is a pointer to the first element in an array of pointers to char.

    argv has type char **.
    argv[2] has type char *.
    argv[2][0] has type char.

    > since both argv[2]
    > and arv[2][0] point to the same address of an array,


    They don't. argv[2][0] is a char, not a pointer, so it doesn't point to the
    same address of an array, it doesn't point to anything, and in fact it
    doesn't even point.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jan 3, 2006
    #14
  15. Re: Thank you very much EMD

    zotkara a écrit :
    > i think *argv[] cannot be an array since it is a pointer.


    It is an pointer that holds the address of an array of pointers to char.

    For example :

    The following command line

    '$ myprog param1 param2'

    is tokenized by the system in something similar to:

    int argc = 3;
    char *argv[] =
    {
    "myprog",
    "param1",
    "param2",
    NULL
    };

    The type of argv is char **
    The type of argv[x] is char *
    The type of argv[x][y] is char

    got it ?

    > so i agree
    > with you if you'd said that argv[] is char* type.


    'argv[]' alone has no meaning. In the context of a function parameter,
    'char argv[]' means 'char *argv'.

    > since both argv[2]
    > and argv[2][0] point to the same address of an array,


    Wrong. argv[2][0] points nowhere. It's not a pointer but rather a char.

    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Jan 3, 2006
    #15
  16. zotkara

    zotkara Guest

    Yes I got it.

    ..On Tue, 03 Jan 2006 22:55:04 +0100, Emmanuel Delahaye wrote:

    > zotkara a écrit :
    >> i think *argv[] cannot be an array since it is a pointer.

    >
    > It is an pointer that holds the address of an array of pointers to char.
    >
    > For example :
    >
    > The following command line
    >
    > '$ myprog param1 param2'
    >
    > is tokenized by the system in something similar to:
    >
    > int argc = 3;
    > char *argv[] =
    > {
    > "myprog",
    > "param1",
    > "param2",
    > NULL
    > };
    >
    > The type of argv is char **
    > The type of argv[x] is char *
    > The type of argv[x][y] is char
    >
    > got it ?
    >
    >> so i agree
    >> with you if you'd said that argv[] is char* type.

    >
    > 'argv[]' alone has no meaning. In the context of a function parameter,
    > 'char argv[]' means 'char *argv'.
    >
    >> since both argv[2]
    >> and argv[2][0] point to the same address of an array,

    >
    > Wrong. argv[2][0] points nowhere. It's not a pointer but rather a char.
     
    zotkara, Jan 3, 2006
    #16
  17. zotkara

    zotkara Guest

    Cool. I got it now. Thx.

    ..On Tue, 03 Jan 2006 21:48:55 +0000, Richard Heathfield wrote:

    > zotkara said:
    >
    >> i think *argv[] cannot be an array since it is a pointer.

    >
    > argv is a pointer to the first element in an array of pointers to char.
    >
    > argv has type char **.
    > argv[2] has type char *.
    > argv[2][0] has type char.
    >
    >> since both argv[2]
    >> and arv[2][0] point to the same address of an array,

    >
    > They don't. argv[2][0] is a char, not a pointer, so it doesn't point to the
    > same address of an array, it doesn't point to anything, and in fact it
    > doesn't even point.
     
    zotkara, Jan 3, 2006
    #17
  18. Re: Thank you very much EMD

    zotkara <> writes:
    > i think *argv[] cannot be an array since it is a pointer. so i agree
    > with you if you'd said that argv[] is char* type. since both argv[2]
    > and arv[2][0] point to the same address of an array, and was subjected to
    > the same precondition for pointer initialization, i thought both argv[2]
    > and argv[2][0] would be of char type. your thoughts RCH?


    The C FAQ is at <http://www.c-faq.com/>. Section 6, Arrays and
    Pointers, is particularly relevant here, but you should consider
    reading the whole thing.

    And please don't top-post. Your response goes below, or interspersed
    with, any quoted text -- and it's seldom necessary to quote the entire
    article to which you're replying.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 3, 2006
    #18
  19. Re: Thank you very much LWP

    zotkara <> writes:
    [nothing but quoted text]

    Please don't change the Subject: header when you post a followup; it
    makes it difficult to follow the thread.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Jan 3, 2006
    #19
  20. zotkara

    Randy Howard Guest

    Re: Thank you very much EMD

    zotkara wrote
    (in article <>):

    > i think *argv[] cannot be an array since it is a pointer.


    It's not wise to argue with Richard about C. Almost all of the
    time he is correct. If you are having trouble with your code,
    odds are you are nowhere near as experienced. Listen to him, he
    has a great deal to offer for those that want to learn and have
    an open mind.


    --
    Randy Howard (2reply remove FOOBAR)
    "The power of accurate observation is called cynicism by those
    who have not got it." - George Bernard Shaw
     
    Randy Howard, Jan 3, 2006
    #20
    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. Wilq
    Replies:
    0
    Views:
    1,442
  2. MCP
    Replies:
    11
    Views:
    1,140
    Andrew Thompson
    Jun 11, 2004
  3. vv1
    Replies:
    1
    Views:
    450
    T.M. Sommers
    Nov 6, 2006
  4. Andrew Poelstra

    Re: can anyone help me in correcting this code?

    Andrew Poelstra, Nov 8, 2006, in forum: C Programming
    Replies:
    8
    Views:
    346
    goose
    Nov 9, 2006
  5. Dr Dav

    correcting code for larger 2D arrays help

    Dr Dav, May 30, 2007, in forum: C Programming
    Replies:
    2
    Views:
    314
    Keith Thompson
    May 30, 2007
Loading...

Share This Page