Thread: Problem with array of pointers to structures and such

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    30

    Problem with array of pointers to structures and such

    I have a little problem.

    In the following problem I am trying to display an array of pointers in a separate function. The commented section displays the "name" variable correctly, however, the code in display() gives me a funky result with odd letters. Why is that??

    Code:
    static int count = 0;
    typedef struct Record{
            char *name;
            int age;
            float hbp;
            float lbp;
            float riskFactor;
    
    
    } Record;
    static Record *patients[30];
    
    
    void display()
    {
            int k;
            for (k=0;k<count;k++)
            {
                    printf("%d      %s",count, patients[k]->name);
            }
    }
    
    
    
    void enter(){
            patients[count] = (Record *)malloc(sizeof(Record));
            char name[20];
            int age;
            float hbp, lbp;
    
            printf("name: ");
            scanf("%s", name);
            printf("age: ");
            scanf("%d", &age);
            printf("high bp: ");
            scanf("%f", &hbp);
            printf("low bp: ");
            scanf("%f", &lbp);
            patients[count]->name = name;
            patients[count]->age = age;
            patients[count]->hbp = hbp;
            patients[count]->lbp = lbp;
            count++;
    
    //      int k;
    //      for (k=0; k<count;k++)
    //      {
    //              printf("%s", patients[k]->name);
    //      }
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    void enter(){
            patients[count] = (Record *)malloc(sizeof(Record));
            char name[20];
    Code:
    patients[count]->name = name;
    The variable "name" is local to enter(); the address of name is NOT valid to use outside of the scope of enter(). You are assigning address on the stack to "patients[count]->name" this is NOT somthing that is save to do.

    Tim S.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It's because you are not assigning your string variables correctly...

    Code:
            patients[count]->name = name;
    This isn't going to work because you are not creating a string only copying a pointer to a string and in the end every record will point to the same place...

    You need to do it like this...
    Code:
    typedef struct Record{
            char name[20];
            int age;
            float hbp;
            float lbp;
            float riskFactor;
    } Record;
    Record *patients[30];
    
    // other code...
    
            printf("name: ");
            scanf("%s", patient[count]->name);
            printf("age: ");
            scanf("%d", &patient[count]->age);
            printf("high bp: ");
            scanf("%f", &patient[count]->hbp);
            printf("low bp: ");
            scanf("%f", &patient[count]->lbp);
    Store the actual name buffer in the struct then load the name etc. directly into it.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    30
    Thanks, it worked! How would I go about clearing the variables then? Since I was more or less doing the same thing.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    30
    Code:
            Record *temp[30];
            int k;
            count = 0;
            for (k=0;k<count;k++)
            {
                    patients[k]->name = temp[k]->name;
                    patients[k]->age = temp[k]->age;
                    patients[k]->hbp = temp[k]->hbp;
                    patients[k]->lbp = temp[k]->lbp;
            }
    Is what I have, an assignment error?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bungkai View Post
    Thanks, it worked! How would I go about clearing the variables then? Since I was more or less doing the same thing.
    If you use my example there's no need to clear variables... because you are feeding the newly created struct directly.

    If you are worried about garbage as the back end of your string use calloc() instead.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bungkai View Post
    Code:
            Record *temp[30];
            int k;
            count = 0;
            for (k=0;k<count;k++)
            {
                    patients[k]->name = temp[k]->name;
                    patients[k]->age = temp[k]->age;
                    patients[k]->hbp = temp[k]->hbp;
                    patients[k]->lbp = temp[k]->lbp;
            }
    Is what I have, an assignment error?
    Maybe... but it's also unnecessary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 05-26-2011, 06:44 AM
  2. Problem with array of structures
    By Sonia in forum C Programming
    Replies: 10
    Last Post: 10-12-2010, 03:37 AM
  3. Help with 2d array of structures and pointers.
    By CopperHead4750 in forum C Programming
    Replies: 2
    Last Post: 12-09-2009, 03:02 PM
  4. array of pointers to an array of structures
    By phoneix_hallows in forum C Programming
    Replies: 3
    Last Post: 08-27-2009, 11:13 AM
  5. returning an array of pointers to structures
    By dharh in forum C Programming
    Replies: 9
    Last Post: 02-06-2003, 03:26 PM