Learn point C language (20): Data type

Source: Internet
Author: User
Tags printf

1. Variable addresses can be obtained through & variables:

#include <stdio.h>

int main(void)
{
  int num = 10;

  printf("变量值: %d\n",num);
  printf("变量址: %d\n",&num);

  getchar();
  return 0;
}

2. A variable that represents a variable address is a pointer:

#include <stdio.h>

int main(void)
{
  int num = 10;
  int *p = &num;

  printf("%d,%p\n",num,p);

  getchar();
  return 0;
}

3. * The pointer is the same as the variable itself:

#include <stdio.h>

int main(void)
{
  int num = 10;
  int *p = &num;

  printf("%d,%p,%d\n",num,p,*p);

  *p = 11;
  printf("%d,%p,%d\n",num,p,*p);

  (*p)++;
  printf("%d,%p,%d\n",num,p,*p);

  num = 99;
  printf("%d,%p,%d\n",num,p,*p);

  getchar();
  return 0;
}

4. Be aware of initialization when declaring pointers, and no pointers to initialize are dangerous:

#include <stdio.h>

int main(void)
{
  int n1 = 11;
  int n2 = 22;
  int *p = NULL; /* 初始化为空 */

  p = &n1;
  printf("%d,%p,%d\n",n1,p,*p);

  p = &n2;
  printf("%d,%p,%d\n",n2,p,*p);

  getchar();
  return 0;
}

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.