Const BASIC Usage

Source: Internet
Author: User

1, const int a=1; At this point A is a constant whose value cannot be changed and must be initialized at the same time as defined

The wrong program:

#include <stdio.h><stdlib.h>int  main () {    constint A;    A=1;    }

A is not initialized at the same time as defined, and then attempts to modify the value of a.

The correct code:

#include <stdio.h><stdlib.h>int  main () {    constint a= 1 ;}

2, const int *p; The content that P points to is not mutable, but you can point the pointer to another type of int variable

The wrong code:

int Main () {    int val=1;     Const int *p=&val;     *p=3;}

At this point, try to modify its point through the pointer to the content, error: Error:assignment of read-only location ' * p '

The correct code:

int Main () {    int val=1;     Const int *p=&val;    Val=3;    printf ("%d\n", *p);}

Although you cannot modify the content that it points to by pointers, you can directly modify the content that it points to by using the name of the original variable.

Correct code 2:

int Main () {    int val=1;     int k=3;     Const int * p=&val;    P=&K;    printf ("%d\n", *p);}

Although it is not possible to change the value of a pointer to an object by using pointers, you can modify the pointer's point, and the pointer p is not necessarily initialized at the same time as defined.

3, int * Const p=&val; At this point P can only point to the variable Val and cannot point to other variables

The wrong code:

int Main () {    int val=1;     int k=3;     int Const p=&val;    P=&K;    printf ("%d\n", *p);}

This will cause an error: Error:assignment of read-only variable ' p '

Correct code 2:

int Main () {    int val=1;     int Const p=&val;     *p=3;    printf ("%d\n", *p);}

Although you cannot change the pointer's direction, you can change the value of the object it points to by using the pointer.

4, const int * Const p=&val; The pointer P's point cannot be changed, and the value of its pointed content cannot be changed by the pointer.

The wrong code:

int Main () {    int val=1;     int k=3;     Const int Const p=&val;    P=&K;     *p=3;}

At this point, there will be two errors:

1) error:assignment of read-only variable ' P ', stating that the point of P cannot be changed;

2) Error:assignment of Read-only location ' * (const int*) p ', stating that P points to the object (val) and cannot modify its value by pointer.

But:

int Main () {    int val=1;     Const int Const p=&val;    Val=3;}

We can still modify the value by the variable name, that is to say, "The value of the object pointed to cannot be changed" means that it cannot be modified by a pointer.

Const BASIC Usage

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.