The difference between char* arr and char[] Arr

Source: Internet
Author: User
Tags strcmp

Date of writing: 2016.08.31

AC qq:992591601

Spent a few days reviewing the C language. For the C language of the string operation some are not used to, so as an exercise, wrote down the following such a wrong program:

#include <stdio.h>#defineMax_str_size 100voidCopy_string (Char* from,Char*To ) {     while(*to++ = * from++);}voidSwap_strs (Char* STR1,Char*str2) {    CharTmp[max_str_size];    Copy_string (STR1, TMP);    Copy_string (str2, str1); Copy_string (TMP, STR2);}voidSort_strs_by_ascii (Char* arr[3]) {    if(strcmp (arr[0], arr[1]) <0) swap_strs (arr[0], arr[1]); if(strcmp (arr[1], arr[2]) <0) swap_strs (arr[1], arr[2]); if(strcmp (arr[0], arr[1]) <0) swap_strs (arr[0], arr[1]);}intMain () {Char* arr[3] = {"DD","AA","cc"};    Sort_strs_by_ascii (arr);  for(inti =0; I <3; i++) printf ("%s\n", Arr[i]); return 0;}

This program is certainly not very good, mainly to practice C, deliberately to use something, such as a C-string pointer, pointer array.

The array in the program holds three string pointers. Use the array as a parameter to sort by the Sort_strs_by_ascii method. But the error is in the process of string manipulation.

Then I learned that char* arr is a string pointer that points to a value that is stored in a constant area and cannot be rewritten. However, the pointer can be arbitrarily pointed to other address spaces.

While an array pointer such as Char arr[] is essentially a pointer, the pointer to the content is fixed in the memory space of the array. However, the contents of the array space can be rewritten.

The difference between char* arr and char[] Arr

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.