Thread: Advice requested, Code makes sense to me, not compiler

  1. #16
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    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.

  2. #17
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    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

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by andrew.bolster View Post
    Made all changes noted above, and its still not decending into the while loop, so the pointers must be screwing up somewhere, but i dont understand why.

    Each time i create a new pointer, assign it to the original file pointer, so as far as i understand it, thats two separate pointers pointing to the same original file location.

    Then i take the copy, and mess around with it, so the original should still be the same?
    Compare the following code:
    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;
    }
    (Note the copying of file pointers inside the function.) Tell me what you think it does, then run it and see what it does.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Did you miss something?

    Quote Originally Posted by Salem View Post
    > for(i=0;i!=j;)
    This will either loop forever, or not at all.

    > fgets(NULL,20,ip);
    This will simply crash with a segfault.

    > while(!(feof(ip)))
    Read the FAQ as to why using feof() to control a loop is bad.
    Instead, you should write
    while ( fgets(string,20,ip) != NULL )

    Or better
    while ( fgets(string,sizeof(string),ip) != NULL )
    Something like this?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    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.

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Listen to tabstop, please. Your understandings of FILE* pointers is probably incorrect.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    ok, point taken, readjusting understandings. I'll post the rewritten code asap

  8. #23
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    if that is the incorrect way to handle such pointers, what is the correct way?
    copy the pointers in the main()?

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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("&#37;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;
    }

    Quote Originally Posted by andrew.bolster View Post
    if that is the incorrect way to handle such pointers, what is the correct way?
    copy the pointers in the main()?
    Do not copy FILE* pointers.
    Last edited by Elysia; 01-05-2008 at 04:53 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Elysia View Post
    The FILE* value returned is not for you to modify or duplicate. If you do, then the result when doing file operations again on one pointer, then the next, is undefined.
    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?

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Perhaps the wording was incorrect. Yes, you shouldn't make copies of the FILE structure itself.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    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..

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Okay: now change both of the pStruct->a=1 lines to pLocalStruct->a = 1. Do they still do the same thing?

  14. #29
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    to answer your question, before foo1, 50, after 1, before foo2, 50, after, 1

  15. #30
    Syncopated Kestrel andrew.bolster's Avatar
    Join Date
    Nov 2007
    Location
    Belfast
    Posts
    45
    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?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 02-21-2002, 06:05 PM
  2. my code is messed up says the compiler
    By 0927 in forum C++ Programming
    Replies: 11
    Last Post: 01-30-2002, 01:59 PM
  3. Replies: 4
    Last Post: 01-16-2002, 12:04 AM
  4. Replies: 3
    Last Post: 11-04-2001, 03:53 PM
  5. Bad code or bad compiler?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 10-22-2001, 09:08 PM