Reputation: 61
I am new to C.
I am trying to build a data structure which stores 10 strings (which I call commands
in the code comments).
Right now, it looks like I'm having trouble with my insertCommand
function. 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;
}
Upvotes: 1
Views: 86
Reputation: 1797
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);
}
Upvotes: 2