C/C++语言基础

工作后开始应用的C#,长时间不用C/C++都忘的差不多了,有些知识是不能忘记的,所以简单回顾一下。

主要包括以下内容:
(1)字符、字符串数组、字符串数组指针
(2)sizeof()、strlen()
(3)字符串拷贝strcpy_s()、memcpy()
(4)指针数组、数组指针、二维数组

代码例子如下:(VS2012)


// basic.cpp : 定义控制台应用程序的入口点。
//

// #include "stdafx.h"
// #include "string.h"  // for : strlen(str)
// #include "stdlib.h"  // for : system("pause");
// #include <ostream>  // for :endl
// #include <iostream> // for : cout
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{

//---------------1:字符、字符串数组、sizeof()、strlen()-------------------
    //char c = 'a';
    //char str[]= "chen";
    //printf("%c\n", c);
    //printf("%s\n", str);
    //printf("%d\n", sizeof(str));  //编译期
    //printf("%d\n", strlen(str));  //运行期
    //system("pause");

//--------------------1.1:字符串数组的修改----------------
    //char str[] = "program";
    //str[1] = 'p';
    //printf("%s\n", str);
    //system("pause");

//-----------------1.2:字符串指针,指向常量字符串,不能修改---------------
    //char* str = "program";
    //str[1] = 'p';
    //printf("%s\n", str);
    //system("pause");

//--------------------1.3:字符串常量指针,正确定义,const---------------
    //const char* str = "program";
    //printf("%s\n", str);
    //system("pause");

//--------------------1.4:字符串拷贝、动态new字符串数组---------------
    //char* str = new char[100];
    ////strcpy(str, "program"); //不安全,推荐使用strcpy_s或者memcpy
    //strcpy_s(str, sizeof("program"), "program");
    ////memcpy(str, "program", sizeof("program"));  //OK
    //str[1] = 'p';
    //printf("%s\n", str);
    //system("pause");

//-------1.5:字符串数组定义与动态new动态数组的差别----
    //char str1[100]= "chen";
    //printf("%s\n", str1);
    //printf("sizeof() = %d\n", sizeof(str1));  // 编译期
    //printf("strlen() = %d\n", strlen(str1));  // 运行期
    //printf("\n");
    //char* str2 = new char[100];
    //memcpy(str2, "chen", sizeof("chen"));
    //printf("sizeof() = %d\n", sizeof(str2));
    //printf("strlen() = %d\n", strlen(str2));
    //system("pause");

//---------------2:数组指针、数组元素的指针地址--------------
    //int a[3];
    //int *p = a;
    //p[0] = 1949;
    //p[1] = 1950;
    //p[2] = 1951;
    //cout << a[0] << '\t' << a[1] << '\t' << a[2] << endl;
    //cout << endl;
    //int b[5] = {1};
    //int* m = b;
    //int* n = (int*)&b;
    //cout << m << endl;
    //cout << n << endl;
    //system("pause");

//----------------3:指针数组与数组指针----------------
    //int c[4] = {1,2,3,4};
    //int* a[4]; //指针数组
    //int (*b)[4]; //数组指针
    //b = &c;
    ////将数组c中元素赋给数组a
    //for(int i=0; i<4; i++)
    //{
    //  a[i] = &c[i];
    //}
    //cout << *a[1] <<endl; //输出2就对
    //cout << (*b)[2] <<endl; //输出3就对
    //system("pause");

//----------------3.1 指针数组---------------
    //int a,b,c;
    //int* p[3];
    //p[0] = &a;
    //p[1] = &b;
    //p[2] = &c;
    //*p[0] = 3;
    //*p[1] = 6;
    //*p[2] = 9;
    //cout << a << '\t' << b << '\t' << c << endl;
    //cout << endl;
    //system("pause");

//--------------------3.2:数组指针----------------
    //int a[3];
    //int* q = a;       // a 即是a[0]的首地址
    //int (*p)[3] = &a; // &a 是数组的首地址 
    //(*p)[0] = 3;
    //(*p)[1] = 6;
    //(*p)[2] = 9;
    //cout << a[0] << '\t' << a[1] << '\t' << a[2] << endl;
    //cout << *q << endl;
    //cout << endl;
    //system("pause");

//--------------------4:二维数组-----------------
    //int a[2][3];
    //int* p = a[0]; 
    //int* q = a[1];       
    //p[0] = 1; 
    //p[1] = 2;
    //p[2] = 3;
    //q[0] = 4;
    //q[1] = 5;
    //q[2] = 6;
    //cout << a[0][0] << '\t' << a[0][1] << '\t' << a[0][2] << endl;
    //cout << a[1][0] << '\t' << a[1][1] << '\t' << a[1][2] << endl;
    //system("pause");
   //return 0;
}

推荐一个C语言学习网站,针对每一项都讲的很仔细,个人觉得很好,有兴趣的可以前往学习之。
C语言中文网之C语言深度剖析【进阶篇】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值