zoukankan      html  css  js  c++  java
  • C++数组和指针

    先看以下一段代码:

    #include <iostream>
    #include<stdio.h>
    #include<string>
    using namespace std;
    
    int main() {
    	int time[] = {1,2,3};
    	int *q ;
    	q = time;
    	cout<<*q<<" "<<q<<endl;
    	char arr[]="hello world";
    	string *p;
    	string str[] = {"linux","unix"};
    	p = str;
    	cout<<*p<<" "<<p<<endl;
    	char* pc;
    	pc = arr;
    	cout<<arr<<" "<<*pc<<endl;
    	cout<<pc<<endl;
    	string s = "linux";
    	string *ps;
    	ps = &s;
    	cout<<ps<<*ps<<endl;
    
    	return 0;
    }


     

    以下是执行结果:

    1 0x7fff6de21a20
    linux 0x7fff6de21a00
    hello world h
    hello world
    0x7fff6de219f0 linux
    解释:

    一直以来都对指针有点疑惑.所以没事的时候就专门写了这段代码,代码不难.却对我认识指针和数据有很大的帮助.

    样例中的time,str和s的全部的输出,依据书中的描写,就非常easy推断出来输出的数据.唯一让我困惑的就是char*和char[].

    在c和c++中假设直接赋值char* p="hello world",是相当于char arr[]="hello world"; p = arr的,"hello world"是在内在的文字常量区,所以直接输出p的结果是hello world





  • 相关阅读:
    .net经典笔试题
    asp.net面试题
    Java 面 试 题
    .net面试题集
    J2EE面试题集锦
    linux下查看硬件信息
    linux下软件的安装[转]
    在Linux系统下优化Oracle具体步骤
    制作Linux的优盘启动盘
    两端对齐,图片文字列表
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/5281218.html
Copyright © 2011-2022 走看看