Thread: This compiler is driving me nuts

  1. #1
    martin_00
    Guest

    This compiler is driving me nuts

    my ide/compile is driving me nuts(the new Dev C++), it give me a error with this little amount of code:

    Code:
    char copy(char data[], int start, int end) {
        char value[255];
        
        return value;
    }
    it complains about the 4th line("return value;"), why? i declared value as a char and the function.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>my ide/compile is driving me nuts
    Yeah, and I expect your compiler is getting annoyed at you giving it bad code They have feelings too you know

    Anyway, back in reality:
    value is defined as an array of type char. It's local to the function. You cannot (shouldn't) return it.

    What exactly are you trying to do? Explain, and someone will guide you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    martin_00
    Guest
    im attempting to return value's data. i know i have gotten this to compile before with only a warning.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    If you got it to compile with a warning you did this probably.
    Code:
    char* copy(char data[], int start, int end) {
        char value[255];
        
        return value;
    }
    Noo!!!!! Don't do that!! You're trying to return a local variable. If you must do this , please make value static.

    Code:
    char* copy(char data[], int start, int end) {
        static char value[255];
        
        return value;
    }

    EDIT: Thanks hammer I rememered to change it on the first one to show him it'll give you a warning but was in too much of a hurry and forgot it on the correct one , doh.
    Last edited by MrWizard; 12-26-2002 at 07:27 PM.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    value's data is an array. You can return a pointer to it, but only if you make it static first.

    Or, you can create the memory for value dynamically (via malloc) and return that pointer instead. This will change the definition of value from char[] to char*. Then your calling function must free() the memory.

    Or, and I don't recommend this one, you can put value inside a struct and return that. That way the char array will be passed back whole.


    I am presuming you are wanting to return the complete array, not just the first character?

    >>i know i have gotten this to compile before with only a warning
    Doesn't always mean the code is correct though


    [edit]
    MrWizard, you're example is wrong, the function is returning a char, it should be a char*.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    martin_00
    Guest
    i thought in c when ever you make a call or return a copy of the data is placed in a new varible. what really happens?

    does that mean this is bad
    Code:
    int some_function() {
        int x = 1;
    
        return x;
    }

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>does that mean this is bad
    Your some_function() example isn't dealing with an array, only a single int. That's the main difference that is causing you confusion, I expect.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    martin_00
    Guest
    >i thought in c when ever you make a call or return a copy of the data is placed in a new varible.

    am i wrong here

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by martin_00
    >i thought in c when ever you make a call or return a copy of the data is placed in a new varible.

    am i wrong here
    OK, let me try and explain a little:

    Code:
    int some_function(void) {
        int x = 1;
    
        return x;
    }
    Here we have some_function that returns an int. The value within x is returned at the end of the function. X itself only exists within this function.
    Code:
    int *another_func(void)
    {
       static int x[10] = {100};
       return x;
    }
    Here we've changed things slightly. We deal with an array of ints rather than a single int. The variable x is now really a pointer in disguise, so when we return it, we are returning a pointer to the static array. We are NOT returning 10 ints. As the array is declared static, it exists for the life of the program, rather than this one function. That means another function can use a pointer to the array and read/write to those 10 ints.

    If it wasn't declared static, you'll get compiler warnings, and the returned pointer will be useless, pointing to memory that has been made available for other things.

    I'm not sure what it is you're trying to do within your function, so I won't go into too much more detail unless you ask.

    Maybe if you tell us what your function is supposed to do (not how it is going to do it), we can advise you better. For example, are you trying to duplicate a string (like strdup)?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    martin_00
    Guest
    heres is what i wanted it for :

    Code:
    char* copy(char data[], int start, int end) {
        char tmp[255];
        strcpy(tmp, data);
        static char value[255];
            
        memset(tmp, 8, start - 1);
        strncpy(value, tmp, (end - start + 2) );
        value[ end - start + 2 ] = '\0';
         
        return value;
    }
    i asked for help on this function here before but then i couldnt get around to finishing it. so now i figured i should find out if i learned anything from, expect some more question!!!

    thanks

  11. #11
    martin_00
    Guest
    i have a question already, did i have to copy data to another varible or is modifying an arguement alright? (i remember some one telling me about this)

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by martin_00
    i have a question already, did i have to copy data to another varible or is modifying an arguement alright? (i remember some one telling me about this)
    If you return a static variable, you're really pointing at that variable inside that function call. You still have to end up copying that data somewhere (depending on how you want to use it) to actually make use of it.

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

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by martin_00
    i have a question already, did i have to copy data to another varible or is modifying an arguement alright? (i remember some one telling me about this)
    It depends. If you call your function like this:

    >>copy("abcdefghijklmnopqrstuvwxyz", 5, 15)
    You cannot safely modify the data pointed to by the data[] variable.

    Maybe I'm being thick, but I still don't understand what it is you want your code to do. Can you explain, in English not C code, what you want. I don't want to correct your current code anymore, until I know what direction to steer you in.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    martin_00
    Guest
    sorry for the delay

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

    i know this isnt the most useful function in the world but it seems like a good start since i couldnt do it at first! ill probally but this and some other functions in a header, this is mainly just a excuse to learn alittle more and to actually do something.

  15. #15
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Let's kick this out into the lake and see if it floats :P

    Code:
    #include <stdio.h>
    
    void strNtoM(char [], int, int);
    
    int main(void)
    {
       char string1[80];
       
       printf("Enter your string: ");
       fgets(string1, 80, stdin);
       
       strNtoM(string1, 2, 5);
       
       return 0;
    }
     
    void strNtoM(char target[], int start, int end)
    {
    	int j;
    	
    	for(j = start-1; j < end; j++)
    		printf("%c", target[j]);
    }
    Dunno if it's technically correct but I get "esti"
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

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, 11: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, 03: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, 07:10 AM
  5. driving me nuts
    By boontune in forum C++ Programming
    Replies: 3
    Last Post: 10-07-2002, 04:35 AM