Say I have a struct:

typedef struct entry{
	char name[40];			/* Contains name */
	char phone_number[9];	/* Contains phone number */
	struct entry *next ;	/* next is used to navigate through structures. */
} contact;

I have a pointer:

contact *email, *address;

In my main(), I dynamically assign memory by use of malloc():

/* malloc() to create a dynamic-sized array for email/home addresses. */
	email = (contact*)malloc(sizeof(contact));
	address = (contact*)malloc(sizeof(contact));

How can I modify my struct so that I can make use of the email/address pointers to store a corresponding value (email/address) into it dynamically?

I feel I'm missing out some declarations in my struct in order for my pointers to store the corresponding value. What have I missed out?

Hey!

Say I have a struct:
How can I modify my struct so that I can make use of the email/address pointers to store a corresponding value (email/address) into it dynamically?

So you can simply use define your structure like this:

typedef struct xyz{
   char * name;
   char * email;
   contact * next;
} contact;

Then you can create pointers like:

contact * a;
a=(contact *) malloc(sizeof(contact));
char * temp;
temp=(char *)malloc(sizeof(char)*10);
//enter data into temp
then a->name=temp;

and then you are done. I hope I have understood your question properly.

Reagrds,
Digvijay

How can I modify my struct so that I can make use of the email/address pointers to store a corresponding value (email/address) into it dynamically?

Why do you need to modify struct when you can do something like this at main:

printf("Enter Name:");
	scanf("%s",&email->name[40]);
	printf("Enter student ID:");
	scanf("%s",&email->phone_number[9]);

I believe your now stepping into linked lists
and I feel like your gonna ask next on how to traverse the linked list ;)

Haha, currently my problem statement is as such:

You may assume the following:
• Names will be 40 characters or less
• Phone numbers will be exactly 8 digits long
• Email addresses can be any length
• Mailing addresses can be any length

So basically, I need to dynamically assign the email address/mailing address part uh.. Otherwise, the simple method would be just to define another array email/address in the same manner as how the name[40] and phone_number[9] was defined..

It's stated in my problem that:

You should modify it to also store email and mailing addresses without any length restrictions.

typedef struct entry{
char name[40];
char phone_number[9];
struct entry * next ;
} contact;

So, having implemented malloc() already, I was wondering how I can use the pointer to link back to the struct (essentially to create a char email[40]/char address[40] without the [40]... This is the part I'm unsure about. :X

@ begame

typedef struct xyz{
   char * name;
   char * email;
   contact * next; // not possible
} contact;

you can't use the typedef contact yet since it's not yet finished with the struct, So when it reaches contact * next; it would not know what contact is
So when you try to use a variable of the type pointer to contact it has no idea how it should handle this.

Edited 4 Years Ago by zeroliken: n/a

You may assume the following:
• Names will be 40 characters or less
• Phone numbers will be exactly 8 digits long
• Email addresses can be any length
• Mailing addresses can be any length

If I understand it correctly you already got all of these correctly :)

You should modify it to also store email and mailing addresses without any length restrictions.

that's why you used dynamically allocated email and mailing addresses, so thst each have no length restrictions
IMHO, there's no need to create a char email[40]/char address[40] without the [40]

Edited 4 Years Ago by zeroliken: n/a

@ begame

typedef struct xyz{
   char * name;
   char * email;
   contact * next; // not possible
} contact;

you can't use the typedef contact yet since it's not yet finished with the struct, So when it reaches contact * next; it would not know what contact is
So when you try to use a variable of the type pointer to contact it has no idea how it should handle this.

Right! Woops.

If I understand it correctly you already got all of these correctly :)

that's why you used dynamically allocated email and mailing addresses, so thst each have no length restrictions
IMHO, there's no need to create a char email[40]/char address[40] without the [40]

Oh, but if I don't have these 2 things in my struct, then how would I be able to call them when traversing through the linked list?..

Like, ok, I have a few pointers now:

contact *firstc, *currentc, *newc

So, somewhere below in my code, say I wanna add a new contact to my address book. I would do this to add the contact name:

printf("Enter the contact's name: ");
	gets(currentc->name);

How would I then add the email/home address then?

I would do this normally:

printf("Enter the contact's email: ");
	gets(currentc->email);

Thing is, there is no email array now in my struct, so actually my question would be how would I call my email array to store values inside.

I tried modifying it to:

printf("Enter the contact's email: ");
	gets(*email->email);

while defining a char *email; in my struct, but it doesn't work that way. I think I'm confusing some aspects up.. :X

though if you would insist it would look something like this:

typedef struct linkedlist{
	char *charStudName;
	struct linkedlist * next;
}list;
int main()
{
      list *newnode;	
	newnode = (list *)malloc(sizeof(list));	//create a new node
	newnode->charStudName  = (char *)malloc(sizeof(char)); //allocate size of charStudName
	printf("Enter Student Name:");
	scanf("%s",newnode->charStudName);
	printf("%s\n",newnode->charStudName);[
}

Edited 4 Years Ago by zeroliken: tag error

Oh, but if I don't have these 2 things in my struct, then how would I be able to call them when traversing through the linked list?..

Like, ok, I have a few pointers now:

contact *firstc, *currentc, *newc

So, somewhere below in my code, say I wanna add a new contact to my address book. I would do this to add the contact name:

printf("Enter the contact's name: ");
	gets(currentc->name);

How would I then add the email/home address then?

I would do this normally:

printf("Enter the contact's email: ");
	gets(currentc->email);

Thing is, there is no email array now in my struct, so actually my question would be how would I call my email array to store values inside.

I tried modifying it to:

printf("Enter the contact's email: ");
	gets(*email->email);

while defining a char *email; in my struct, but it doesn't work that way. I think I'm confusing some aspects up.. :X

email is not a variable it is an instance of a structure so you can't assign a value to it.
For the variable check my first post on this thread

Edited 4 Years Ago by zeroliken: correcting

This article has been dead for over six months. Start a new discussion instead.