Thread: Return Statement

  1. #16
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19
    Thanks for the input guys/gals.........really gonna help me in the final exam! I hope you guys dont mind if I ask more questions later on when I stumble over something I dont understand.

  2. #17
    The Nice Guy
    Join Date
    Sep 2004
    Posts
    19
    Given the memory setup shown, fill in the chart by indicating the data type and value of each reference as well as the name of the function in which the reference would be legal. Give pointer values by referring to cell attributes. For example, the value of valp would be "pointer to color shaded cell," and the value of &many would be "pointer to gray shaded cell."

    http://users.tpg.com.au/dogdoo/Cprog/Table.jpg

    Code:
    Reference          Where Legal          Data Type          Value                                   
    
    --------------------------------------------------------------------------------------------------------
    
    valp               sub                  double*            pointer to color shaded cell
    &many
    code
    &code
    countp
    *countp
    *valp
    letp
    &x
    Now, the confusion is the data type of the first one (valp). Its double*. Isnt it suppose to be double, and not double*. I though * is part of *valp, and not part of double*

    Like in the following code:

    Code:
    double *valp;
    Last edited by Daveo; 11-07-2004 at 03:33 AM.

  3. #18
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by Daveo
    Now, the confusion is the data type of the first one (valp). Its double*. Isnt it suppose to be double, and not double*. I though * is part of *valp, and not part of double*
    The table clearly shows a pointer (literally) from function sub()'s valp to main()'s x. x is 17.1 so it has to be some kind of floating point type, e.g. double. Maybe this helps to understand it:

    Code:
          // variable x of type double
          double x;
          
          // variable valp of type pointer to double
          double *valp;
          
          // same as above 
          double* valp;
          
          // tricky: variable valp of type pointer to double and variable valq of type double (not a pointer to a double).
          double* valp, valq;
          
          // same as above, but easier to read (personal opinion)
          double *valp, valq;
         
         // this should be easy to figure out now
         double valq, *valp= &valq;
    Some other bits of info I thought you might maybe not yet understand in detail:

    Functions can take parameters and have one return value. If a function main() calls another function X(), the parameters specified for X() by main() are copied into X()'s scope and can be accessed like local variables. Any modification to those variables will *not* change "their" content in main. After all, they're just copies.
    However, if main() passes X() a pointer to one of its (main()'s) variables, X() can use that pointer to modify the data/variable pointed at. It can however still not change the pointer itself in main()'s scope, just its local copy. So pointers are a good way if you need more then one return value or if you want a function to change data at a specific, already existing address in memory, eg. to modify a string.
    If you see X* or X * then that's a type of a pointer ("a type X * that can be used to create pointers pointing at variables of type X"), whereas *x (or * x) is a variable x of an (here unspecified) pointer-type - e.g. int *x.

    Now when X() has a return value, that value directly modifies the variable to which X() as been assigned, e.g.

    Code:
    int x_returned= X();
    Last edited by Nyda; 11-07-2004 at 05:08 AM.
    main() { int O[!0<<~-!0]; (!0<<!0)[O]+= ~0 +~(!0|!0<<!0); printf("a function calling "); }

  4. #19
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Do you mean: Trying to point to a variable in function 'main' from a pointer within the body would lead to an error.
    there is a typo there , i ment "Trying to point to a variable in function 'main' from a pointer within the body of any other function would lead to an error. "

    take this as an example and compare it to our program that uses pointers earlier :
    Code:
    #include <stdio.h>
    
    void sum();
    
    int main(void)
    {
       int result;
    
       sum();
    
       printf("%d", result);
    
    
       return 0;
    }
    
    void sum(void)
    {
       int *x;
       int num1, num2 , sum;
    
       printf("Enter the first number : ");
       scanf("%d", &num1);
    
       printf("Enter the second number : ");
       scanf("%d", &num2);
    
       sum = num1 + num2;
    
        x = &result     /* we're trying to point to the adresss of 'result' 
                                   in function 'main' but we can't */
       *x = sum;
    
    }
    we tried to point to 'result' from a pointer within the body of another function but it won't work , why? because 'result' is a local variable and its scope ends with the end of 'main' function.

    Are they the same? I like your way, its shorter
    i already answered your question in my previous reply :
    " between the parentheses we're telling the computer how many and what kind of arguments our sum function will take (or point to). We declared two int variables without the necissity of specifing their names."


    What does the * mean?
    the '*' is called the indirection operator , it enables us to refer to target objects. In easy words , you precede a variable in its declaration with a '*' to tell that its a pointer-to-type where type is the type of object its pointing to.

    for example :
    Code:
    ...
    int n = 5;
    int *p = n;
    ...
    now 'p' has the memory adress of 'n' , whereas '*p' will be pointing to the value of 'n' wich is 5.

    Now, the confusion is the data type of the first one (valp). Its double*. Isnt it suppose to be double, and not double*. I though * is part of *valp, and not part of double*
    It's type is 'double *' wich means 'pointer-to-double' , we use it this way to distinguish pointers and regular variables.


    p.s : my name isn't 'Brian' its 'Brain' Cell
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  5. #20
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    Quote Originally Posted by Daveo
    Also, im having a hard time understanding the basics of pointers. Could you guys direct me to the right place, possibly a tutorial on pointers that is not hard, and easy to understand for someone like me who is a novice at C.

    I'm surprised no-one's mentioned that there is an tutorial on pointers in the FAQ's.

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Jez
    I'm surprised no-one's mentioned that there is an tutorial on pointers in the FAQ's.
    You mean there is a FAQ here?!

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

  7. #22
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Quote Originally Posted by quzah
    You mean there is a FAQ here?!

    Quzah.
    LOL ... i hope that was a sarcastic comment


    here's the pointers tutorial from the FAQ. And www.google.com would be your best in finding other tutorials
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM