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.
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.
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
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*Code:Reference Where Legal Data Type Value -------------------------------------------------------------------------------------------------------- valp sub double* pointer to color shaded cell &many code &code countp *countp *valp letp &x
Like in the following code:
Code:double *valp;
Last edited by Daveo; 11-07-2004 at 03:33 AM.
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:Originally Posted by Daveo
Some other bits of info I thought you might maybe not yet understand in detail: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;
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 "); }
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. "Do you mean: Trying to point to a variable in function 'main' from a pointer within the body would lead to an error.
take this as an example and compare it to our program that uses pointers earlier :
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.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; }
i already answered your question in my previous reply :Are they the same? I like your way, its shorter
" 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."
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.What does the * mean?
for example :
now 'p' has the memory adress of 'n' , whereas '*p' will be pointing to the value of 'n' wich is 5.Code:... int n = 5; int *p = n; ...
It's type is 'double *' wich means 'pointer-to-double' , we use it this way to distinguish pointers and regular variables.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*
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
Originally Posted by Daveo
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?!Originally Posted by Jez
Quzah.
Hope is the first step on the road to disappointment.
LOL ... i hope that was a sarcastic commentOriginally Posted by quzah
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