Question:
C programming. Can you interpret this line for me (arrays)?
Ray
2013-01-12 02:24:00 UTC
Hi guys

I'm new to arrays and pointers. the fact that English isn't my native language makes it all worse..

have been reading an article that brings fegts, arrays and pointers together. here is the command:

char *fgets(char *restrict s, int n, FILE *restrict stream);

and here the description:

"The fgets() function shall read bytes from stream into the array pointed to by s, until n-1 bytes are read, or a is read and transferred to s, or an end-of-file condition is encountered. The string is then terminated with a null byte."

I'm fully lost...which one is the array's name now? can you describe in your own words what this command line says?

thanks
Three answers:
Chris
2013-01-12 04:35:01 UTC
This comes down to the distinction between passing something to a function by reference or not.

The asterisk at the start of the variable name means that the variable doesn't contain any data itself, it merely points at the location in memory where the data is actually stored.



Think of it like this: say you have a mail robot that can deliver packages. You can either give him a package (variable), or you can give him the number of a box that can hold a package (pointer).



The advantage of the latter case is that you can also send the robot to fetch a package, telling him the number of the box he's supposed to put the package in.

This is exactly what fgets() does; the parameter it needs is the location of an array of chars, not the values contained in that array. Thus you're passing a pointer to the function (also called "passing by reference") as opposed to the actual data, and the function can now put the data it reads off the file into the array.
Robert
2013-01-12 02:45:08 UTC
C's fgets does not deal with "string"* arrays in any clever way itself (though you can use it to help process "string" arrays), but it does a good job of reading records delimited by line feed (ascii code 10) from text files.



The source link shows this example of fgets use, and around its use there is an array of character pointers whose memory is dynamically allocated using malloc:



"

char inbuf[255];

FILE *afile = fopen("afile.txt", "r");

// allocate array of 10 pointers and initialize them all to 0

char* pString[10] = {0};

int i;

for(i = 0; i < 10 && fgets(inbuf, sizeof(inbuf), afile); i++)

{

// allocate memory for the string

pString[i] = malloc(strlen(inbuf+1);

// copy the string to the pString array

strcpy(pString[i], inbuf);

}





// do more as needed



fclose(afile);

"





So fgets arguments are:



*

1. a character buffer to hold the data of one text file record (suppose you could call this a character array ... but it is more meaningful to think of it as a "string" (not a C data type concept)) ... say this because it is good to differentiate this from char[][] data types which really are arrays of "strings" ... C++ and C# do have "string" data types

2. a maximum size of this buffer

3. a FILE pointer to a file previously opened using fopen
2016-12-15 10:39:54 UTC
The compiler is calling forward to you to grant it a quantity interior the "double" format, yet you have given it a quantity in "drift" format. the version between those 2 kinds is kinda awkward, with out going into too lots factor, yet to shrink a protracted tale short, once you write "7.2f", the f on the tip potential you're telling the computing gadget that this quantity is a "drift". If, instead, you wrote "7.2nd", you would be telling it the quantity is a double, and it is going to repair your errors. wish that enables.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...