so what am i missing? i have two completely isolated functions that only receive a pointer that is immediatly duplicated. They do similar jobs (count letters, count lines), same while constraint. very similar code. I'm missing something.
This is a discussion on Advice requested, Code makes sense to me, not compiler within the C Programming forums, part of the General Programming Boards category; so what am i missing? i have two completely isolated functions that only receive a pointer that is immediatly duplicated. ...
so what am i missing? i have two completely isolated functions that only receive a pointer that is immediatly duplicated. They do similar jobs (count letters, count lines), same while constraint. very similar code. I'm missing something.
getword was written a while ago, back in the "structuring" stages, also all those undeclared identifiers are skeleton code from a different attempt, once its working i'll tidy it up ofocourse
Compare the following code:
(Note the copying of file pointers inside the function.) Tell me what you think it does, then run it and see what it does.Code:#include <stdio.h> #include <stdlib.h> void readfile(FILE *fileptr) { FILE *ip; char nextfive[6]; ip = fileptr; fgets(nextfive, 6, ip); printf("%s\n", nextfive); } int main(void) { FILE *infile; infile = fopen("test.txt", "r"); readfile(infile); readfile(infile); readfile(infile); fclose(infile); return 0; }
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
the structure idea is to eventually get this all sifted down to one function call, one function input, (getword(wordtype)) that returns a string that i can call to generate the arrays (already declared) that are just used to generate the final printf, but one step at a time.
Listen to tabstop, please. Your understandings of FILE* pointers is probably incorrect.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
ok, point taken, readjusting understandings. I'll post the rewritten code asap
if that is the incorrect way to handle such pointers, what is the correct way?
copy the pointers in the main()?
I think I'm going to test you a little, as well.
Can you tell me the output of this program?
Code:typedef struct { int a, b, c; } MyStruct; void foo1(MyStruct* pStruct) { MyStruct* pLocalStruct = pStruct; pLocalStruct ->a = 1; printf("%i", pStruct->a); } void foo2(MyStruct* pStruct) { MyStruct* pLocalStruct; pLocalStruct = malloc( sizeof(MyStruct) ); *pLocalStruct = *pStruct; pLocalStruct->a = 1; printf("%i", pStruct->a); } int main() { MyStruct s; s.a = s.b = s.c = 50; printf("Before foo1, a = %i\n", s.a); foo1(&s); printf("After foo1, a = %i\n", s.a); s.a = s.b = s.c = 50; printf("Before foo2, a = %i\n", s.a); foo2(&s); printf("After foo2, a = %i\n", s.a); return 0; }
Do not copy FILE* pointers.
Last edited by Elysia; 01-05-2008 at 03:53 PM.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
Is this so? I thought you couldn't actually copy the FILE structure itself and guarantee it would then refer to any real file, but that copying the pointer was ok (it just gives you another handle to the same file) -- after all, if we pass a FILE * to a function, we make a local copy of it, do we not?
Perhaps the wording was incorrect. Yes, you shouldn't make copies of the FILE structure itself.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
as i read it foo1 sets the value of a inside MyStruct s to a by pointer assignment via a local structure pointer thats a copy of the pointer to s.
foo2 does the same operation by making a copy of the entire structure as opposed to the pointer to the structure.
but, i dunno wether im reading this wrong or not, but the only actual operations on the pStruct pointer are
pStruct->a = 1;
printf("%i", pStruct->a);
in both cases,
so they both do the same thing the same way..... sorry, i must be missing something again..
Okay: now change both of the pStruct->a=1 lines to pLocalStruct->a = 1. Do they still do the same thing?
to answer your question, before foo1, 50, after 1, before foo2, 50, after, 1
wont make a difference to the output of foo1, but foo 2 stays at 50 afterwards because the ->1 only applies to the local structure copy, is that close for an explanation?