Thread: Pointer Problem

  1. #1
    Registered User
    Join Date
    Apr 2002

    Unhappy Pointer Problem

    I declare this at the beginning of my program:

    typedef struct personnelData
    int* Credits;
    int* StudentID;
    char* StudentName;

    typedef PersonnelData* PersonPtr;
    PersonPtr ptrList[20];

    I want to write a function to search a student record by typing a student ID.

    int search_number(int student_number)

    int index=0;
    int found=0;

    while(index<20 && found==0)

    i found that I fail to get the value of ptrList[index]->StudentID
    Therefore, the program is failed to do the comparision.
    Can someone help me?

    I tried to print out the value of ptrList[index]->StudentID before the while loop, but something strange has happened.
    when I wrote:

    printf("%d", ptrList[0]->StudentID);
    > the printed value is incorrect

    but when I wrote
    printf("%c", ptrList[0]->StudentID);
    >the printed value is correct

    Thank you very much!

  2. #2
    Registered User
    Join Date
    Apr 2002
    First off, why do you make a struct consisting of pointers?? It will work fine if you change the struct to this:

    typedef struct personnelData 
    int Credits; 
    int StudentID; 
    char* StudentName; 
    But if you want to keep the struct the same (I assume you would since you've already written code to setup these records), you must remember that it does consist of pointers, so doing ptrList[index]->StudentID will return a pointer to an integer (cause that's what StudentID is) so you need to dereference it: *(ptrList[index]->StudentID)
    Last edited by Vlade; 04-05-2002 at 10:28 AM.

  3. #3
    Join Date
    Aug 2001
    Groningen (NL)
    >typedef PersonnelData* PersonPtr;
    >PersonPtr ptrList[20];

    This is the same as

    PersonellData *ptrList [20];

    You have an array of 20 pointers to elements of type PersonellData. Do you have declared memory for the structures? Something like:

    ptrList [0] = malloc (sizeof (PersonellData));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM