Const pointers vs. pointer to const is one of the nooks and crannies of C. :)<br><br>Via <a href="http://blog.voidnish.com/?p=37">http://blog.voidnish.com/?p=37</a>:<br><br><div style="margin-left: 40px;">"People new to C/C++ are sometimes confused about the difference between a const pointer and a pointer to const. A const pointer essentially means you can’t change the pointer variable itself, but you can change the value it points to. A pointer to const means you can change the pointer but not what it points to. You can use them both together and have a const pointer to a const. The code snippet below should make it really clear I hope."<br>
<br>//pointer to a const<br>void f1()<br>{<br> int i = 100;<br> const int* pi = &i;<br> //*pi = 200; <- won't compile<br> pi++;<br>}<br><br>//const pointer<br>void f2()<br>{<br> int i = 100;<br>
int* const pi = &i;<br>
*pi = 200;<br> //pi++; <- won't compile<br>}<br><br>//const pointer to a const<br>void f3()<br>{<br> int i = 100;<br> const int* const pi = &i;<br> //*pi = 200; <- won't compile<br> //pi++; <- won't compile<br>
}<br></div><br><div class="gmail_quote">On Sun, Feb 28, 2010 at 10:49 PM, Carl Boettiger <span dir="ltr"><<a href="mailto:cboettig@gmail.com">cboettig@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<br><br><div class="gmail_quote"><div class="im">On Sun, Feb 28, 2010 at 6:35 PM, Brian Lavender <span dir="ltr"><<a href="mailto:brian@brie.com" target="_blank">brian@brie.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I think if anything, C has been a certain detriment to the field of<br>
computer science!<br>
<br>
One calls a function and the arguments are passed by value. Call a<br>
function with an array as an argument, and feel free to modify its<br>
contents!<br>
<br></blockquote></div><div>so declaring an array as const prevents this, func(const double * a). I understand that this also helps the compiler make optimizations it cannot do when you don't use const. I think you could still modify the contents of the array by first copying the pointer though,<br>
<br>double * b = a;<br>b[i] = something new.<br><br>So there's also the modifier "restrict", which I believe would prevent this, and again helps out the compiler do smart things. Others can probably confirm/correct this? Is it good practice to use these modifiers as often as possible/appropriate?<br>
</div><div><div></div><div class="h5"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Certainly, C++ added the idea of reference, but I think Pascal<br>
simplifies these concepts much better. Yet, Pascal seems to be relegated<br>
to the status as a legacy language!<br>
<br>
brian<br>
<br>
<br>
#include <stdio.h><br>
<br>
#define CAP 10<br>
<br>
void mod_array(int a[])<br>
{<br>
a[2] = 5;<br>
}<br>
<br>
void trychange(int a)<br>
{<br>
a = 2;<br>
}<br>
<br>
void reallychange(int *a)<br>
{<br>
*a = 2;<br>
}<br>
<br>
int main() {<br>
int b[CAP];<br>
int c;<br>
int i;<br>
<br>
printf("Load array and change a value\n");<br>
for (i=0; i < CAP; i++)<br>
b[i] = i + 20;<br>
<br>
<br>
mod_array(b);<br>
<br>
for (i=0; i < CAP; i++)<br>
printf("b[%d] has value of %d\n",i,b[i]);<br>
<br>
c = 10;<br>
<br>
printf("c has a value of %d\n",c);<br>
trychange(c);<br>
<br>
printf("c has a value of %d after trychange(c)\n",c);<br>
<br>
reallychange(&c);<br>
<br>
printf("c has a value of %d after reallychange(&c)\n",c);<br>
<br>
<br>
}<br>
<br>
--<br>
Brian Lavender<br>
<a href="http://www.brie.com/brian/" target="_blank">http://www.brie.com/brian/</a><br>
<br>
"There are two ways of constructing a software design. One way is to<br>
make it so simple that there are obviously no deficiencies. And the other<br>
way is to make it so complicated that there are no obvious deficiencies."<br>
<br>
Professor C. A. R. Hoare<br>
The 1980 Turing award lecture<br>
_______________________________________________<br>
vox mailing list<br>
<a href="mailto:vox@lists.lugod.org" target="_blank">vox@lists.lugod.org</a><br>
<a href="http://lists.lugod.org/mailman/listinfo/vox" target="_blank">http://lists.lugod.org/mailman/listinfo/vox</a><br>
</blockquote></div></div></div><font color="#888888"><br><br clear="all"><br>-- <br>Carl Boettiger<br>Population Biology, UC Davis<br><a href="http://two.ucdavis.edu/%7Ecboettig" target="_blank">http://two.ucdavis.edu/~cboettig</a><br>
</font><br>_______________________________________________<br>
vox mailing list<br>
<a href="mailto:vox@lists.lugod.org">vox@lists.lugod.org</a><br>
<a href="http://lists.lugod.org/mailman/listinfo/vox" target="_blank">http://lists.lugod.org/mailman/listinfo/vox</a><br>
<br></blockquote></div><br>