如何在C语言中将数组X和Y的元素包含在数组total
中?
你能用一个例子来表示吗。
X = (float*) malloc(4);
Y = (float*) malloc(4);
total = (float*) malloc(8);
for (i = 0; i < 4; i++)
{
h_x[i] = 1;
h_y[i] = 2;
}
//How can I make 'total' have both the arrays x and y
//for example I would like the following to print out
// 1, 1, 1, 1, 2, 2, 2, 2
for (i = 0; i < 8; i++)
printf("%.1f, ", total[i]);
答案 0 :(得分:33)
您现有的代码正在分配错误的内存量,因为它根本不考虑sizeof(float)
。
除此之外,您可以使用memcpy
将一个数组附加到另一个数组:
float x[4] = { 1, 1, 1, 1 };
float y[4] = { 2, 2, 2, 2 };
float* total = malloc(8 * sizeof(float)); // array to hold the result
memcpy(total, x, 4 * sizeof(float)); // copy 4 floats from x to total[0]...total[3]
memcpy(total + 4, y, 4 * sizeof(float)); // copy 4 floats from y to total[4]...total[7]
答案 1 :(得分:3)
for (i = 0; i < 4; i++)
{
total[i] =h_x[i] = 1;
total[i+4]=h_y[i] = 2;
}
答案 2 :(得分:2)
当你知道它们的大小时连接两个C数组的方法。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define ARRAY_CONCAT(TYPE, A, An, B, Bn) \
(TYPE *)array_concat((const void *)(A), (An), (const void *)(B), (Bn), sizeof(TYPE));
void *array_concat(const void *a, size_t an,
const void *b, size_t bn, size_t s)
{
char *p = malloc(s * (an + bn));
memcpy(p, a, an*s);
memcpy(p + an*s, b, bn*s);
return p;
}
// testing
const int a[] = { 1, 1, 1, 1 };
const int b[] = { 2, 2, 2, 2 };
int main(void)
{
unsigned int i;
int *total = ARRAY_CONCAT(int, a, 4, b, 4);
for(i = 0; i < 8; i++)
printf("%d\n", total[i]);
free(total);
return EXIT_SUCCCESS;
}
答案 3 :(得分:1)
这里是一种将两个或多个静态分配数组连接在一起的解决方案。静态分配的数组是长度在编译时定义的数组。 sizeof
运算符返回以下数组的大小(以字节为单位):
char Static[16]; // A statically allocated array
int n = sizeof(Static_array); // n1 == 16
我们可以使用运算符sizeof
构建一组宏,这些宏将连接两个或更多数组,并可能返回数组的总长度。
我们的宏:
#include <string.h>
#define cat(z, a) *((uint8_t *)memcpy(&(z), &(a), sizeof(a)) + sizeof(a))
#define cat1(z, a) cat((z),(a))
#define cat2(z, a, b) cat1(cat((z),(a)),b)
#define cat3(z, a, b...) cat2(cat((z),(a)),b)
#define cat4(z, a, b...) cat3(cat((z),(a)),b)
#define cat5(z, a, b...) cat4(cat((z),(a)),b)
// ... add more as necessary
#define catn(n, z, a ...) (&cat ## n((z), a) - (uint8_t *)&(z)) // Returns total length
使用示例:
char One[1] = { 0x11 };
char Two[2] = { 0x22, 0x22 };
char Three[3] = { 0x33, 0x33, 0x33 };
char Four[4] = { 0x44, 0x44, 0x44, 0x44 };
char All[10];
unsigned nAll = catn(4, All, One, Two, Three, Four);
但是,由于定义宏的方式,我们可以串联任何类型的对象,只要sizeof
返回它们的大小即可。例如:
char One = 0x11; // A byte
char Two[2] = { 0x22, 0x22 }; // An array of two byte
char Three[] = "33"; // A string ! 3rd byte = '\x00'
struct {
char a[2];
short b;
} Four = { .a = { 0x44, 0x44}, .b = 0x4444 }; // A structure
void * Eight = &One; // A 64-bit pointer
char All[18];
unsigned nAll = catn(5, All, One, Two, Three, Four, Eight);
使用常量文字,还可以使用以下宏来连接常量,函数结果甚至常量数组:
// Here we concatenate a constant, a function result, and a constant array
cat2(All,(char){0x11},(unsigned){some_fct()},((uint8_t[4]){1,2,3,4}));
答案 4 :(得分:0)
我想我会添加这个,因为我发现过去有必要将值附加到C数组(如Objective-C中的NSMutableArray
)。此代码管理C float
数组并将值附加到其中:
static float *arr;
static int length;
void appendFloat(float);
int main(int argc, const char * argv[]) {
float val = 0.1f;
appendFloat(val);
return 0;
}
void appendFloat(float val) {
/*
* How to manage a mutable C float array
*/
// Create temp array
float *temp = malloc(sizeof(float) * length + 1);
if (length > 0 && arr != NULL) {
// Copy value of arr into temp if arr has values
memccpy(temp, arr, length, sizeof(float));
// Free origional arr
free(arr);
}
// Length += 1
length++;
// Append the value
temp[length] = val;
// Set value of temp to arr
arr = temp;
}
答案 5 :(得分:0)
可能这很简单。
#include <stdio.h>
int main()
{
int i,j,k,n,m,total,a[30],b[30],c[60];
//getting array a
printf("enter size of array A:");
scanf("%d",&n);
printf("enter %d elements \n",n);
for(i=0;i<n;++i)
{scanf("%d",&a[i]);}
//getting aaray b
printf("enter size of array b:");
scanf("%d",&m);
printf("enter %d elements \n",m);
for(j=0;j<m;++j)
{scanf("%d",&b[j]);}
total=m+n;
i=0,j=0;
//concating starts
for(i=0;i<n;++i)
{
c[i]=a[i];
}
for(j=0;j<m;++j,++n)
{
c[n]=b[j];
}
printf("printing c\n");
for(k=0;k<total;++k)
{printf("%d\n",c[k]);}
}
答案 6 :(得分:0)
我喜欢乔恩的回答。就我而言,我必须使用静态解决方案。 因此,如果您被迫不使用动态内存分配:
body {
overflow: hidden;
}
.box {
width: calc(33.3% - 4px);
display: inline-block;
border-radius: 5px;
border: 2px solid #333;
border: #000 border-color: #e6e600;
margin: -2px;
border-radius: 0%;
background-color: #99ffff;
}
.box {
height: 15vh;
display: inline-flex;
align-items: center;
justify-content: center;
cursor: pointer;
}
.box002 {
position: absolute;
top: 27.3vh;
left: 72.98vw;
cursor: pointer;
}
.box002 img {
width: 14.0vw;
height: 23.0vh;
border-radius: 50%;
}
.reset {
position: absolute;
top: 87.8vh;
left: 73.3vw;
cursor: pointer;
}
.reset img {
width: 5.3vw;
height: 11.1vh;
border-radius: 50%;
}
.quit {
position: absolute;
top: 88.3vh;
left: 84.3vw;
cursor: pointer;
}
.quit img {
width: 4.3vw;
height: 9.5vh;
border-radius: 50%;
}
#timer {
font-family: 'Sigmar One', cursive;
margin-top: -20%;
margin-left: 120%;
}
#heading {
font-family: 'Sigmar One', cursive;
color: #F534BB;
}
#container {
white-space: nowrap;
border: px solid #CC0000;
}
.containerr {
border: px solid #FF3399;
}
.pic {
background-size: 100% 100%;
}
.container2 {
width: 35.1vw;
position: fixed;
top: 43.5vh;
left: 13.5vw;
}
.box p {
font-size: calc(2vw + 10px);
}
p {
font: "Courier New", Courier, monospace;
font-size: 30px;
color: rgba(0, 0, 0, 0.6);
text-shadow: 2px 8px 6px rgba(0, 0, 0, 0.2), 0px -5px 35px rgba(255, 255, 255, 0.3);
color: #005ce6;
text-align: center;
}
.text {
padding: 20px;
margin: 7 px;
margin-top: 10px;
color: white;
font-weight: bold;
text-align: center;
}
body {
background-size: 100vw 100vh;
}
.next {
margin-right: 50%;
margin-left: 50%;
margin-bottom: 10%;
float: right;
}
ul {
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
}
.reset img:hover {
opacity: 1
}
#hiddenimagewas {
transform-origin: 50% 50%;
font-size: 50px;
font-family: 'Sigmar One', cursive;
cursor: pointer;
z-index: 2;
text-align: center;
width: 100%;
position: absolute;
top: 8.5vh;
left: 0.3vw;
}
.hiddenimage {
position: absolute;
top: 15.3vh;
left: 10vw;
cursor: pointer;
}
.hiddenimage img {
width: 35.3vw;
height: 45.5vh;
border-radius: 15%;
}
#timetaken2 {
transform-origin: 50% 50%;
font-size: 50px;
font-family: 'Sigmar One', cursive;
cursor: pointer;
z-index: 2;
text-align: center;
width: 100%;
position: absolute;
top: 60.5vh;
left: -12.8vw;
}
答案 7 :(得分:0)
为什么不使用这样的简单逻辑?
#include<stdio.h>
#define N 5
#define M (N * 2)
int main()
{
int a[N], b[N], c[M], i, index = 0;
printf("Enter %d integer numbers, for first array\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Enter %d integer numbers, for second array\n", N);
for(i = 0; i < N; i++)
scanf("%d", &b[i]);
printf("\nMerging a[%d] and b[%d] to form c[%d] ..\n", N, N, M);
for(i = 0; i < N; i++)
c[index++] = a[i];
for(i = 0; i < N; i++)
c[index++] = b[i];
printf("\nElements of c[%d] is ..\n", M);
for(i = 0; i < M; i++)
printf("%d\n", c[i]);
return 0;
}
结果数组的大小必须等于数组a和b的大小。