问题
I'm new to C but I've programmed in pascal a few weeks ago. In pascal, if you want to change an arrays data, you pass by reference, by typing var myArray
essentially. I can't figure out how to do this in C. I've read a ton of questions but none seem to work. Here's what I have so far.
void set_up_elements(char (*array_to_populate)[20])
{
char* buffer;
FILE *f;
f=fopen("elementList.txt","r");
char copied_text[118][20];
int i=0;
while (!feof(f))
{
fgets(copied_text[i],80,f);
++i;
}
//Close the file to free up memory and prevent leaks
fclose(f);
f = NULL;
}
Here is my code to populate the array, i read a list of the elements in the periodic table in to the array copied_text
. This part works, it successfully populates the array that is INSIDE the function.
int main()
{
char element_array[118][20];
set_up_elements(element_array);
<..>
}
This is how i'm trying to call it. The contents of the array element_array
doesn't change. Does anyone know how to fix this? Thanks.
回答1:
Apply the right-left reading rule in understanding the problem...
char element_array[118][20]
means that element_array is an array of 118 blocks of 20 chars.
On the other hand, in char (*array_to_populate)[20]
array_to_populate is a pointer to an array of 20 chars because of the parens.
You could use char (&array_to_populate)[118][20]
which means that array_to_populate is a reference to 118 blocks of 20 chars.
That said, the big issue is that in your function, you are reading into a temporary array that you never copy into the passed in array. In addition, your file read is the wrong size. The following should get the job done...
#define DIM1 118
#define DIM2 20
void set_up_elements(char (&array_to_populate)[DIM1][DIM2])
{
int i=0;
FILE *f=fopen("elementList.txt","r");
for (i = 0; f && !feof(f) && i < DIM1; i++ )
{
fgets(array_to_populate[i],sizeof(array_to_populate[0]),f);
}
//Close the file to free up memory and prevent leaks
fclose(f);
}
回答2:
1) The following line is wrong
fgets(copied_text[i],80,f);
the size of one element string in your array of strings is 20
. and you put as maximum chrachter to read in your fgets()
function is 80
so change it to :
fgets(copied_text[i],sizeof(copied_text[0]),f);
2) and you can change this code:
while (!feof(f))
{
fgets(copied_text[i],80,f);
++i;
}
with
while (i<118 && fgets(copied_text[i++],80,f));
3) The input parameter of your function
void set_up_elements(char (*array_to_populate)[20])
is not used in your function. it seems that you want to fill it in your function but it's not the case and you are using another array (local array) which could not be seen by the caller
回答3:
void set_up_elements(char (*array_to_populate)[20])
{
char* buffer;//unuse var
FILE *f;
f=fopen("elementList.txt","r");
int i=0;
while (!feof(f))//caution
{
fgets(&(*array_to_populate[i]),20,f);
++i;
}
//Close the file to free up memory and prevent leaks
fclose(f);
f = NULL;
}
来源:https://stackoverflow.com/questions/16196965/function-to-change-array-data-data-not-changing-c