I have been confused about when exacly to use a pointer. When I write a program that needs to pass arguments I don't see the difference in just saying-- for example
char mystr;
or
char *mystr;
what is the difference?
Thanks
Printable View
I have been confused about when exacly to use a pointer. When I write a program that needs to pass arguments I don't see the difference in just saying-- for example
char mystr;
or
char *mystr;
what is the difference?
Thanks
char mystr; = the actual char
char *mystr; = the address of mystr
When you're passing parameters in functions with C, the parameters are passed by value (as opposed to by reference). This means that the variables holding the incoming parameters in a functions are copies of the original values, and you can't change the original values.
However, if you pass in a pointer to a variable that contains a value, then what is passed in is the address. This address is still the address of the variable containing the value, and if you change that value then you change the original variable. This means you can pass in variables by reference.
Actually, to truly pass by reference (passing a pointer still copies the value of the address) you need to use C++, when you can actually pass references.