On 10 October 2012 06:09, Anthony Toft wrote: > But why? (the bane of my parent's existence) > > p++; only increments the value of the pointer on the stack, not the > original pointer (&myInt or &anotherInt) nor the contents of the > pointer. > Correct, it just tries to avoid situations where you may try to use the pointer as an address of a buffer as opposed to a single element. Tamas > > Here at work (I am 18 years into a software engineering career) I see > examples of unnecessary const-ness all over the place, it causes any > amount of trouble! The only time I have ever needed a is > because of overzealous use of const parameters, the compiler will not > remove const-ness without express permission, unlike adding it. > > Now, on the other hand, pointers to const objects or values ie "const > int *p" I feel is an excellent idea, but once again, it's not the > calling function saying "I won't allow him to change this" it's the > called function saying "I promise I won't change it" > > > On 2012-10-10 03:16, Tamas Rudnai wrote: > > On 9 October 2012 18:22, Anthony Toft wrote: > > > >> Not sure that I've ever wanted a const pointer other than "int > >> p[2];" > >> > >> What would you use it for? > >> > > > > We can make sure a function never screws up a pointer causing hard to > > debug > > bugs... For example: > > > > void foo ( int * const p ) { > > *p =3D 1234; // works great > > p++; // hey, we do not want to do that! Error! > > } > > > > main () { > > int myInt; > > int anotherInt; > > > > foo ( &myInt ); > > foo ( &anotherInt ); > > } > > > > $ gcc -o testConstPointer testConstPointer.c > > testConstPointer.c: In function =91foo=92: > > testConstPointer.c:5: error: increment of read-only location > > > > > > > >> > >> -----Original Message----- > >> From: piclist-bounces@mit.edu [mailto:piclist-bounces@mit.edu] On > >> Behalf > >> Of > >> V G > >> Sent: Tuesday, October 09, 2012 20:04 > >> To: Microcontroller discussion list - Public. > >> Subject: Re: [OT] C/C++ pointer syntax > >> > >> Another case. What if you want to declare a const pointer like so: > >> int * > >> const p;? > >> > >> int *const p; looks weird, and it's illogical. How would you even go > >> about > >> trying to justify why the asterisk is touching the const keyword? > >> > >> int* const p; looks better. > >> > >> int * const p; looks the best > >> -- > >> http://www.piclist.com PIC/SX FAQ & list archive View/change your > >> membership > >> options at http://mailman.mit.edu/mailman/listinfo/piclist > >> > >> > >> -- > >> http://www.piclist.com PIC/SX FAQ & list archive > >> View/change your membership options at > >> http://mailman.mit.edu/mailman/listinfo/piclist > >> > > > > > > > > -- > > int main() { char *a,*s,*q; printf(s=3D"int main() { char *a,*s,*q; > > printf(s=3D%s%s%s, q=3D%s%s%s%s,s,q,q,a=3D%s%s%s%s,q,q,q,a,a,q); }", > > q=3D"\"",s,q,q,a=3D"\\",q,q,q,a,a,q); } > > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > --=20 int main() { char *a,*s,*q; printf(s=3D"int main() { char *a,*s,*q; printf(s=3D%s%s%s, q=3D%s%s%s%s,s,q,q,a=3D%s%s%s%s,q,q,q,a,a,q); }", q=3D"\"",s,q,q,a=3D"\\",q,q,q,a,a,q); } --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .