分配一个单独的语句整个数组 [英] Assigning an entire array with a single statement

查看:86
本文介绍了分配一个单独的语句整个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我声明并初始化

  int类型的[3] = {1,2,3};

如何我后来asisgn在一整个阵列一举?即。

  A = {3,2,1};


解决方案

如果您的C编译器支持复合文字,可以使用的memcpy

 的memcpy(A,(INT []){3,2,1}的sizeof一);

如果您不打算坚守在那里的任何变量(可以是未C99惊艳?),(INT [])可以通过<取代code>(const int的[])把文字变成静态存储器。

Let us say that I declare and initialize

int a[3] = {1, 2, 3};

How can I later asisgn the entire array in one fell swoop? i.e.

a = {3, 2, 1};

解决方案

If your c compiler supports compound literals, you can use memcpy:

memcpy(a, (int[]){3, 2, 1}, sizeof a);

If you don't plan to stick any variables in there (you can; isn't C99 amazing?), (int[]) can be replaced by (const int[]) to put the literal into static memory.

这篇关于分配一个单独的语句整个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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