473,387 Members | 1,318 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

problem: string in struct

3
#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(struct 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",&selection);

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



// Enter Account Name
printf("Enter Account Name : ");
scanf("%s",&TmpName);
Username = TmpName;
// Enter Age
printf("Enter Age : ");
scanf("%d",&UserAge);
// Enter Account Balance
printf("Enter Account Balance : ");
scanf("%d",&UserBalance);

insert(&startPtr,Username,UserAge,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",&selection);
}

}

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

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

if(newPtr != NULL) {


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


previousPtr = NULL;
currentPtr = *headOfList;



while(currentPtr != 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",headOfList->AccName);

}

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

while(headOfList != NULL) {
printf("%s\t%d\t%d\n",headOfList->AccName,headOfList->Age,headOfList->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

Sep 26 '09 #1
3 3333
Banfa
9,065 Expert Mod 8TB
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.
Sep 26 '09 #2
lye85
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(account));

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(currentPtr != 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",headOfList->AccName);

}

i try this but still the same names printed, pls correct me
Sep 26 '09 #3
newb16
687 512MB
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.
Sep 27 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

57
by: Xarky | last post by:
Hi, I am writing a linked list in the following way. struct list { struct list *next; char *mybuff; };
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
1
by: xoinki | last post by:
hi experts, I need a little help in debugging this code.. would u pleeze kindly help me? here this program sends a datagram every 10 seconds and on reception it cheks whether the source IP is...
22
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
1
by: Don.Leri | last post by:
Hi, I have a logger.dll (unmanaged c++ dll compiled in vs2005). I have a C# interop to use that dll in managed code implemented in Interfaces.dll (used by other C# dlls). I also have a...
21
by: softwindow | last post by:
#include "stdio.h" #include "malloc.h" struct student{ int age; char *nms; struct student *next; }; struct student *create(){ int ags=0,size=sizeof(struct student); char *nms=" ";
5
by: nick048 | last post by:
Hi to All, Foollowing Your suggestion, I have written this code: #include <stdio.h> #include <string.h> #include <malloc.h> #define BUFLEN 100
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
15
by: vivekian | last post by:
Hi, I have this following class class nodeInfo ; class childInfo { public: int relativeMeshId ;
5
by: Chad | last post by:
say my input file is $ more suck ______ < gnu? > ------ \ , , \ /( )` \ \ \___ / | /- _ `-/ '
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.