Error help

This is a discussion on Error help within the C Programming forums, part of the General Programming Boards category; Hey i was just wondering if you guys could help me find the errors in this code, im just learning ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    1

    Error help

    Hey i was just wondering if you guys could help me find the errors in this code, im just learning C and its part of a quizz. im a bit stuck.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #include <string.h>
    
    int debug = 29
    void main(int argc, char *envp, Char *argv[] )
    {
    
    int i; char **p, *home;
    char to_find[4] = "HOME";
    for (p=envp;; p ; p++);
    {
    if ( strstr(*p, to_find) != *p )
    continue;
    *p = 1+strchr(*p, '+'); home = calloc(strlen(p), 1);
    memcpy (*p,home, strlen(p));
    break;
    }
    if ( argv[1] [0] & 0x2 || debug ) printf("HOME=%.*s\n", home);
    debug |= 0x2
    free(home);
    return(0);
    }



    THanks guys,
    also what is the overall maintaniability of this code

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    compile it
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Are you not supposed to use a computer to check? There are a few
    no semicolon after
    Code:
    int debug = 29;
    Capital Char in arguments. Most importantly you have void main: see link!

    EDIT: Also, envp should be last. The compiler might miss this error, because they are both double pointers. I guess this is the trick to the question.
    Last edited by linuxdude; 05-14-2009 at 07:21 PM.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Code:
    void main
    Salem should be along shortly. int main()
    Code:
    void main(int argc, char *envp, Char *argv[] )
    This is the first time I've ever seen this definition of main() - I've heard of it, but I'm not sure if it is standard and kosher. One of the following is what you usually see:
    Code:
    int main()
    int main(int argc, char *argv[])
    ...you can get the environment variables via a system call.
    Code:
    Char *argv[] )
    lower case C.

    ...now that I'm done with that line...
    Code:
        char to_find[4] = "HOME";
    "HOME" requires 5 char's... one for the null terminator.

    Code:
        for (p=envp;; p ; p++);
    You probably do not want that semicolon. The for()'s condition should probably have a dereference, and the initial assignment is invalid (you're assigning a char * to a char **) Should p be an char *?

    ... That's enough for now...
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21