1

so I have an array that keeps getting overwritten with new values. For example, this is the output:

instruc[0] = PRINTNUM
instruc[1] = PRINTNUM
instruc[2] = PRINTNUM

where PRINTNUM is supposed to be the last thing in the array and the first two elements are supposed to be something else.

Here is my code for the specific segment:

    //array of instructions
char** instruc = malloc(numLines * 200); 

c = fgets(inputString, 200, in_file);

while (c != NULL){
    instruc[i]=inputString;     
    i++;
    c = fgets(inputString, 200, in_file);
}

//print out what's in the array
i=0;
for (i=0; i<numLines; i++){
    printf("instruc[%d] = %s\n", i, instruc[i]);
}

Thanks in advance!

3
  • You're not allocating space for the strings. Commented Apr 21, 2013 at 18:17
  • To be technical, he IS allocating the space, just not using it because he points to the other string instead edit: actually you're right Commented Apr 21, 2013 at 18:19
  • @AK4749: No, he's allocating space for the pointers to the strings, not for the strings themselves. Commented Apr 21, 2013 at 18:27

2 Answers 2

1

There is no memory allocated to store the lines read in from the file. Yes there is memory allocated but instruc is of type char ** and the program is using this memory as if instruc is of char * type.

If you wish to store the pointers to all the records in the file memory must be allocated not only to store the data in the file BUT also memory is required to store the pointers to the start of each record.

In this code it appears the file records are fixed length 200 bytes long. The array of pointers which a variable of char ** would point to is not strictly necessary - it would be possible to determine the start of the next record simply by adding 200 bytes to the char * pointer to the start of the data read from the file.

To store the pointers to the start of each record something like this would be required-:

char **fileRecPtrs = malloc(sizeof(char *) * numLines);
char *instruc = malloc(200 * numLines); 

Then in the loop to read each record of the file-:

c = fgets(instruc, 200, in_file);     
while (c != NULL) {
    fileRecPtrs[i] = instruc;
    instruc += 200;
    i++;
    c = fgets(instruc, 200, in_file);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You're pointing every index in the array to the same memory address. I think you'd be better off using strcpy

5 Comments

If I use 'strcpy(instruc[i], inputString)' I get a segmentation fault.
you need to allocate space for every index first
hmm, I'm pretty new to C so I'm not sure what you mean...do I need to malloc inputString when I declare it, or malloc each time I put something in instruc[i]?
you can malloc before you call strcpy for each instruc[i]
OK, let me see if I got this right. So, I instruc[i] = malloc(200); strcpy(instruc[i], inputString);?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.