[英]Regarding passing pointers in C
I am new to C. 我是C的新手。
I am trying to build a data structure which stores 10 strings (which I call commands
in the code comments). 我试图建立一个存储10个字符串的数据结构(我在代码注释中称为
commands
)。
Right now, it looks like I'm having trouble with my insertCommand
function. 现在,看来我的
insertCommand
函数遇到了麻烦。 It looks like it's not inserting the commands. 好像没有插入命令。 I'm looking for where the bug as, as well as anything else that may be immediately wrong with the code.
我正在寻找错误的位置以及其他可能立即导致代码错误的错误。
#include <stdio.h>
#include <string.h>
#define MAX_COMMANDS 10
#define MAX_STRING_LENGTH 100
struct commandStorage
{
/* When initalizing an @commandStorage structure, one must set mostRecent = 0. */
char stringArray[MAX_COMMANDS][MAX_STRING_LENGTH];
int mostRecent;
};
void insertCommand(struct commandStorage* addressOfcStore, char newCommand[])
{
struct commandStorage cStore;
cStore = *addressOfcStore;
cStore.mostRecent++;
if (cStore.mostRecent >= MAX_COMMANDS)
{
cStore.mostRecent = 0;
}
strncpy(cStore.stringArray[cStore.mostRecent], newCommand, MAX_STRING_LENGTH);
}
void printRecentTen(struct commandStorage cStore)
{
int inx;
int i;
inx = cStore.mostRecent;
for (i=0; i<=9; i++)
{
if (cStore.stringArray[inx] == 0)
continue;
printf("%d %s\n", i, cStore.stringArray[inx]);
inx -= 1;
if (inx < 0)
{
inx = MAX_COMMANDS - 1;
}
}
}
char* retrieveMostRecent(struct commandStorage cStore)
{
int mrInx = cStore.mostRecent;
char *mrString;
mrString = cStore.stringArray[mrInx];
return mrString;
}
char* retrieveNth(struct commandStorage cStore, int n)
{
int cmdInx = 0;
cmdInx += cStore.mostRecent - n + 1;
if (cmdInx < 0)
{
cmdInx = MAX_COMMANDS - cmdInx;
}
return cStore.stringArray[cmdInx];
}
int main()
{
struct commandStorage thisStore;
thisStore.mostRecent = 0;
char* recentCommand;
char one[3] = "One";
char two[3] = "Two";
char three[5] = "Three";
char four[4] = "Four";
char five[5] = "Five";
char six[3] = "Six";
char seven[5] = "Seven";
char eight[5] = "Eight";
char nine[4] = "Nine";
char ten[3] = "Ten";
char eleven[6] = "Eleven";
memset (&thisStore, 0, sizeof(thisStore));
insertCommand(&thisStore, one);
insertCommand(&thisStore, two);
insertCommand(&thisStore, three);
printRecentTen(thisStore);
recentCommand = retrieveMostRecent(thisStore);
printf("%s", recentCommand);
insertCommand(&thisStore, four);
insertCommand(&thisStore, five);
insertCommand(&thisStore, six);
insertCommand(&thisStore, seven);
insertCommand(&thisStore, eight);
insertCommand(&thisStore, nine);
insertCommand(&thisStore, ten);
insertCommand(&thisStore, eleven);
printRecentTen(thisStore);
recentCommand = retrieveMostRecent(thisStore);
printf("%s", recentCommand);
recentCommand = retrieveNth(thisStore, 3);
printf("%s", recentCommand);
return 0;
}
All of your trouble has to do with making a copy of the input struct and then updating the copy rather than the one you passed in. Change as follows: 您的所有麻烦都与制作输入结构的副本,然后更新该副本而不是传递的副本有关。更改如下:
void insertCommand(struct commandStorage* addressOfcStore, char newCommand[])
{
addressOfcStore->mostRecent++;
if (addressOfcStore->mostRecent >= MAX_COMMANDS)
{
addressOfcStore->mostRecent = 0;
}
strncpy(addressOfcStore->stringArray[addressOfcStore->mostRecent],
newCommand,
MAX_STRING_LENGTH);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.