Pointer Problem

This is a discussion on Pointer Problem within the C Programming forums, part of the General Programming Boards category; I declare this at the beginning of my program: typedef struct personnelData { int* Credits; int* StudentID; char* StudentName; }PersonnelData; ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    5

    Unhappy Pointer Problem

    I declare this at the beginning of my program:

    typedef struct personnelData
    {
    int* Credits;
    int* StudentID;
    char* StudentName;
    }PersonnelData;

    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)
    if(student_number==ptrList[index]->StudentID){
    found=1;
    ................
    ................
    }

    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
    Posts
    12
    First off, why do you make a struct consisting of pointers?? It will work fine if you change the struct to this:

    Code:
    typedef struct personnelData 
    { 
    int Credits; 
    int StudentID; 
    char* StudentName; 
    }PersonnelData;
    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
    Location
    Groningen (NL)
    Posts
    2,386
    >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, 09:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 08:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 11:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 01:14 PM

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