赞
踩
#include<iostream> using namespace std; void run(); int main() { run(); } void run() { //int i2 = 555, i1 = 666; //if opened, the result will be 1, which is the space not allocated int i1= 555 , i2 = 666; //跟声明顺序有关,先声明的变量会先被分配到内存空间 int* ptr = &i1; cout << &i1 << " : " << &i2 << endl; ptr--; int i3 = *ptr; cout << i3 << endl;//666 }
#include <stdio.h> int main(int argc, char *argv[]) { // // Uncleaned // // 下面申请的20个字节的内存有可能被别人用过 // char chs[20]; // // 这个代码打印出来的可能就是乱码,因为printf的%s是“打印一直遇到'\0'”。 // printf("%s\n",chs); // Cleaned! int i; char chs[20] = { 0 }; for(i=0; i<sizeof(chs)/sizeof(char); i++) { printf("%d | ",chs[i]); } printf("\n"); int nums[10] = { 7, 5 }; for(i=0; i<sizeof(nums)/sizeof(int); i++) { printf("%d | ",nums[i]); } return 0; }
#include<stdio.h>
int main(){
int nums[] = {3,5,6,7,9};
void* ptr1 = nums;
//int i = *ptr1; // 对于void指针没法直接取值
int* ptr2 = (int*)nums;
printf("%d,%d\n",ptr1,ptr2);
int i = *ptr2;
printf("%d\n",i);
}
#include<stdio.h> #include<string.h> struct _Person { char *name; int age; double height; }; int main(int argc, char *argv[]) { struct _Person p1; // 不初始化内存区域是脏的 printf("Dirty --- p1.age is %d\n",p1.age); // 方法一:使用memset进行清理 memset(&p1,0,sizeof(struct _Person)); printf("Cleaned in Way1 --- p1.age is %d\n",p1.age); p1.name = "Alexander"; p1.age = 25; printf("Cleaned in Way1 --- Name : %s , Age : %d\n",p1.name,p1.age); printf("Cleaned in Way1 --- p1.age is %d\n",p1.age); printf("------------------------------\n"); // 方法二:初始化 struct _Person p2 = { 0 }; p2.name = "刘德华"; p2.age = 60; printf("Cleaned in Way2 --- Name : %s , Age : %d\n",p2.name,p2.age); return 0; }
#include<stdio.h> typedef void (*intFunc)(int i); // Function pointer void test1(int age) { printf("test1:%d\n\n",age); } void foreachNums(int *nums,int len,intFunc func) { int i; for(i=0; i<len; i++) { int num = nums[i]; func(num); // call the function through its pointer } } void printNum(int num) { printf("value=%d\n",num); } int main() { // 声明一个intFunc类型的函数指针 intFunc f1 = test1; // 执行f1函数指针所指向的代码区 f1(8); int nums[] = { 1,5,666,23423,223 }; foreachNums(nums,sizeof(nums)/sizeof(int),printNum); // send the function as pointer }
#include<stdio.h> #include<stdlib.h> ///Initial one using stack //int *getData() //{ // int nums[10]={1,2,3,4,5,6,7,8}; // return nums; //} // //int *getData2() //{ // int aaa[10]={8,7,6,5,4,3,2,1}; // return aaa; //} // //int main(int argc, char *argv[]) //{ // int *nums = getData(); // getData2(); // open and close // printf("%d,%d,%d\n",nums[0],nums[1],nums[2]); // // return 0; //} ///Developed one using heap int *getData() { int *nums = (int*)malloc(sizeof(int)*10); nums[0]=1; nums[1]=8; nums[2]=3; return nums; } int *getData2() { int *nums = (int*)malloc(sizeof(int)*10); nums[0]=2; nums[1]=7; nums[2]=5; return nums; } int main(int argc, char *argv[]) { int *numsptr = getData(); int *numsptr2 = getData2(); // numptr[1]等价于*(numptr+1) printf("%d,%d,%d\n",numsptr[0],numsptr[1],numsptr[2]); // 不要忘记释放内存 free(numsptr); free(numsptr2); return 0; }
#include<stdio.h> void mymemset(void *data,int num,int byteSize) { // char就是一个字节,而计算机中是以字节为单位存储的 char *ptr = (char*)data; int i; for(i=0;i<byteSize;i++) { *ptr=num; ptr++; } } int main(int argc, char *argv[]) { int nums[20]; mymemset(nums,0,sizeof(nums)); int i,len=sizeof(nums)/sizeof(int); for(i=0;i<len;i++) { printf("%d ",nums[i]); } printf("\n"); return 0; }
#include<stdio.h> // 分解一个三位数,传递个位、十位和百位数字 int parseNumber(int num,int* g,int* s,int* b) { if(num < 100 || num > 999) { // 只允许100~999的数字 return -1; } *g = num % 10; *s = (num / 10)%10; *b = (num/100)%10; return 1; } int main(int argc, char *argv[]) { int num = 365; int g,s,b; if(parseNumber(num,&g,&s,&b)) { printf("%d %d %d\n",b,s,g); //3 6 5 } return 0; }
#include <cstdio> #include <algorithm> #include <cstring> using namespace std; //#define LOCAL int d[101][101]; int a[101][101]; int n; int dp(int x, int y) { if(d[x][y] >= 0) { return d[x][y]; } return d[x][y] = a[x][y] + (x == n? 0: max(dp(x + 1, y), dp(x + 1, y + 1))); } int main() { #ifdef LOCAL FILE *fi; fi = fopen("hdu2084.txt", "r"); #endif /* int c; scanf("%d", &c); while(c--) { int a[101][101]; memset(a, 0, sizeof(a)); int n; scanf("%d", &n); for(int i = 1; i <= n; i++) { for(int j = 1; j <= i; j++) { scanf("%d", &a[i][j]); } } for(int j = 1; j <= n; j++) { d[n][j] = a[n][j]; } for(int i = n - 1; i >= 1; i--) { for(int j = 1; j <= i; j++) { d[i][j] = a[i][j] + max(d[i + 1][j], d[i + 1][j + 1]); } } printf("%d\n", d[1][1]); } */ int c; scanf("%d", &c); while(c--) { memset(d, -1, sizeof(d)); scanf("%d", &n); for(int i = 1; i <= n; i++) { for(int j = 1; j <= i; j++) { scanf("%d", &a[i][j]); } } for(int j = 1; j <= n; j++) { d[n][j] = a[n][j]; } dp(1, 1); printf("%d\n", d[1][1]); } return 0; }
#include<iostream> using namespace std; void SwapWayOne(int* x, int* y) { int temp = 0; temp = *x; *x = *y; *y = temp; } void SwapWayTwo(int &x, int &y) { int temp = 0; temp = x; x = y; y = temp; } void failedSwapWay(int x, int y) { int temp = 0; temp = x; x = y; y = temp; } inline void inlineSwapWayOne(int* x, int* y) { //address of x: 0x6ffde0, address of y: 0x6ffde8 cout << "address of x: " << &x << ", " << "address of y: " << &y << endl; //value of address x: 0x6ffe0c, value of address y: 0x6ffe08 cout << "value of address x: " << *(&x) << ", " << "value of address y: " << *(&y) << endl; //value of address x: 0x6ffe0c, value of address y: 0x6ffe08 cout << "value of address x: " << x << ", " << "value of address y: " << y << endl; //value of x: 5, value of y: 9 cout << "value of x: " << *x << ", " << "value of y: " << *y << endl; int temp = 0; temp = *x; *x = *y; *y = temp; } inline void inlineSwapWayTwo(int &x, int &y) { //address of x: 0x6ffe0c, address of y: 0x6ffe08 cout << "address of x: " << &x << ", " << "address of y: " << &y << endl; //value of x: 5, value of y: 9 cout << "value of x: " << *(&x) << ", " << "value of y: " << *(&y) << endl; //value of x: 5, value of y: 9 cout << "value of x: " << x << ", " << "value of y: " << y << endl; int temp = 0; temp = x; x = y; y = temp; } inline void failedSwapThoughInline(int x, int y) { int temp = 0; temp = x; x = y; y = temp; } int main() { int numA = 5, numB = 9; cout << "Original order: " << numA << ", " << numB << endl; // failedSwapWay(numA, numB); // SwapWayOne(&numA, &numB); // SwapWayTwo(numA, numB); // inlineSwapWayOne(&numA, &numB); inlineSwapWayTwo(numA, numB); // failedSwapThoughInline(numA,numB); // inline function's scope is still inside! cout << "After changed: " << numA << ", " << numB <<endl; return 0; }
#include <iostream> using namespace std; class Student { public: Student(char *name, int age, float score); public: friend void show(Student *pstu); //将show()声明为友元函数 private: char *m_name; int m_age; float m_score; }; Student::Student(char *name, int age, float score): m_name(name), m_age(age), m_score(score) { } //非成员函数 void show(Student *pstu) { cout<<pstu->m_name<<"的年龄是 "<<pstu->m_age<<",成绩是 "<<pstu->m_score<<endl; } int main() { Student stu("小明", 15, 90.6); show(&stu); //调用友元函数 Student *pstu = new Student("李磊", 16, 80.5); show(pstu); //调用友元函数 return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。