Thread: problem with arrays

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    7

    problem with arrays

    when I print out my array in one place it works fine, in another function the same code don't work.

    my code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "lab41.h"
    
    int readfil(FILE *filen, person *personer[10]) {
        int counter = 0;
        char mystring[100];
        person p1;
    
        while(feof(filen) == NULL) {
    
                            fscanf(filen,"%s", &p1.fnamn);
                            fscanf(filen,"%s", &p1.enamn);
                			fscanf(filen,"%d", &p1.alder);
                			fscanf(filen,"%d", &p1.vikt);
                			personer[counter] = &p1;
                			
                			/*--------test ---------*/
                			
                            printf("%s", personer[counter]->fnamn);
                            printf("\n");
                            printf("%s", personer[counter]->enamn);
                            printf("\n");
                            printf("%d", personer[counter]->alder);
                            printf("\n");
                            printf("%d", personer[counter]->vikt);
                            printf("\n");
                            
                            /*--------slut test-------*/
                           counter++;
                           }
    		printf("\n-----------------END---------------\n");
        return 0;
        }
    
    
    int skriv_ut_personer(FILE *filen, person *personer[10]) {
    int counter = 0;
    rewind (filen);
    	while(counter < (sizeof(personer)-1)) {
                            printf("%s", personer[counter]->fnamn);
                            printf("\n");
                            printf("%s", personer[counter]->enamn);
                            printf("\n");
                            printf("%d", personer[counter]->alder);
                            printf("\n");
                            printf("%d", personer[counter]->vikt);
                            printf("\n");
    		counter++;
    	}
     free(personer);
    
    return 0;
    }
    
    
    int main() {
    person *personer[10];
    FILE *filen;
    char filnamn[40];
    enum filtyp {db,txt,bin};
    typedef enum filtyp filtyp;
    
    printf("Skriv filnamn: ");
    scanf("%s", &filnamn);
    if((filen = fopen(filnamn, "r")) == NULL) {
                 printf("file don't exist");
    	     exit(1);
            }
    readfil(filen,personer);
    skriv_ut_personer(filen,personer);
    return 0;
    }
    header
    Code:
    typedef struct {
            char fnamn[40];
            char enamn[40];
            int alder;
            int vikt;
            } person;

    output
    Code:
    client128-24:CORSAIR $ ./a.out 
    Skriv filnamn: p.txt
    Johan
    olsson
    32
    64
    nilj
    kall
    31
    56
    frida
    ........
    32
    12
    monika
    ulv
    54
    123
    monika
    ulv
    54
    123
    
    -----------------END---------------
    moni????
    ???W????1???1?X???????8?1??1??
    -1607342536
    0
    
    ???W????1???1?X???????8?1??1??
    -1607342536
    0
    
    ???W????1???1?X???????8?1??1??
    -1607342536
    0
    a.out(607) malloc: *** error for object 0xbffff904: Non-aligned pointer being freed
    *** set a breakpoint in malloc_error_break to debug

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    while(feof(filen) == NULL)
    do not use feof to control loop - read FAQ
    --------------
    also read description of the function you are using, feof never returns NULL, it returns true or false
    ----------------

    personer[counter] = &p1;
    you are storing pointer to the local variable which will be
    1. overwritten on the next loop iteration
    2. destructed as soon as you leave the function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    7
    allright, I change personer[10] to a global varible and that it only print one post. but still the same.

    output.
    Code:
    client128-24:CORSAIR $ ./a.out 
    enter filename:: p.txt
    Johan olsson
    32
    64
    
    -----------------END---------------
    Joha???? ???W????1???1?????????8?1??1??
    -1607342536
    0
    client128-24:CORSAIR
    code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "lab41.h"
    person *personer[10];
    
    int readfil(FILE *filen) {
        int counter = 0;
        char mystring[100];
        person p1;
    
        while(counter < 1) {
    
                            fscanf(filen,"%s", &p1.fnamn);
                            fscanf(filen,"%s", &p1.enamn);
                			fscanf(filen,"%d", &p1.alder);
                			fscanf(filen,"%d", &p1.vikt);
                			personer[counter] = &p1;
                			
                			/*--------test ---------*/
                			
                            printf("%s", personer[counter]->fnamn);
                            printf(" ");
                            printf("%s", personer[counter]->enamn);
                            printf("\n");
                            printf("%d", personer[counter]->alder);
                            printf("\n");
                            printf("%d", personer[counter]->vikt);
                            printf("\n");
                            
                            /*--------slut test-------*/
                           counter++;
                           }
    			  
    		printf("\n-----------------END---------------\n");
        return 0;
        }
    
    
    int skriv_ut_personer(FILE *filen) {
    int counter = 0;
    rewind (filen);
    	while(counter < 1) {
                            printf("%s", personer[counter]->fnamn);
                            printf(" ");
                            printf("%s", personer[counter]->enamn);
                            printf("\n");
                            printf("%d", personer[counter]->alder);
                            printf("\n");
                            printf("%d", personer[counter]->vikt);
                            printf("\n");
    		counter++;
    	}
     
    
    return 0;
    }
    
    
    int main() {
    FILE *filen;
    char filnamn[40];
    enum filtyp {db,txt,bin};
    typedef enum filtyp filtyp;
    
    printf("enter filename: ");
    scanf("%s", &filnamn);
    if((filen = fopen(filnamn, "r")) == NULL) {
                 printf("file don't exist");
    	     exit(1);
            }
    readfil(filen);
    skriv_ut_personer(filen);
    return 0;
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The problem isn't personer, it is with the fact that p1 is a local variable that gets destroyed and has it's memory reused for other purposes once the function that reads the file is finished.

    You either need to allocate memory for person in the function that does the reading, and store that in the personer array, or make personer array not a pointer, but a array of class person.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    7
    allright. I'm trying to allocate memory with malloc but i'm getting a error.

    &p1 = (person*) malloc(sizeof(person));

    error: invalid lvalue in assignment

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, I can see that. You need to make p1 a pointer. You can't set the address of a variable, so &p1 or something like that can not be the left-hand side of = (which is pretty much what "lvalue" means - I mean, something that can go to the left of =).

    Edit: And you should not cast the result of malloc. If you find that you get compiler errors from that, you are probably using a C++ compiler rather than a C compiler.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Jun 2009
    Posts
    7
    Now p1 is a pointer. now i get back to a old problem (p1 was a pointer before) now i get Segmentation Fault at
    fscanf(filen,"%s", p1->alder);


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "lab41.h"
    person *personer[10];
    
    
    int readfil(FILE *filen) {
        int counter = 0;
        char mystring[100];
        person *p1;
    
        while(counter < 1) {
    			        p1 = malloc(sizeof(person));
    			        fscanf(filen,"%s", p1->fnamn);
                        fscanf(filen,"%s", p1->enamn);
                		fscanf(filen,"%s", p1->alder);
                		fscanf(filen,"%s", p1->vikt);
                		personer[counter] = p1;
                			
                			/*--------test ---------*/
                			
                            printf("%s", personer[counter]->fnamn);
                            printf(" ");
                            printf("%s", personer[counter]->enamn);
                            printf("\n");
                            printf("%d", personer[counter]->alder);
                            printf("\n");
                            printf("%d", personer[counter]->vikt);
                            printf("\n");
                            
                            /*--------slut test-------*/
                           counter++;
                           }
    		printf("\n-----------------END---------------\n");
        return 0;
        }
    
    
    int skriv_ut_personer(FILE *filen) {
    int counter = 0;
    rewind (filen);
    	while(counter < 1) {
                            printf("%s", personer[counter]->fnamn);
                            printf(" ");
                            printf("%s", personer[counter]->enamn);
                            printf("\n");
                            printf("%d", personer[counter]->alder);
                            printf("\n");
                            printf("%d", personer[counter]->vikt);
                            printf("\n");
    		counter++;
    	}
     
    
    return 0;
    }
    
    
    int main() {
    FILE *filen;
    char filnamn[40];
    enum filtyp {db,txt,bin};
    typedef enum filtyp filtyp;
    printf("Skriv filnamn: ");
    scanf("%s", &filnamn);
    if((filen = fopen(filnamn, "r")) == NULL) {
                 printf("file don't exist");
    	     exit(1);
            }
    readfil(filen);
    skriv_ut_personer(filen);
    return 0;
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    fscanf(filen,"%s", p1->alder);
    I thought alder was an int? Why are you trying to read a string into it? (And you need & to read into an int variable as well.)

  9. #9
    Registered User
    Join Date
    Jun 2009
    Posts
    7
    It's printing correct now. Thanks! but when it reach return 0 in main() the window close, should it do that? I use DeV-C++

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do something at the end of the program that stops it from closing? Perhaps read something from the console?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a problem regarding dynamic memory and arrays
    By Michty in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2006, 01:26 PM
  2. assignment of arrays problem
    By HumbuckeR in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2006, 04:25 PM
  3. Problem with arrays
    By dogbert234 in forum C++ Programming
    Replies: 2
    Last Post: 03-25-2006, 03:06 AM
  4. Problem with character arrays in classes
    By spoketoosoon in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2004, 03:57 AM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM