for example char a[] = "hello"; const char * p = a; p[1] = '3'; //is this ok? I know it is not ok for this: *p = '3' since p point to a const char. But it is not saying p+1 is pointing to a const char. so p[1] ='3' would have been ok. But I find that it is not ok to do this: p[1] = '3' after I compile it Any comments? seems a stupid question. thanks
I bought the original package last year, and only opened it to check it out. Never used. Includes: 1. Read me first letter 2. Course schedule days 1-10 3. Welcome letter. 4. Exam Focus 3-Ring Binder (New Sealed with Complete Documents) 5. The Pre-course workbook 6. The ClaimsLight workbook 7. The Prime Questions workbook 8. 36 audio CDs 9. 8 video DVDs 10. Patware 9.0 11. 2013 Supplemental CD Including the updated MPEP Version 8.8. Bilski and KSR, AIA and new rule changes for 2012-2013 Ask for $400. If interested, contact me.
type of p is const char *, so type of p+1 is also const char *, thus type of p[1], which is *(p+1), is const char, and you can't assign to it.
【在 r*******y 的大作中提到】 : for example : char a[] = "hello"; : const char * p = a; : p[1] = '3'; //is this ok? : I know it is not ok for this: *p = '3' : since p point to a const char. : But it is not saying p+1 is pointing to a const char. : so p[1] ='3' would have been ok. : But I find that it is not ok to do this: p[1] = '3' after I compile it : Any comments? seems a stupid question. thanks
Not a stupid question at all. We all learn from asking questions. p[1] is converted to *(p+1) in compiler. p is declared as a character pointer to a constant string. Therefore, although the pointer can be incremented/decremented, it is illegal to use the pointer to change the data it is pointing to.
that is my question: type of p is const char *, why type of p+1 is also const char * ? thanks
【在 X****r 的大作中提到】 : type of p is const char *, so type of p+1 is also const char *, : thus type of p[1], which is *(p+1), is const char, and you can't : assign to it.
【在 r*******y 的大作中提到】 : for example : char a[] = "hello"; : const char * p = a; : p[1] = '3'; //is this ok? : I know it is not ok for this: *p = '3' : since p point to a const char. : But it is not saying p+1 is pointing to a const char. : so p[1] ='3' would have been ok. : But I find that it is not ok to do this: p[1] = '3' after I compile it : Any comments? seems a stupid question. thanks
O*y
21 楼
看几集觉得没意思,演员不行
i*e
22 楼
http://www.parashift.com/c++-faq-lite/const-correctness.html#fa Fred const* p means "p points to a constant Fred": the Fred object can't be changed via p. In your case, you are still trying to change the data p is pointing to, even though you use p+1. What is the point of const-correctness if you can increment the pointer and changing the data that is declared as const?
【在 r*******y 的大作中提到】 : that is my question: type of p is const char *, why type of p+1 is : also const char * ? : thanks
【在 r*******y 的大作中提到】 : that is my question: type of p is const char *, why type of p+1 is : also const char * ? : thanks
X*r
24 楼
because it's turtle's ass... joke aside, think about it yourself. what type you think p+1 should be?
【在 r*******y 的大作中提到】 : that is my question: type of p is const char *, why type of p+1 is : also const char * ? : thanks
j*d
25 楼
depending on your machine/arch/compiler "hello" could be in text segment, or the .rodata, or even data segment. 如果是前二,大多数OS会直接seg fault你 如果是data segment,应该没问题。
【在 r*******y 的大作中提到】 : for example : char a[] = "hello"; : const char * p = a; : p[1] = '3'; //is this ok? : I know it is not ok for this: *p = '3' : since p point to a const char. : But it is not saying p+1 is pointing to a const char. : so p[1] ='3' would have been ok. : But I find that it is not ok to do this: p[1] = '3' after I compile it : Any comments? seems a stupid question. thanks