如何才能字符指针与字符串(字符数组),而不是一个int指针整数数组初始化? [英] How can a char pointer be initialized with a string (Array of characters) but an int pointer not with an array of integer?

查看:370
本文介绍了如何才能字符指针与字符串(字符数组),而不是一个int指针整数数组初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何字符指针来用字符串(字符数组),但一个 INT 有没有指针初始化整数数组?

How can char pointer be initialized with a string (Array of characters) but an int pointer not with a array of integer?

当我尝试这样做

int* a={1,2,3,4,5};

它给出​​了一个错误说

It gives an error saying

错误:标量对象'一'需要初始化一个元素

error: scalar object ‘a’ requires one element in initializer

但是,

char* name="mikhil"

完美的作品。

推荐答案

在你正试图初始化情况下为int *

In case of you're trying to initialize an int * with

  {1,2,3,4,5};

这是错误的,因为 {1,2,3,4,5}; ,按原样,不是的整数数组的。这是初始的一个括号括起来的列表。这可以被用来初始化一个 INT 阵列(该阵列的各个元素,具体而言),而不是一个指针。 数组是不是指针,反之亦然。

it is wrong because {1,2,3,4,5};, as-is, is not an array of integers. It is a brace enclosed list of initializer. This can be used to initialize an int array (individual elements of the array, to be specific), but not a pointer. An array is not a pointer and vice-versa.

不过,你可以使用一个复合文字以初始化为int * ,像

However, you can make use of a compound literal to initialize an int *, like

int * a = (int []){1,2,3,4,5};

这篇关于如何才能字符指针与字符串(字符数组),而不是一个int指针整数数组初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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