Embedded C language used in character arrays and character pointers summarize again

Before the time to learn C language also has a separate study recorded over character arrays and character pointers can be found in:

  • https://blog.csdn.net/ReCclay/article/details/60638929
  • https://blog.csdn.net/ReCclay/article/details/78034368
  • https://blog.csdn.net/ReCclay/article/details/78946179

Do embedded development, found in the actual development is sometimes completely forgotten the difference between the two, specially today again as a summary.


Notes 1

When using a char *type of variable, this variable compilers normally constant region in memory, the contents of the string can not be modified.

If you want to modify the contents of the string will be placed in an array of characters, so as to ensure that it be modified on the system!

eg:

  char *sp = "Hello World";
  char sa[] = "Hello World";

1. If you want to modify the string array sa represented by only a single directly modify the content of the element, not directly on the entire modification!
Can sa[0] = 't'but not directly sa = “happy”this is illegal, if it is to be used to modify the entire strcpyfunction

2. To modify the represented string pointer sp, can operate on the entire (corresponding to modify its point) but may not modify the individual elements directly.
Can sp = "happy", notsp[0] = 't'

Here on a note, in the actual embedded development, we can define a character array, then let a character pointer to the character array, this time by a similar character pointer can sp[0] = 't'modify a single character in a way!

Note 2

About array of characters realization ++method traversal, look at the test code

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char len = 0;
    char str[] = "wang";
    char *p = str;

//    while(*str++ != '\0')str代表的是数组名,是不能进行++的
//    {
//        len++;
//    }
    while(*p++ != '\0')//用一个指针指向就可以了!
    {
        len++;
    }
    printf("%d", len);
    return 0;
}

When I use the comment section of that program, the compiler will have lvalue required as increment operandthe error, but when I use a character pointer ppointing to an array of characters strwhen they can achieve ++traversal.

Note 3

Not earmarked for specific character array size, the compiler will automatically allocate memory just the right size. See specific examples below.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char str1[] = "abcde";
char str2[10] = "abcde";

int main()
{
    printf("%d\r\n", sizeof(str1));
    printf("%d\r\n", sizeof(str2));
    printf("%d\r\n", strlen(str1));
    printf("%d\r\n", strlen(str1));

    return 0;
}

Export

6
10
5
5

strAllocating a b c d e \0a total of six bytes.

Note 4

In more detail refer to: https: //blog.csdn.net/qq_28877125/article/details/79933782

Hexadecimal string into a character array to be noted converted to two hexadecimal digits, Specific examples are as follows:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char buf2[] = "abc";
char buf3[10];

int i;

int main()
{
    for(i=0; i<strlen(buf2); i++)
	{
		sprintf(&buf3[i*2], "%02X", buf2[i]);
	}
    printf("%s", buf3);
    return 0;
}

Export

616263
Published 653 original articles · won praise 1016 · Views 730,000 +

Guess you like

Origin blog.csdn.net/ReCclay/article/details/104016287