Like Tree1Likes
  • 1 Post By jimblumberg

I need some help with C

This is a discussion on I need some help with C within the C Programming forums, part of the General Programming Boards category; Well guys Im starting with C and Im doing a really simple program, but its giving me some issues Maybe ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    16

    I need some help with C

    Well guys Im starting with C and Im doing a really simple program, but its giving me some issues Maybe you will be able to help me, thats my code
    Code:
    #include <stdlib.h>
    Code:
    #include <stdio.h>
    void LimitesLee(int linf, int lsup)
    {
    
    printf("Introduzca limite inferior y superior.\n");
    scanf("%d",&linf);
    scanf("%d",&lsup);
    /*if(linf>lsup){
    LimitesLee(linf,lsup);
    */
    }
    
    void LimitesImprime(int linf, int lsup){
    printf("Limites: [%d, %d]",linf,lsup);
    }
    /*int EnteroAleatorio(){
    int a;
    rand();
    a = rand()
    if(a>
    
    return rand();
    }
    
    */int main()
    { 
    int linf = 0;
    int lsup = 0;
    LimitesLee(linf,lsup);
    LimitesImprime(linf,lsup);
    
    system("pause");
    return 0;
    }
    The prob is that when I set the "linf" and the "lsup" the function LimitesImprime still giving me the 0,0 values instead the values that I set
    Thanks for your time I hope you can answer me!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,693
    You're sending copies of the variables into your LimitesLee() function so any changes made to those variables in that function are lost when you return from the function. You'll need to use pointers for the parameters if you want to change the variables in the calling function.


    Jim

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Quote Originally Posted by zetaXX View Post
    The prob is that when I set the "linf" and the "lsup" the function LimitesImprime still giving me the 0,0 values instead the values that I set
    Thanks for your time I hope you can answer me!
    Passing linf and lsup to the function like that passes their value -- any changes you make to them in LimitesLee() won't have any affect on the variables in main(). This is the usual way arguments are passed in C -- to have the function modify them, you'll need to use pointers. There are plenty of tutorials on this.

    Or you could have two separate functions to read linf and lsup, and return the value read. Depends if you want to get your head round pointers.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    It is because you are setting the values in the function LimitesLee(..) This function takes copies of the variables it receives - They are 'local' to the scope of the function - your variables -anywhere else, even with the same name will not be affected by anything that happens in your called function. You need to learn about pointers and scope:

    "using pointers is one way to have a function modify a variable passed to it"
    Last edited by rogster001; 05-24-2013 at 02:54 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    It's pass-by-value vs. pass-by-pointer.

    Here's a debugging session with comments

    Code:
    gdb) b 30
    Breakpoint 1 at 0x100000e36: file limits.c, line 30.
    (gdb) run
    Starting program: limits 
    Reading symbols for shared libraries +............................. done
    
    Breakpoint 1, main () at limits.c:30
    30		LimitesLee(linf,lsup);
    (gdb) p linf
    $1 = 0
    /* Print address of linf variable being passed */
    (gdb) p &linf
    $2 = (int *) 0x7fff5fbffbb4
    /* Print address of lsup variable being passed */
    (gdb) p &lsup
    $3 = (int *) 0x7fff5fbffbb0
    /* Step into LimitesLess function */
    (gdb) s
    LimitesLee (linf=0, lsup=0) at limits.c:6
    6		printf("Introduzca limite inferior y superior.\n");
    /* Print addresses of function parameters */
    (gdb) p &linf
    $4 = (int *) 0x7fff5fbffb9c
    (gdb) p &lsup
    $5 = (int *) 0x7fff5fbffb98
    (gdb)
    Notice the addresses are different? Now with pass-by-pointer:
    Code:
    (gdb) run
    Starting program: limits 
    Reading symbols for shared libraries +............................. done
    
    Breakpoint 1, main () at limits.c:30
    30		LimitesLee(&linf,&lsup);
    /* Print addresses being passed */
    (gdb) p &linf
    $1 = (int *) 0x7fff5fbffbb4
    (gdb) p &lsup
    $2 = (int *) 0x7fff5fbffbb0
    (gdb) s
    LimitesLee (linf=0x7fff5fbffbb4, lsup=0x7fff5fbffbb0) at limits.c:6
    6		printf("Introduzca limite inferior y superior.\n");
    /* Print pointers being passed (addresses) */
    (gdb) p linf
    $5 = (int *) 0x7fff5fbffbb4
    (gdb) p lsup
    $6 = (int *) 0x7fff5fbffbb0
    (gdb)
    Notice they're now the same?

    Meh...too slow.

  6. #6
    Registered User
    Join Date
    May 2013
    Posts
    16
    Quote Originally Posted by jimblumberg View Post
    You're sending copies of the variables into your LimitesLee() function so any changes made to those variables in that function are lost when you return from the function. You'll need to use pointers for the parameters if you want to change the variables in the calling function.


    Jim
    Could you explain me how should I do it? I mean I know how to use pointers, but where should I place them and in what way should I change the function and main?

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    The function takes pointers as arguments, and you pass the address of each variable to the function.

    Note: this means your scanf call variables will also change, because you will be receiving pointers as function arguments.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    2,693
    You may want to start by studying this tutorial: C pointers.

    Jim
    rags_to_riches likes this.

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    in effect, at present you may as well have written this :

    Code:
    LimitesLee(0,0);
    You know you cant expect changes to 0,0 to be accesible after the function call
    Last edited by rogster001; 05-24-2013 at 03:22 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

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