char * vs Char [] depth gouging

Source: Internet
Author: User

compilation does not pass
#include "iostream" using namespace std; int _tmain (int argc, _tchar* argv[]) {char *p= "hello";//Do not assign a string to a character pointer, but instead point to the first address of the string with a pointer to a character type. strcpy (P, "hel"); cout << p << Endl; return 0; }//compile Pass but
parsing:char*p= "Hello";is actuallyconstchar *p= "Hello";There's no change in the contents .,so compile and pass.
integer assignment of character array

chara[10]= "Hello";// This is exactly the right wording . , the way it is initialized, looks like an lvalue is a string, but it's actually an initialization list. The final list contains the.

chara[10]; a[10]= "Hello"; / This is a bad notation, a character cannot hold a string, besides A[10] and it doesn't exist!

chara[10]; a= "Hello";// This is wrong and is not allowed, a Although it is a pointer, it has already pointed to the allocation in the stack Ten character space, and now this situation a and point to the data area. Hello constant, here's the pointer a there was confusion.

chara[10]; strcpy (A, "hello");// This is the right way to do it, and it's recommended.

Comparison of character numbers: cannot use "=="To make comparisons,can only be usedstrcmp ()function to handle

parsing: in the C A string is treated as an array in the language, so the strings are restricted in the same way as arrays, especially if they are not used C the operator for the language to copy and compare operations. Attempting to copy or compare a string directly fails.

CHARSTR1[10], str2[10];

str1= "abc";/*** wrong ***/ Use = operator is not possible to copy a string into a character array.

str2= str1;/*** wrong ***/ Use = operator is not possible to copy a string into a character array.

charstr1[10] = "abc";/*** Right ***/ because in the declaration, = is not an assignment operator.

if (str1==str2)/*** wrong ***/ This statement is to str1 and the str2 Instead of comparing the contents of two arrays, compare them as pointers. Because str1 and str2 have different addresses, the value of the expression str1 = = str2 must be 0 .

storage of arrays and strings in the machine

array: In the machine, when defined Char a[10]; The memory space has been allocated and placed in the data segment stack, where the values can be modified.

string: The string is stored in memory in the data constant segment, but the string is also called a string constant, which is the compiler " nailed to death " cannot be modified, so *p = ' Z '; It is not allowed to change the value of a string.

Example: char *a = "World" and the charb[]= "Hello" the Difference

+---+---+---+---+---+---+

B:|h|e | l | l | o|\0|

+---+---+---+---+---+---+

+-----++---+---+---+---+---+---+

a:|*======>|w|o|r|l|d|\0|

+-----++---+---+---+---+---+---+

1, a with the b Different types: a Pointer to a string constant , andb to the array that holds the string

2, "hello" "world" saved in the stack, where only the (b[3] = ' f ';) "Hello" is saved in the global data area at .rodata (a[3] = ' a '; error )

3, a is the global data area, b is the function of the stack space area, the function is completed, the space can be lost. So,return A; is possible and return B; it's dangerous.

4, 2 in cases where the array name is not equal to the array pointer, & with the sizeof Operator:

&a get is the address of a pointer variable, not a string "World" 's first address; &b get is the string "Hello" 's first address.

" siziof "The difference:

sizeof (S1); Return 4, pointer size

sizeof (S2); Return 6, array size

the first address of the array and the first address of the array difference

The array name represents the entry address of the array, which is similar to the function name, struct identifier, and so on.

Char a[10]; so a represents the address of the first element of this array, which is a &a[0];

and &a; represents the first address of this array.

It is estimated that many people are confused at this time, do these two have a difference? Is the value different?

Their two values are the same, because the address is only one, the value of the address of an element of an array is the value of the address of the array, then why do you want to divide it so carefully? Here's an example.

char A[10];char *p = NULL; char (*P_A) [ten] = NULL;  p = A; P_a = &a;

cases where memory is not allocated in advance


1) What is the problem with the following code:
int main ()
{
Char A;
Char *str=&a;
strcpy (str, "Hello");
printf (str);
return 0;
}
Answer: There is no memory space allocated for STR, an exception occurs when a string is copied into the address of a character variable pointer. Although the results can be output correctly, the program crashes due to the internal read and write across the bounds
2) Char *s; strcpy (S, "Hello"); it's not true. Why not?
If s does not allocate memory space
strcpy (S, "hello") will cause memory access exceptions
can be used S = (char*) malloc (10);//allocate 10 bytes of memory to s
or S = new char[10];//is tightly limited to the C + + language
3) Is there a problem with the following statement?
Char *a;
String b=a;
Parsing: All unknown exceptions occur because no memory address is specified in advance for a, but there is no problem if a memory location or initialization value is specified.

char * vs Char [] depth gouging

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.