Differences between a C string pointer and a character array

Source: Internet
Author: User

Differences between a C string pointer and a character array
Both character arrays and character pointer variables can be used to store and operate strings. However, the two are different. When using this function, you must pay attention to the following issues: 1. [String pointer variable] is a variable used to store the first address of a string. The string itself is stored in a contiguous memory space headed by this first address and ended with '\ 0. [Character array] is composed of several array elements, which can be used to store the entire string. 2. for the string pointer method char * ps = "C Language"; can be written as: char * ps; ps = "C Language"; for the array method: char st [20] = "C Language"; cannot be written as: char st [20]; st = "C Language"; [compilation error] error C2106: '= ': left operand must be l-value, instead of assigning values to each element of the character array one by one. From the above points, we can see the difference between the string pointer variable and the character array in use. It can also be seen that it is more convenient to use the pointer variable. It is dangerous to use a pointer variable before obtaining a definite address, which may cause errors. An example of an error is as follows: char * name; scanf ("% s", name); printf ("% s", name); Some compilers can pass, however, this is incorrect because it is a pointer that points to an unavailable address during definition. There are two ways to solve this problem: the array method or the method to allocate memory space to the character needle. Array Method: char name [20]; scanf ("% s", name); printf ("% s", name); Method for allocating memory space to the character needle: char * name; name = (char *) malloc (50); // The name already points to the allocated address space. Scanf ("% s", name); printf ("% s", name); however, you can assign values directly to pointer variables. This is because the C system must give a definite address when assigning values to pointer variables. 3. int main () {char str1 [40] = "hello world! "; // Char * str1 =" hello world! "; Str1 [4] = 'a'; // If str1 is pointer-type, It is compiled successfully, but running it here will block the error printf (" % s \ n ", str1); return 0;} arrays and pointers can both be initialized with string constants in their definitions. Although they look the same, the underlying implementation mechanisms are different. When defining a pointer, the compiler does not allocate space for the object to which the Pointer Points. It only allocates space for the pointer, unless it assigns a value to a String constant of the pointer for initialization at the same time. For example, the following definition creates a String constant (memory allocated to it): char * p = "abcdefg"; note that this is only true for string constants, you cannot expect to allocate space for constants such as floating point numbers, for example, Float * p = 3.14;/* is incorrect, you cannot compile */The following example to talk about the difference between the String constant created during pointer initialization and the string in the array: in ansi c, the String constant created during pointer Initialization is defined as read-only. If you try to modify the value of this string through a pointer, the program will be undefined. In some compilers, A String constant is stored in a text segment that can only be read to prevent modification. The array can also be initialized using a String constant: Char a [] = "abcdefg"; if the pointer is opposite, the array initialized by a String constant can be modified. A single character can be changed later. The following is an example in vc6 to convert all uppercase letters in a string to lowercase letters:

#include<iostream.h>  #include<ctype.h>        int strlower(char *string)  {      if(string==NULL)            return -1;      while(*string)   {          if(isupper(*string))              *string=tolower(*string);          string++;      }      *string='\0';      return 0;  }  /*char *strlower(char *string) {     char    *s;     if (string == NULL) {         return NULL;     }     s = string;     while (*s) {         if (isupper(*s)) {             *s = (char) tolower(*s);         }         s++;     }     *s = '\0';     return string; } */     void main()  {      char *test="ABCDEFGhijklmN";      strlower(test);      cout<<test<<endl;  }

 


Related Article

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.