I am trying to compare patterns. So I have structs which hold the patterns as strings, however I want to be able to build a string and store the VALUE of that string in the struct. At the moment, I am only copying the address of my string.
typedef struct
{
int emp;
char *context;
int numOnes;
int numZeros;
}Pattern;
char *patt, str[t+1];
patt = str;
while(count<t){
//printf("Stream: %d\n", stream[end]);
if(stream[end] == 1){
patt[count]= '1';
//writeBit(1);
}
else{
patt[count]='0';
//writeBit(0);
}
end--;
count++;
}
patt[count]=0;//building string
if(found == 0){//if pattern doesnt exist, add it in
patterns[patternsIndex].context = patt; //NEED HELP HERE. This copies the address not the actual value, which is what i need
patterns[patternsIndex].emp = 1;
prediction = 0;
checkPredict(prediction,stream[end],patternsIndex);
patternsIndex++;
found =1;
}
strcpy()
. If you start off with just a pointer, rather than an array, you'll also need to allocate memory for it before you copy, since you can't copy a string until you have some valid memory to copy it into.