我知道如何创建一个结构数组但具有预定义的大小.但是有没有办法创建一个动态的结构数组,以便数组可以变得更大?
例如:
typedef struct
{
char *str;
} words;
main()
{
words x[100]; // I do not want to use this, I want to dynamic increase the size of the array as data comes in.
}
Run Code Online (Sandbox Code Playgroud)
这可能吗?
我研究了这个: words* array = (words*)malloc(sizeof(words) * 100);
我想摆脱100并存储数据.因此,如果76个数据字段进来,我想存储76而不是100.我假设我不知道有多少数据会来进入我的计划.在我上面定义的结构中,我可以创建第一个"索引":
words* array = (words*)malloc(sizeof(words));
Run Code Online (Sandbox Code Playgroud)
但是我想在之后动态地向数组中添加元素.我希望我能够清楚地描述问题区域.主要挑战是动态添加第二个字段,至少这是目前的挑战.
然而,我取得了一些进展:
typedef struct {
char *str;
} words;
// Allocate first string.
words x = (words) malloc(sizeof(words));
x[0].str = "john";
// Allocate second string.
x=(words*) realloc(x, sizeof(words));
x[1].FirstName = "bob"; …Run Code Online (Sandbox Code Playgroud) 可能重复:
C# - 如何获得计算机的RAM总量?
以下将检索可用的ram数量:
PerformanceCounter ramCounter;
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
Console.WriteLine("Total RAM: " + ramCounter.NextValue().ToString() + " MB\n\n");
Run Code Online (Sandbox Code Playgroud)
当然我们必须使用System.Diagnostics; 类.
performancecounter是否具有检索特定计算机的RAM量的任何功能?我不是在谈论使用或未使用的ram数量.我说的是机器的内存量.
$almostallTech=array();
$almostallTech[]="no";
$almostallTech[]="no";
$almostallTech[]="yes";
$almostallTech[]="yes";
$almostallTech[]="no";
$almostallTech[]="yes";
$almostallTech=array_unique($almostallTech);
printf("size of array: %d<br/>", sizeof($almostallTech));
for ($x = 0; $x < (sizeof($almostallTech)); $x++) {
printf("%s", $almostallTech[$x]);
}
Run Code Online (Sandbox Code Playgroud)
调用unique方法后,返回大小为2 - 这是正确的.然而,for循环给出了未定义的偏移误差.
进一步检查,如果我打印出来:
printf("%s", $almostallTech[0]); - I get no
printf("%s", $almostallTech[2]); - I get yes
printf("%s", $almostallTech[1]); - undefined offset error
Run Code Online (Sandbox Code Playgroud)
因此,唯一的功能是删除重复项,但保留前一个数组的相同索引 - 这是它的工作原理.这应该很简单,但无法弄清楚如何删除空或更具体的未定义索引.尝试了array_filter但仍然无法正常工作.有什么建议?
我想要的是在调用array_unique方法之后,删除重复项但应该应用新索引.即:我希望$ almostallTech [0]包含"否"我希望$ almostallTech [1]包含"是"
我在变量中有动态值,每个变量用[和]例如
var data="[Continuing] [Returning] "; or
var data="[ACCT_BBA] "; or
var data="[12001] [12009] [21077] [13880] ";
var data="[13880] ";
Run Code Online (Sandbox Code Playgroud)
有没有办法使用split函数从上面提取[和]之间的值?
var arr= data.split("<what goes here?>");
Run Code Online (Sandbox Code Playgroud)
例如,在最后一个检索示例:12001,12009,21077,13880