problem: string in struct

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lye85
    New Member
    • Sep 2009
    • 3

    problem: string in struct

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>


    struct account {
    char *AccName;
    int Age;
    double AccBalance;
    struct account *Next;
    };

    typedef struct account BankAcc;

    void insert(struct account **headOfList, char *name, int age, double balance);
    //int delete(struct account **headOfList, char *name );
    void print(struct account *headOfList );
    //void printAbove50(st ruct account *headOfList);

    void main() {
    BankAcc *startPtr = NULL;

    int selection;
    printf("Enter selection and following below: \n");
    printf("1. Insert Account \n2. Delete Account \n3. Print All \n");
    printf("Enter your selection : ");
    scanf("%d",&sel ection);

    while(selection != 5) {
    switch(selectio n) {
    case 1:
    char *Username;
    char TmpName[20];
    int UserAge;
    double UserBalance;



    // Enter Account Name
    printf("Enter Account Name : ");
    scanf("%s",&Tmp Name);
    Username = TmpName;
    // Enter Age
    printf("Enter Age : ");
    scanf("%d",&Use rAge);
    // Enter Account Balance
    printf("Enter Account Balance : ");
    scanf("%d",&Use rBalance);

    insert(&startPt r,Username,User Age,UserBalance );
    break;

    case 3:
    print(startPtr) ;
    break;
    }

    printf("---------------------------------------------------------\n");
    printf("Enter selection and following below: \n");
    printf("1. Insert Account \n2. Delete Account \n3. Print All \n");
    printf("Enter your selection : ");
    scanf("%d",&sel ection);
    }

    }

    void insert(struct account **headOfList, char *name, int age, double balance) {
    struct account *newPtr;
    struct account *previousPtr;
    struct account *currentPtr;

    newPtr = (account*) malloc(sizeof(a ccount));

    if(newPtr != NULL) {


    newPtr->AccName = name;
    newPtr->Age = age;
    newPtr->AccBalance = balance;
    newPtr->Next = NULL;


    previousPtr = NULL;
    currentPtr = *headOfList;



    while(currentPt r != NULL && balance < currentPtr->AccBalance) {
    previousPtr = currentPtr;
    currentPtr = currentPtr->Next;
    }

    if(previousPtr == NULL) {
    newPtr->Next = *headOfList;
    *headOfList = newPtr;
    } else {
    previousPtr->Next = newPtr;
    newPtr->Next = currentPtr;
    }
    }

    //printf("%s\n",h eadOfList->AccName);

    }

    void print(struct account *headOfList ) {
    if(headOfList == NULL) {
    printf("List is empty. \n");
    } else {
    printf("Account Name\tAge\tAcco unt Balance\n");
    printf("_______ _______________ _______________ ____________\n" );

    while(headOfLis t != NULL) {
    printf("%s\t%d\ t%d\n",headOfLi st->AccName,headOf List->Age,headOfLi st->AccBalance);
    headOfList = headOfList->Next;
    }
    }
    }

    when execute the print function, name in all records are same, is the latest name i enter, some expert pls help me on this

  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    In your struct you declare AccName as a pointer. You then pass insert a pointer to a local array and that is copied into the account you are inserting.

    All accounts end up pointing at the same buffer.

    Worst that buffer is a temporary buffer, when it goes out of scope all the accounts will have invalid pointers.

    In your insert function you need to allocate memory for the AccName, alternitively AccName needs to be an array not a pointer.

    Comment

    • lye85
      New Member
      • Sep 2009
      • 3

      #3
      void insert(struct account **headOfList, char *name, int age, double balance) {
      struct account *newPtr;
      struct account *previousPtr;
      struct account *currentPtr;

      newPtr = (account*) malloc(sizeof(a ccount));

      if(newPtr != NULL) {

      newPtr->AccName = (char *)malloc(40 * sizeof(char));
      newPtr->AccName = name;
      newPtr->Age = age;
      newPtr->AccBalance = balance;
      newPtr->Next = NULL;


      previousPtr = NULL;
      currentPtr = *headOfList;



      while(currentPt r != NULL && balance < currentPtr->AccBalance) {
      previousPtr = currentPtr;
      currentPtr = currentPtr->Next;
      }

      if(previousPtr == NULL) {
      newPtr->Next = *headOfList;
      *headOfList = newPtr;
      } else {
      previousPtr->Next = newPtr;
      newPtr->Next = currentPtr;
      }
      }

      //printf("%s\n",h eadOfList->AccName);

      }

      i try this but still the same names printed, pls correct me

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        The same - you assign it to point to some allocated buffer, then reassign it to point to the same buffer that is TmpName. Use strcpy.

        Comment

        Working...