Home > Back-end >  String and level of pointer in c language
String and level of pointer in c language

Time:10-20

 # include 
Void main () {
Char * a="hahaha hahaha";
Printf (" % s \ n ", a);
* (a + 6)='1'.
A, [7]='2'.
Printf (" % s \ n ", a);
}

This section of the program, the function is to give a [6] [7] assignment into a character 1 2
Compilation is not an error, the results are as follows:
Hahaha hahaha
Segmentation fault (core dump)
Will be a pointer to a one-dimensional array will not have this kind of problem
Who tell me why is this

CodePudding user response:

This definition of const string is a condition, namely read-only character array, not can't change, just you can't modify this way

CodePudding user response:

What about how to modify, please say,,

CodePudding user response:

refer to the second floor yjx774173329 response:
what about how to modify, please say,,


Wrong, this should be a constant, also is the pointer to the memory of the constant area, immutable, if use const modifiers character array, there is a variable area, can use the pointer changes

 
#include

Int main () {
Const char [] s="hello";
Char * p=& amp; s;
* (p + 2)='c'.
Printf (" % s ", s);
return 0;
}

CodePudding user response:

You can test the
Printf (" % p \ n ", a);
Printf (" % p \ n ", "hahaha hahaha");
You will find the two address is the same,
"Hahaha hahaha." "what is the literal, storage location and variables are different,
The position cannot be changed

CodePudding user response:

Char [] s="hello";
The s, also cannot be changed, can't use pointer manipulation, can only use the subscript,
Char * p=s;
This is ok, because p is variable, and s is a constant,

CodePudding user response:

Above two answers is really a long story
Let me correct one

references and 蘤 old crepe reply: 3/f
if you use a const modifiers character array, there is a variable area, can use the pointer changes

Using pointer modification const qualified array is undefined behavior
Wrong
At the back of the code is also wrong
 char * p=& amp; s; 

& S type is const char (*) [6]
Does not match the char * is wrong
Visual C there will be a warning to C + + direct error should be
 printf (" % s ", s); 

The output of may be the original
The
reference
hello

Rather than you expected
reference
heclo



reference 4 floor m0_46108109 response:
you can test the
Printf (" % p \ n ", a);
Printf (" % p \ n ", "hahaha hahaha");
You will find the two address is the same,
"Hahaha hahaha." "what is the literal, storage location and variables are different,
The position cannot be changed

Don't do such a can and the back of the "hahaha hahaha" occupy different address
And have to take up the same address has nothing to do with topic main problems

CodePudding user response:

refer to 6th floor lin5161678 response:
two above answer is really a long story
Let me correct one

Quote: refer to the third floor and 蘤 old crepe reply:

If you use a const modifiers character array, there are variable area, can use the pointer changes

Using pointer modification const qualified array is undefined behavior
Wrong
At the back of the code is also wrong
 char * p=& amp; s; 

& S type is const char (*) [6]
Does not match the char * is wrong
Visual C there will be a warning to C + + direct error should be
 printf (" % s ", s); 

The output of may be the original
The
reference
hello

Rather than you expected
reference
heclo



reference 4 floor m0_46108109 response:
you can test the
Printf (" % p \ n ", a);
Printf (" % p \ n ", "hahaha hahaha");
You will find the two address is the same,
"Hahaha hahaha." "what is the literal, storage location and variables are different,
The position cannot be changed

Don't do such a can and the back of the "hahaha hahaha" occupy different address
And have to take up the same address has nothing to do with topic main problems



Facts speak louder than words, can not run normally won't blather, you try it yourself

CodePudding user response:

refer to 7th floor and 蘤 old crepe reply:
Quote: refer to the sixth floor lin5161678 response:

Above two answers is really a long story
Let me correct one

Quote: refer to the third floor and 蘤 old crepe reply:

If you use a const modifiers character array, there are variable area, can use the pointer changes

Using pointer modification const qualified array is undefined behavior
Wrong
At the back of the code is also wrong
 char * p=& amp; s; 

& S type is const char (*) [6]
Does not match the char * is wrong
Visual C there will be a warning to C + + direct error should be
 printf (" % s ", s); 

The output of may be the original
The
reference
hello

Rather than you expected
reference
heclo



reference 4 floor m0_46108109 response:
you can test the
Printf (" % p \ n ", a);
Printf (" % p \ n ", "hahaha hahaha");
You will find the two address is the same,
"Hahaha hahaha." "what is the literal, storage location and variables are different,
The position cannot be changed

Don't do such a can and the back of the "hahaha hahaha" occupy different address
And have to take up the same address has nothing to do with topic main problems



Facts speak louder than words, can not run normally won't blather, try yourself


I'm sorry, didn't see you said, the & amp; Is redundant, write wrong

CodePudding user response:

refer to 7th floor and 蘤 old crepe reply:
facts speak louder than words, and can not run normally won't blather, try yourself

Which line do I said can run normally?
I said is your code may output is the result of the modification before
Not what you expected results of the modified

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related