I am pretty confused with pointers in C. I am finding it hard to wrap my mind around creating them and passing stuff around? I have a “Segmentation Fault: 11” error after I added code, in which previously it worked. Needed to add something. This is part of the code:
char *token2;
char *line2;
char comma_loc = 0;
int num_of_commas = 0;
char *line2[1];
while(token != NULL) { //lets make sure token has a string token
//printf("Wats in token: %s\n", token);
if(key==true) {
//printf("This should be an identifier: %s\n", token);
if(comma != true) { //added if statement, just take away if it fails, the first case is the original
int len = strlen(token);
iden_holder[iden_holder_count] = (char *)malloc(sizeof(char) * (len +1));
memcpy(iden_holder[iden_holder_count], token, len +1);
iden_holder_count++;
key = false;
} else {
int len2 = strlen(token);
line2[0] = (char *)malloc(sizeof(char) * (len2 + 1));
memcpy(line2[0], token, len2 + 1);
token2 = strtok(line2[0],",");
while(token2 != NULL) {
int len = strlen(token2);
iden_holder[iden_holder_count] = (char *)malloc(sizeof(char) * (len +1));
memcpy(iden_holder[iden_holder_count], token, len +1);
iden_holder_count++;
token2 = strtok(line2[0],",");
}
key = false;
}
Point of this code is to take the string within token and copy it into another token, in my case token2. I decided to use memcpy, but I am confused how to use it due to the pointers confusion. I should also note that I used strtok before this, and the code here is within in. Could it be that if I use it again that it will override the other one?
Read this completely. It will help you with your basics. It did to me. 🙂