Is it possible to add an element without modifying anything else to an array pointer? Like is I have int *p = {1, 2, 3, 4};
is there a possible way to add a number, such as 5
so it's equal to {1, 2, 3, 4, 5}
?
是否可以在不修改数组指针的情况下添加元素?就像我有int * p = {1,2,3,4};是否有可能添加数字的方法,如5,所以它等于{1,2,3,4,5}?
3 个解决方案
#1
4
With plain array it's not possible without reallocation.
使用普通数组,没有重新分配就不可能。
I would propose you to use std::vector
, which internally contains continuos block of memory and resize automatically when you add elements to it. Moreover you can get pointer to the internal array and pass it to the functions expecting C array:
我建议你使用std :: vector,它在内部包含continuos内存块,并在向其添加元素时自动调整大小。此外,您可以获取指向内部数组的指针并将其传递给期望C数组的函数:
std::vector<int> v;
int* pv = &v[0];
#2
1
int *p = {1,2,3,4}
will not compile anyway. However you can add to an int*
if you realloc
the pointer. Code below will do what you want.
int * p = {1,2,3,4}无论如何都不会编译。但是,如果重新分配指针,则可以添加到int *。下面的代码将做你想要的。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define DEFAULT_SIZE 4
//Print the array...
void print_array(int*, int);
void print_array(int *p, int len) {
int i;
for (i = 0; i<len; i++)
printf("> %i\n", p[i]);
}
int main() {
int *p = (int*)malloc(sizeof(int) * DEFAULT_SIZE);
int array[] = {1, 2, 3, 4};
memcpy(p, array, sizeof(int) * DEFAULT_SIZE);
printf("Array...\n");
print_array(array, DEFAULT_SIZE);
//Realloc and insert 5
p = realloc(p, sizeof(int)*(DEFAULT_SIZE + 1));
p[DEFAULT_SIZE] = 5;
printf("Modified array...\n");
print_array(p, DEFAULT_SIZE+1);
return 0;
}
#3
0
If you wanted to add an element to an array in C, you need to firstly define an array, not a pointer (and make it sufficiently large):
如果要在C中向数组添加元素,首先需要定义一个数组,而不是指针(并使其足够大):
int arr[ 5 ] = { 1, 2, 3, 4 };
... and then do this:
......然后这样做:
arr[ 4 ] = 5;
Remark: Pointers and arrays are not interchangeable.
备注:指针和数组不可互换。
#1
4
With plain array it's not possible without reallocation.
使用普通数组,没有重新分配就不可能。
I would propose you to use std::vector
, which internally contains continuos block of memory and resize automatically when you add elements to it. Moreover you can get pointer to the internal array and pass it to the functions expecting C array:
我建议你使用std :: vector,它在内部包含continuos内存块,并在向其添加元素时自动调整大小。此外,您可以获取指向内部数组的指针并将其传递给期望C数组的函数:
std::vector<int> v;
int* pv = &v[0];
#2
1
int *p = {1,2,3,4}
will not compile anyway. However you can add to an int*
if you realloc
the pointer. Code below will do what you want.
int * p = {1,2,3,4}无论如何都不会编译。但是,如果重新分配指针,则可以添加到int *。下面的代码将做你想要的。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define DEFAULT_SIZE 4
//Print the array...
void print_array(int*, int);
void print_array(int *p, int len) {
int i;
for (i = 0; i<len; i++)
printf("> %i\n", p[i]);
}
int main() {
int *p = (int*)malloc(sizeof(int) * DEFAULT_SIZE);
int array[] = {1, 2, 3, 4};
memcpy(p, array, sizeof(int) * DEFAULT_SIZE);
printf("Array...\n");
print_array(array, DEFAULT_SIZE);
//Realloc and insert 5
p = realloc(p, sizeof(int)*(DEFAULT_SIZE + 1));
p[DEFAULT_SIZE] = 5;
printf("Modified array...\n");
print_array(p, DEFAULT_SIZE+1);
return 0;
}
#3
0
If you wanted to add an element to an array in C, you need to firstly define an array, not a pointer (and make it sufficiently large):
如果要在C中向数组添加元素,首先需要定义一个数组,而不是指针(并使其足够大):
int arr[ 5 ] = { 1, 2, 3, 4 };
... and then do this:
......然后这样做:
arr[ 4 ] = 5;
Remark: Pointers and arrays are not interchangeable.
备注:指针和数组不可互换。