'insert a pointer variable with the size part of the fgets function.what's difference between p and *p?
char* cp = malloc(sizeof(char) * MAX_STR);
FILE* fp = fopen("paren_matching.txt", "r");
while (fgets(cp,cp, fp) != NULL) { }
(MAX_STR is 100 and the txt file has some sentences)
In this case, I don't understand when fgets works only use cp in the size part of fgets
I used *cp but it makes runtime error. fgets(cp,*cp,fp)!=NULL{}
I think cp's r-value is address then *cp's r-value is what..?
I'll appreciate it if you could tell me the answer
Solution 1:[1]
Because cp is a pointer, *cp is dereference - which basicly means *cp = value that is being pointed at by cp. You used malloc which allocates memory but does not inicialize it to anything. So when you don't put anything inside, it's just garbage there. It could be whatever so it can cause a problem(for example nagative value). Cp on the other hand is a pointer so it's unsigned number, so it will never cause problems.
In either way it probably doesn't do what you wanted it to. The second argmunent of fgets should be MAX_STR, because that is how much you allocated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ježek |
