2020-12-03C/C++学习00
请注意,本文编写于 726 天前,最后修改于 204 天前,其中某些信息可能已经过时。

目录


void 指针

  1. 没有解释方式的指针,仅仅保存地址
  2. 可以接受任何有解释方式的指针
c
# include <stdio.h>
# include <stdlib.h>

int main()
{
    int n = 999;
    char ch = 'a';
    short int w = 'BA';
    int *pn = NULL;
    char *pch = NULL;
    short int *pw = NULL;

    void *pv = NULL;

    pn = &n;
    pch = &ch;
    pw = &w;

    //有解释方式的指针给以赋值给void指针
    pv = pn;
    //不能将void指针赋值给有解释方式的指针
    //pn = pv

    system("pause");
    return 0;
}

编译错误

  1. 不能对它取内容
  2. 不能做下标运算
  3. 不能做加减法

返回指针值的函数

c
# include <stdio.h>
# include <stdlib.h>
# include <string.h>

char *foo(char *psz)
{
    char *szBuf = "Hello World!";
    return szBuf;    //返回的是值
}

int main()
{
    char *psz =  NULL;
    char szHello[] = "Hello World!";
    printf("%d\r\n",sizeof("Hello"));
    printf("%d\r\n",sizeof(szHello));

    psz = foo(szHello);
    printf(psz);    //printf会将foo栈空间覆盖,所以不确定输出什么


    system("pause");
    return 0;
}

栈

c
# include <stdio.h>
# include <stdlib.h>
# include <string.h>

char *fun()
{
    int n = 999;
    return &n;
}

int main()
{
    int *pn  = fun();
    *pn = 666;
    printf("%d\r\n",*pn);//666,printf还没来的及占用,就把值传递了
    printf("%d\r\n",*pn);//为止
    system("pause");
    return 0;
}

_T和__T

c
# include <stdio.h>
# include <stdlib.h>
# include <tchar.h>


# ifdef __T
# undef __T
# endif
# define __T(x) L ##  x

# ifdef _T
# undef _T
# endif
# define _T(x) __T(x)

# define MYHELLO "Hello"

int _tmain()
{

    wchar_t wTest[] = __T(MYHELLO);
    //wchar_t wTest[] = LMYHELLO;
    wchar_t wTest[] = _T(MYHELLO);//_T解决对宏字符串的处理
    //wchar_t wTest[] = L"Hello";

    system("pause");
    return 0;
}

本文作者:Na1r

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!