A pointer written question in C Language

Source: Internet
Author: User
Tags aliyun

The questions are as follows:

#include
 
  int main(){char *a="Aliyun";char **b=&a;*b="programming test";char *c=++a;a="talents.";printf("%c\n",*++c);return 0;}
 

Ask what the output is.

We analyze it step by step.

First, the first sentence: char * a = "Aliyun";, this program declares a pointer variable a, a pointing to the constant string "Aliyun ". That is to say, the address in variable a, that is, the address that stores the string "Aliyun.

The second sentence: char ** B = & a; this statement declares a pointer. Actually, B is a pointer, but B points to a variable. That is to say, B stores the address of a. Note that it is the address of a, not the value of. Here we need to understand the concept that pointer variables are always stored in addresses, and NULL is not included.

The third sentence: * B = "programming test"; here * is called a quotient character. In simple words, it is the content pointed to by pointer B. Here * B = a; so * B is actually.

That is to say, this sentence is equal to a = "programming test"; that is, pointer a points to the new string "programming test ". Note that when a pointer is directly = string, the address of the string is assigned to the pointer. Therefore, if int * a = 12; is incorrect, it means to assign 12 to pointer a directly. Because of the different types, an error is prompted. Of course, you can also forcibly convert 12 to a pointer type value and then assign it to the variable.

Third sentence: char * c = ++ a; here the value of a is actually an address, pointing to the first character of the string "programming test", that is, "p ", ++ a is auto-incrementing first. So c actually points to "r".

The fourth sentence is interference. c and a are irrelevant.

Fifth sentence: * ++ c. That is, return an auto-increment address, and then take another character output. The original "r" is "o" after auto-increment ".

Therefore, the output is the character "o ".


Gcc is successfully verified in ubuntu.

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.