Thread: Help!

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    Help!

    Im working on aprogram for entries the struct is:

    Code:
    struct entry
    {
        char name[STR_SIZE];
        char email[STR_SIZE];
        char phone[STR_SIZE];
    };
    
    i need to do what is in comments how am i doing?
    
    ENTRY *create_Entry(char *name, char *email, char *phone)
    {
        /* TO DO: Allocate memory, assign values to fields, return address. */
    
    
        ENTRY *ptr;
    
    
        ptr = (ENTRY *)malloc(sizeof(ENTRY));
    
    
        ptr -> name = NULL;
        ptr -> email = NULL;    
        ptr -> phone = NULL;
    
        return ptr;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need to allocate any memory other than for the ptr because name, email, and phone are all arrays, not pointers. You also don't need to typecast malloc.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    How do i do this setter?

    Im stuck at this anyone help?


    Code:
    /* This function sets the name of the entry with the given value. */
    void setName_Entry(ENTRY *self, char *name)
    {
        /* TO DO: Assign name to corresponding field. */
    	char *ptr;
    
    
    	for (ptr = name; *ptr != '\0'; ptr++){
    	
    	*self -> name = *ptr;
    	
    	}
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't have pointers, so stop trying to assign as if they were. You need to read: Arrays and Pointers

    You have arrays. You either need to loop through them assigning one value at a time, or you need to use a function that does that for you (ie: strcpy).


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Thanks very much me and my friend finally did the program of entries now we are stuck on a phonebook app ^^

Popular pages Recent additions subscribe to a feed

Tags for this Thread