C/C++语言基础
2016-03-20 21:56
225 查看
工作后开始应用的C#,长时间不用C/C++都忘的差不多了,有些知识是不能忘记的,所以简单回顾一下。
主要包括以下内容:
(1)字符、字符串数组、字符串数组指针
(2)sizeof()、strlen()
(3)字符串拷贝strcpy_s()、memcpy()
(4)指针数组、数组指针、二维数组
代码例子如下:(VS2012)
推荐一个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语言深度剖析【进阶篇】
相关文章推荐
- Java客户端C++服务端Socket交互通信
- C++面向对象高级编程(下)-Geekband
- C++内联函数和constexpr函数
- C++面向对象高级编程笔记03--GeekBand
- 排序——快速排序(C++)
- 5-23 币值转换 (20分)c语言版
- 【c++】有理数加法
- 【c++】有理数加法
- C++中的static关键字
- C++上机实验2-2
- C++中虚函数工作原理和(虚)继承类的内存占用大小计算
- 一起talk C栗子吧(第一百二十六回:C语言实例--static关键字)
- C/C++学习之路
- C++ 默认实参
- 读书笔记 |Google C++编程风格指南
- [C/C++] C/C++延伸学习系列之STL及Boost库概述
- [c++]链表实现--持续更。
- C++继承和包含
- C++继承和包含
- 利用c语言宏#的用法打日志并统计错误