Pointer to structure problem

This is a discussion on Pointer to structure problem within the C Programming forums, part of the General Programming Boards category; I am reading a file containing structure. I can't seem to print first 14. I am only printing the 15th ...

  1. #1
    unregistered
    Guest

    Question Pointer to structure problem

    I am reading a file containing structure. I can't seem to print first 14. I am only printing the 15th structure. Can you help?
    // ArrayPointerRead.c

    #include <stdio.h> /* Function Prototype(s) */
    #include <string.h>
    #include <stdlib.h>

    #define MAX 15
    #define NUM 1

    struct employee /* Structure Definition */
    {
    char name[31]; /* Variable Declaration(s) */
    char address[45];
    char pnumber[10];
    };

    struct employee *empArray[MAX];
    //empArray[MAX] = (struct employee *) malloc(sizeof(struct employee));

    void
    readFile (struct employee *array[MAX])
    {
    int i; /* Variable Declaration(s) */
    char file_name[20] = {"emp_in.dat"};
    FILE * fin;
    char name[31]; /* Variable Declaration(s) */
    char address[45];
    char pnumber[10];

    fin = fopen(file_name, "r");

    array[MAX] = (struct employee *) malloc(sizeof(struct employee));


    printf ("*** Start of reading***\n\n");
    for(i = 0; i < MAX; i++) /* Loop for MAX times; 15 names */
    {

    fgets (name, 32, fin);
    fgets (address, 46, fin);
    fgets (pnumber, 11, fin);

    strcpy (array[MAX]->name, name);
    strcpy (array[MAX]->address, address);
    strcpy (array[MAX]->pnumber, pnumber);

    printf ("%s", array[MAX]->name);
    printf ("%s", array[MAX]->address);
    printf ("%s\n", array[MAX]->pnumber);

    }
    fclose(fin);
    }

    void
    printArray (struct employee *empArray[MAX])
    {
    int i;
    for(i = 0; i < MAX; i++) /* Loop for MAX times; 15 names */
    {
    printf ("%s", empArray[MAX]->name);
    // printf ("%s\n", empArray[MAX]->address);
    // printf ("%s\n", empArray[MAX]->pnumber);

    }
    }


    void
    main(void)
    {

    // struct employee *empArray[MAX];

    readFile(empArray); // passing empArray to read_file ( array )
    printArray (empArray);
    }


    Input file):
    Givens, Matthew A.
    1432 E. Woodmere Lane
    272-0985
    Andrew, Marsha L.
    12 E. South Blvd.
    288-9984
    Darrow, Clarence
    100 N. Radon Pkwy.
    288-3862
    Thompson, Cleo M.
    360 Circle Circle
    277-0001
    Childers, Maria
    33 Wild Way
    288-2777
    Wilkinson, George
    555 Razor Drive
    555-7463
    Anderson, Julian R.
    6 Sixth Street
    661-8676
    Brady, Marsha
    421 Sickly Sweet Circle
    678-1234
    McCoy, Kilroy
    712 Grafitti Lane
    283-9255
    Locke, Sandra
    818 Prospector Way
    223-3445
    Michealson, Michael
    24908 Centerpark Drive
    154-0098
    Lear, Chanda
    812 Plantation Drive
    245-0934
    Rounder, Johnathon
    287 Jason Drive #32
    297-4771
    Lincoln, Linda
    25 Liberation Plaza
    937-3733
    Adams, Patricia
    25 Carpenter Lane
    256-0095

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >printf ("%s", empArray[MAX]->name);
    should be
    printf ("%s", empArray[i]->name);

    As it is you are only printing the MAXth element to the screen, which is the last structure. Anywhere you read to empArray or print from it you will need to use the counter i instead of the MAX constant or this problem will persist.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest

    Question

    Thanks. I changed the MAX to i, but they are all showing as NULL. I must be doing something wrong with the pointers but can't figure out what.

  4. #4
    Unregistered
    Guest

    Smile

    Thanks, I got it working. I also moved the malloc statement inside the for-loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dereferencing a Void Pointer in a Structure
    By simpsonseric in forum C Programming
    Replies: 5
    Last Post: 01-17-2009, 04:58 PM
  2. Pointer to structure problem
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-07-2008, 10:46 PM
  3. double pointer to a structure
    By rohit_second in forum C Programming
    Replies: 5
    Last Post: 11-25-2008, 04:32 AM
  4. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 08:37 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21