是什么在以下情况下指针和阵列之间的区别? [英] What is the difference between pointer and array in the following context?

查看:137
本文介绍了是什么在以下情况下指针和阵列之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的#include< CString的GT;
诠释的main()
    {
    字符* PNAME =新的char [10];
    字符哑[] =假的;
    的strcpy(PNAME + 0虚设); //这是如何从不同 - >这作品
    的strcpy(PNAME [0],假人); //这个...-->错误C2664:strcpy的:
                           //不能转换参数1
                           //从'字符'到'字符*'    }


解决方案

  • PNAME [0]是一个字符数组的第一个元素(有一个字符)

  • PNAME是一个快捷方式到&安培; PNAME [0](一个指向你的数组的第一个元素)

您得到您的错误的原因是因为strcpy的期待指向一个字符(字符*),而不是一个char值(这是PNAME [0])

#include <cstring>
int main()
    {
    char *pName = new char[10];
    char dummy[] = "dummy";
    strcpy(pName + 0,dummy);//how this is different from -->this works
    strcpy(pName[0],dummy);//this one...--> error C2664: 'strcpy' : 
                           //cannot convert parameter 1 
                           //from 'char' to 'char *'

    }

解决方案

  • pName[0] is the first element in a character array (one character)
  • pName is a shortcut to &pName[0] (a pointer to the first element of your array)

The reason you are getting your error is because strcpy expects a pointer to a char (char*), and not a char value (which is what pName[0] is)

这篇关于是什么在以下情况下指针和阵列之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆