How to assign initializer list to structure pointer (dynamic)

This is a discussion on How to assign initializer list to structure pointer (dynamic) within the C Programming forums, part of the General Programming Boards category; Situation: A struture pointer consisting of 3 character arrays + 1 int is populated with values via strcpy() and a ...

  1. #1
    Registered User deoren's Avatar
    Join Date
    Mar 2003
    Posts
    63

    Question [SOLVED] How to assign initializer list toructure pointer (dynamic)

    Situation:

    A struture pointer consisting of 3 character arrays + 1 int is populated with values via strcpy() and a 'normal' assignment.

    The 'problem':

    I am confused why assigning a list to the dynamically declared structure pointer does not work.

    The following resources show 'how' a list of values are assigned to a structure pointer using the strcpy() function so I know 'how' to do so.

    But because I'm still learning C I don't understand 'why' this is the way it's done. I'm hoping someone can point it out to me. Google searching hasn't helped much because I'm really not sure what to look for.

    • The C Programming Language, Second Edition (TCPL) [pg 143, lines 5-7]
    • Thinking in C - Beta 3 (TIC) [Ch7 - Solution]
    • C By Example - Academic Edition (CBE) [pg 523]
    • C For Dummies - Volume 2 (CFD) [pg 904-905]


    NOTE:
    The attached files are from the Chapter 7 Exercise - Solution. The license for the TIC material (presentation, source code).

    This seminar is licensed under the Creative Commons "Attribution-NonCommercial-NoDerivs 2.5" license, which means that you may freely distribute the entire package as long as you give attribution to the authors, but you may not sell it or modify it. See the link above for more detail.


    The following sources show the strcpy() function being used to copy values into the members of a structure pointer instead of assigning a list.

    In employ2.c the following is found:

    Code:
    struct Employee* createEmployee(char* last, char* first, char* title, int salary)
    {
        struct Employee* p = malloc(sizeof(struct Employee));
        if (p != NULL)
        {
            strcpy(p->last, last);
            strcpy(p->first, first);
            strcpy(p->title, title);
            p->salary = salary;
        }
        return p;
    }

    That is a working function from the solution copy of employ2.c


    When the exercise was presented the files you were given were employ2.h and lab7.c. The user was given the task of fleshing out an employ2.c file of their own.

    So far I'm not doing too well:

    Code:
    /*  Allocate an Employee struct on the heap and initialize it with
        it's arguments, then return the pointer returned from malloc() */
    struct Employee* createEmployee(char *last, char *first, char *title, int salary) {
                                        
        struct Employee *emp = 
            (struct Employee *)malloc(sizeof(struct Employee));
        
        *emp = { last, first, title, salary };
        
        return emp;
                                        
    }
    Even though the solution did not typecast the void pointer from malloc I did so because of the reference material I was using. All of them predate C99 with TCPL SE being the oldest.


    I have a feeling this is blatantly obvious but I'm not 'getting' it.

    Thanks in advance for your help.
    Attached Files Attached Files
    Last edited by deoren; 07-28-2007 at 03:51 AM. Reason: Changing title to reflect that question is answered.
    It is better to fail with honor than win by deceit
    - unknown

    My erratic tinkerings:
    http://projects.whyaskwhy.org/

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    Even with C99, you still can't assign arrays.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User deoren's Avatar
    Join Date
    Mar 2003
    Posts
    63
    you still can't assign arrays.
    So that's what I tried to do then? The whole 'a pointer is an array, an array is a pointer (in most cases)' thing?

    Or was I trying to assign character arrays to a structure? I'm guessing that those references (books, websites) aren't emphasizing that when using malloc() because they assume the reader picked this up from earlier chapters?


    I guess let me ask it another way. This:

    Code:
        struct Employee *emp = 
            (struct Employee *)malloc(sizeof(struct Employee));
    is a structure pointer, right?

    and this:

    Code:
        *emp = { last, first, title, salary };
    is a 'list' being assigned to that structure pointer?

    A list == array?

    Or is it the fact that character arrays are part of that 'list' and there simply is 'the rule' that arrays are not copied like thus:

    Code:
    char arrayOne[11] = "This is me";
    char arrayTwo[11];
    
    arrayTwo = arrayOne;
    but instead have to be copied like:

    Code:
    char arrayOne[11] = "This is me";
    char arrayTwo[11];
    
    // arrayTwo = arrayOne;
    
    strcpy(arrayTwo,arrayOne);
    
    puts(arrayTwo);
    So 'that' is what prevents the 'list' from being assigned?


    Thanks in advance for helping to clear up this confusion.
    It is better to fail with honor than win by deceit
    - unknown

    My erratic tinkerings:
    http://projects.whyaskwhy.org/

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677

    Code:
        *emp = { last, first, title, salary };
    is a 'list' being assigned to that structure pointer?
    It's a list of arrays (or pointers to arrays, depending on how you look at it).

    Yes, C has a way of "making the distinction between an array and a pointer a bit blurred".

    But in this case it's quite clear - you are trying to set a set of arrays in (*emp) to a set of arrays in your list, and the language does not support setting arrays like that. You need to call strcpy() or some similar function. [You could of cours write a function that assigns all the elements, but you still need that to call strcpy or memcpy or such].

    --
    Mats

  5. #5
    Registered User deoren's Avatar
    Join Date
    Mar 2003
    Posts
    63
    It's a list of arrays
    Ah, I guess I just had to accept that this wasn't a complex issue but instead was really an issue of my trying to copy arrays to arrays.

    Thank you for clarifying. I 'get it' now.
    It is better to fail with honor than win by deceit
    - unknown

    My erratic tinkerings:
    http://projects.whyaskwhy.org/

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,335
    http://c-faq.com/aryptr/index.html
    If you're still at all confused as to the differences between arrays and pointers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 08:01 AM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21