About pointers and array-defined strings

Source: Internet
Author: User

  1. point the pointer to a string of strings (char *a = "abcdef";), which can be "printf ("a[2] =%c\n", a[2])" This way the third character in the string is output. But not so "a[2] = 'g' " to attempt to change the string of the third character ' C ' to ' G '. As for why, I do not know now. Look at the code first .
    1#include <stdio.h>2  3  intMainvoid)4  {5      Char*a ="abcdef";6printf"a[2] =%c\n", a[2]);//can correctly output A[2] = c7a[2] ='G';//The program does not end properly at this point. 8  9printf"In main =%s\n", a);Ten      return 0; One}

    With a little change, you can change the character of a position in a string. Look at the code:

    #include <stdio.h>intMainvoid) {     Chara[Ten] ="abcdef";------> Modify here. printf ("a[2] =%c\n", a[2]);//correct output a[2] = c a[2] ='G'; printf ("In main =%s\n", a);//correct output in main = Abgdefreturn 0; }

    The small gap between the two codes is interesting to map out. Add something to them and keep looking.

  2. Here's a look at the function call.
    1#include <stdio.h>2  3  voidChange_piont (Char*);4  5  intMainvoid)6  {7      Char*a ="abcdef";8printf"a[2] =%c\n", a[2]);9  Ten Change_piont (a); Oneprintf"In main =%s\n", a); A      return 0; -  } -   the  voidChange_piont (Char*a) -  { -printf"a[2] =%c\n", a[2]);//This can be output A[2] = C, which means that pointer a can be accessed in this function.  -a[2] ='g';//There is an error here.  +   -  } +  /************************************** A * Output Result: at * A[2] = c - * A[2] = c - * Segmentation Fault (core has been dumped) -   * ************************************/

    As in the 1th, an error occurs when trying to change a character in a position in a string. Like the 1th, let's change it.

    1#include <stdio.h>2  3  voidChange_piont (Char*);4  5  intMainvoid)6  {7      Chara[Ten] ="abcdef"; The only---------------> change is this line.8printf"a[2] =%c\n", a[2]);9  Ten Change_piont (a); Oneprintf"In main =%s\n", a); A      return 0; -  } -   the  voidChange_piont (Char*a) -  { -printf"a[2] =%c\n", a[2]); -a[2] ='g'; +   -}

    as expected, as in the 1th, when you change the pointer to an array, you can replace it normally.
    Here, we can start with a small summary.
    I don't know if you're aware of the above output: for a pointer initialized to a sentence, you can use an array notation (that is, a[i]) to output any of the characters in the sentence. We know that the values in the output array are done using array notation. In other words, for a definitionchar a[] = "abcdef" AndChar *a ="abcdef can all use array notation a[2] The third character of the output string abcdef: C. So, isn't char a[10] the same as abcdef defined by Char *a? Non-also, first, as can be seen from the above code example, you can modify the array of strings defined by a sequence of characters, such as in the second code a[ g ' modified the third character of the string. While the pointer points to a string, you cannot modify the characters in it. can only be referenced. Also, when you use sizeof (a) to output array A and pointer a separately, you will find a much larger difference. Of course, for pointers, sizeof (a) outputs the memory size of the stored a pointer (typically 8, which is determined by your system). If you want to output the byte size of the address that the a pointer points to, then sizeof (*A), for example of this article, is output 1, char type is to occupy a byte ah.
            pointer points to a string that is a whole constant. Have you ever seen anyone who can change a constant? In fact, there is a deeper meaning of things, I vaguely doubt: with A[i] can output the string constant of a character, which shows that you can know the address of the character, that through this address why can not modify the value of this address? Our usual variable values are not changed by address?
    Personal guess: A[i] is also a constant, constant also has an address, but the address can not modify the constant.

  3. Assign a value to the memory-allocated pointer and see if it can be changed.
    #include <stdlib.h> #include <stdio.h>intMainvoid) {     intNumber =Ten; Char*a = (Char*)calloc(Number,sizeof(Char)); printf ("a_size =%d\n",sizeof(a));  for(inti =0; I < number; ++i) {         Charb ='a'; A[i]= B+i;//using b++ will fail, each time the value of B will be reinitialized in the above sentence. } printf ("A =%s\n", a); printf ("a_size1 =%d\n",sizeof(a)); a[2] ='B'; printf ("A1 =%s\n", a);  Free(a); A=NULL; //Change_piont (a); //printf ("In main =%s\n", a);     return 0; } /************************************** * a_size = 8 * a = ABCDEFGHIJ * a_size1 = 8 * A1 = Abbdefghij * *********** *************************/

    How to add two lines to the code above:

    1 scanf ("%s", a); 2 printf ("a2 =%s\n", a);

    You can use the SCANF function to modify the value of a point arbitrarily (not more than number, of course).
    The problem seems clear, and then look at:

    1#include <stdio.h>2  3  intMainvoid)4  {5      CharA ='a';6      Char*PA = &A;7pa[0] ='A';8printf"A =%c\n", a);9printf"*pa =%c\n",*PA);Ten   One      return 0; A  } -  /************* - * a = a the * *pa = A -   * **********/

    Finally understood. When this is initialized: char *a = "abcdef", which essentially refers to pointer a to a string constant!!!!! And every character of this string is constant, cannot modify these constants, and cannot be scanf ("%s", a)!!!! If pointer A is pointed to a variable, it can be modified!!

About pointers and array-defined strings

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.