This compiler is driving me nuts

This is a discussion on This compiler is driving me nuts within the C Programming forums, part of the General Programming Boards category; hey, i finally remembered to find my password ronin, that does give the output i want but thats not the ...

  1. #16
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    hey, i finally remembered to find my password

    ronin, that does give the output i want but thats not the function i want. yours prints the resaults and thats not too useful, i wanted it to be returned(granted, my idea for this function isnt that useful to begin with). that code does seem familar though

  2. #17
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    code update

    i just realized theres a couple of way where this program could screw up so i guess this is a improvement

    Code:
    /* copy Function - Copies text in string data from start to end and returns the
     * the middle. Start must be >= 1. */
    char* copy(char data[], int start, int end) {
        { /* Detect and hopefully correct errors. */
            if(data[0] == '\0') 
                return (NULL);
            if(start == NULL || start < 0 || start == 0)
                start = 1;           
            if(end == NULL || end > strlen(data))
                end = strlen(data);
        }
        
        static char value[255];
        char tmp[255];
            strcpy(tmp, data);
                
        memset(tmp, 8, start - 1);
        strncpy(value, tmp, (end - start + 2) );
        value[ end - start + 2 ] = '\0';
        
        return value;
    }
    its starting to look like a real function, thanks guys

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by mart_man00
    hey, i finally remembered to find my password (its MOD EDIT)

    ronin, that does give the output i want but thats not the function i want. yours prints the resaults and thats not too useful, i wanted it to be returned(granted, my idea for this function isnt that useful to begin with). that code does seem familar though
    You need to stop programming and take a few [insert time frame here] to study basic logic.

    Given a program which prints the exact result to the screen, how would you convert that program so it returns that information instead?

    The answer is painfully easy: Copy that data instead to some place and return it.

    What is someplace? How about a buffer and then possibly later some allocated memory so I can safely return it?

    Problem solved.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: code update

    Originally posted by mart_man00
    i
    Code:
    /* copy Function - Copies text in string data from start to end and returns the
     * the middle. Start must be >= 1. */
    char* copy(char data[], int start, int end) {
        { /* Detect and hopefully correct errors. */
            if(data[0] == '\0') 
                return (NULL);
            if(start == NULL || start < 0 || start == 0)
                start = 1;           
            if(end == NULL || end > strlen(data))
                end = strlen(data);
        }
        
        static char value[255];
        char tmp[255];
    I hate to break it to you, but that's horrible looking. Why on earth are you creating an additional scope block just so you can put variables later in the middle of the function instead of logicly where they should be, at the top?

    It makes me want to cry. You've already been handed the answer. Just change the output from the screen to a buffer, malloc that much space (plus one), copy the text there, return it.

    [edit]
    I really hate to keep replying over and over to this thread, but every time I read it, I see something more that just boggles my mind... Take for instance, this little gem:

    Originally posted by mart_man00
    hey, i finally remembered to find my password (its MOD EDITED)
    Does anyone else see anything horribly wrong with this?
    [/edit]


    Quzah.
    Last edited by quzah; 12-27-2002 at 11:56 PM.
    Hope is the first step on the road to disappointment.

  5. #20
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    quzah, i know you know alot more about this than me, but im not seeing it now. why should you copy a single char at a time when you could do the whole thing? and with my "error checking part", why should i declare anything if theres a error that will stop the function from working? and yeah, it does look really bad, how would you do it?
    Last edited by mart_man00; 12-27-2002 at 11:52 PM.

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by mart_man00
    quzah, i know you know alot more about this than me, but im not seeing it now. why should you copy a single char at a time when you could do the whole thing? and with my "error checking part", why should i declare anything if theres a error that will stop the function from working? and yeah, it does look really bad, how would you do it?
    It's already been done how I'd do it, with the exception of returning it. You can use a loop, you don't have to. You can use strncpy if you like.

    Basicly, make a buffer, copy the text into it, use a pointer, malloc that amount of memory (the length of the buffer + 1), copy the buffer into it, return.

    Or, skip the buffer completely, make sure the start and end fall within the string, malloc that length (+1), copy the code to it, and return it.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #22
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Originally posted by mart_man00

    ronin, that does give the output i want but thats not the function i want. yours prints the resaults and thats not too useful
    true, but as I read it, you only wanted to know how to get "esti"

    As for your function. You don't really want to do all that extra work from within your function do you? You might consider checking for errors prior to calling the function as that would eliminate a few redundant lines.

    Originally posted by mart_man00
    if(data[0] == '\0')
    This might not be what you expect from a blank string. Try this
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       char array[80];
       
       printf("Enter a blank string: ");
       fgets(array, 80, stdin);
       
       printf("\nuser entered blank string and array[0] contains %d\n", array[0]);
       
       array[0] = '\0';
       
       printf("setting array[0] = '\\0'\narray[0] now contains %d\n", array[0]); 
       
       return 0;
       
    }
    quzah knows his stuff... he's bailed me out many times and has corrected many of my bad habits, but I still have more that need attention. :P
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  8. #23
    e-tard
    Guest
    Although you can do it the way you are trying i would not, i find it easier to do like this

    #include <string.h>

    void copy(char *dst,char *src,int i1,int i2)
    {
    src+=i1;
    strncpy(dst,src,i2);
    dst[i2]='\0';
    }//keep it simple =]


    Now you have another parameter but u wouldnt need to worrey about freeing dst... unless ofcourse u allocated somewhere else urself


    Just incase you really want it to do it the way you wanted here is one way to do it

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


    //returns null if ran outta memory must free(char *) before exiting
    //or copy()ing onto that string again
    char* copy(char *str,int i1,int i2)
    {
    char *t;
    char b;

    str+=i1;
    b = str[i2];
    str[i2] = '\0';

    t = strdup (str);
    str[i2] = b;

    return t;

    }//always keep it simple =]

    Anyways hope that helps

  9. #24
    e-tard
    Guest
    Ohh btw that static variable idea kinda sucks,because like they said you must copy the return right away.. if you didn't you wouldn't be able to call copy and use it more then one string at a time because the static would ofcourse be overwritten..

    Ignore the nay sayers screw programmers logic they will get you stuck in there way of thinking,be original =] Time and practice is all you need

  10. #25
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    thanks

  11. #26
    Registered User
    Join Date
    Jul 2002
    Posts
    913

    im got stumped again

    i must admit defeat again, i keep getting null when i use this in linux

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[]) { 
    	printf("%s\n", ( copy("testing", 2, 5) ) );
    
    	return 0;
    }
    
    int copy(char data[], int start, int end) {
    	char value[255];
    	char tmp[255];
    		strcpy(tmp, data);
    
    	memset( tmp, 8, (start - 1) );
    	strncpy( value, tmp, ((end - start) + 2) );
    	value[ ((end - start) + 2) ] = '\0';
    
    	return ( atoi(value) );
    }
    i checked out cplusplus.com/ref/ for how to use atoi, im using it correctly right? it returns the int so i could use it as a return right?
    Last edited by mart_man00; 12-29-2002 at 01:15 PM.

  12. #27
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    what i wanted it to do is to change it from a char to and int. i know it says to converts a number in a char, but doesnt ascii count? how should i convert it? strtol sounds like the same thing. and should i prototype it because its before main?

  13. #28
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    so how could i take a string like "testing" and make it an int? that seems like my real probelm.

  14. #29
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by mart_man00
    so how could i take a string like "testing" and make it an int? that seems like my real probelm.
    What exactly do you mean?

    atoi() will take a char array (null terminated), and convert it to an int. So, if you had "1234" as you char array, that would convert to 1234 as an int. However, "testing" is not a number, therefore it will convert to 0 (that is atoi() default value for things it doesn't understand).

    So, again, what exactly are you trying to do and how does it relate to this:
    Posted by martin_00
    what i want may program to do is to find to spots in a inputed array and return what is inbetween. so a call like this

    copy("testing", 2, 5); // would give me "esti".
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #30
    Registered User
    Join Date
    Jul 2002
    Posts
    913
    it relates to the function i becuase of the return. as you guys showed me before when i thought was returning a char i was really returning a adress, i would like to return a int value.

    right now i can use it like this:
    strcpy(some_var, copy("testing, 2, 5));

    i would like to be able to get the actual string back as a normal int. this really isnt about fixing a function, its starting now to be about learning annoying little things.

    (by the way, with me always using copy("testing, 2, 5);, theres no real logic behind that particular call. its just the first one i thought up.)

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to call a compiler?
    By castlelight in forum C Programming
    Replies: 3
    Last Post: 11-22-2005, 10:28 AM
  2. Help With finding a compiler
    By macman in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 04-15-2005, 08:15 AM
  3. Compiler questions
    By DvdHeijden in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2005, 02:00 PM
  4. Have you ever written a compiler?
    By ammar in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 12-27-2004, 06:10 AM
  5. driving me nuts
    By boontune in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2002, 04:35 AM

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