函数指针等的一些用法总结

// test.cpp : Defines the entry point for the console application.
//
/*
#include "stdafx.h"
#include<iostream>
#include<fstream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    
    fstream file("d:\ttt.txt");

    string str = "1234";
    file << str;

    string str1;
    file >> str1;

    cout << str1;
    


}
*/

//template<class T>


#include "stdafx.h"

/*
#include <iostream>
#include <fstream>
#include<string>
using namespace std;

//C++写文件和读文件
int main()
{
    ofstream out("D:\\out.txt");
    if (out.is_open())
    {
        out << "This is a line.\n";
        out << "This is another line.\n";
        out.close();
    }
    //out.flush();
    ifstream in("D:\\out1.txt");
    if (!in.is_open())
    {
        cout << "open failed\n";
        return -1;
    }
    
    while(!in.eof())
    {
        string str;
        getline(in, str);//包含头文件 string
        cout << str<<endl;
    }

    in.close();

    return 0;
}
*/

//函数指针
//int (*pFun)(int i ,int j);

/*
#include<iostream> 

#include<vector> 

#include<cstdlib> 

using namespace std;

int main()
{
    vector<int> vec;
    //vec.resize(5);
    vec.push_back(1);
    vec.push_back(2);
    vec.push_back(3);
    
    for (int i = 1; i < 8; ++i)
    {
        vec.push_back(i);

        cout<<"size: " << vec.size() << " " <<"capacity "<< vec.capacity() << endl;
    }

    //cout << vec.size() << " " << vec.capacity() << endl;

    //vec.push_back(6);

    //cout<<"After push  " << vec.size()<<" " <<vec.capacity()<<endl;
    
}
*/

/*
//函数指针
int(*pFun)(void);

typedef int(*pFun)(int, int);

#include <windows.h>
#include <iostream>
using namespace std;
typedef int(*AddFunc)(int a, int b);
int main(int argc, char *argv[])
{
    HMODULE hDll = LoadLibrary("DLLDemo1.dll");
    if (hDll != NULL)
    {
        AddFunc add = (AddFunc)GetProcAddress(hDll, "Add");
        if (add != NULL)
        {
            cout << add(2, 3) << endl;
        }
        FreeLibrary(hDll);
    }
}
*/

#include<iostream>
#include<string.h>
#include<memory.h>
using namespace std;

struct Node
{
    int nData;
    Node *pNext;

    Node()
    {
        nData = 0;
        pNext = NULL;
    }
};


int Insert(Node *pNode, int val)
{
    Node * p1 = pNode->pNext;
    Node * p2 = p1->pNext;
    while (p2->nData<val)
    {
        pNode++;
        p1 = pNode->pNext;
        if (p1->pNext)
        {
            p2 = p1->pNext;
        }
    }
    Node *pNew = new Node();

    p1->pNext = pNew;
    pNew->pNext = p2;

    return 0;
}

struct MyStruct
{
    int a;
    char b[5];
};

void test(char **p)
{
    static char s_Array[] = "test";
    *p = s_Array;
}

typedef int(*PFun)(int, int);

int add(int a, int b)
{
    return a + b;
}

int main()
{
    /*
    int i = 4;
    i <<= 3;
    cout << "----i " <<i;
    */


    /*
    //sizeof和strlen求字符串长度,strlen求得的大小不包含'\0',sizeof包含'\0'
    char *p = "Hello World";
    int n = sizeof(p);
    int nLength = strlen(p);

    cout << "sizeof p:" << n << endl;
    cout << "strlen:" << nLength<<endl;

    char strArray[] = "Hello World";
    int nn = sizeof(strArray);
    int nLen = strlen(strArray);

    cout << "sizeof Array:" << nn << endl;
    cout << "strlen Array:" << nLen << endl;
    */

    
    /*
    //sizeof对指针和结构体求字节数
    MyStruct *pMyStruct = (MyStruct *)malloc(sizeof(MyStruct));
    int n = sizeof(pMyStruct);
    int nn = sizeof(*pMyStruct);
    int nnn = sizeof(MyStruct);

    memset(pMyStruct, 0, nn);
    

    //memset_s(pMyStruct, nn,0,nn);

    cout << "sizeof *pMyStruct: " <<n<<"\n" ;
    cout << "sizeof (*pMyStruct): " << nn << "\n";
    cout << "sizeof MyStruct: " << nnn << "\n";
    */

    /*
    //二级指针用法
    char *pp = new char[20];

    test(&pp);

    */


    /*
    //函数指针的用法
    PFun pFun = add;
    int a = pFun(1, 2);
    int aa = (*pFun)(1, 2);
    cout<<"<pFun(1, 2)=: "<<a<<endl;
    cout << "(*pFun)(1,2)=: " <<aa<<endl;
    */
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wang_anna

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值