Re: ????????? ? ??????????? ???????? ??????? ? ?

Discussion in 'C Programming' started by Richard Heathfield, Jan 15, 2006.

  1. Since I don't speak whatever language does, this is just
    a generic tidying-up answer:

    said:

    > author* create_author(char *name,char *family, article *art)
    > {
    > author *tmpAut;
    > ALLOCATE(tmpAut,author);


    You don't supply a definition for this (presumably) macro, so I can't
    comment on it.

    > if (!tmpAut) return NULL;
    > if (!(tmpAut->name=(char*)malloc(sizeof(char)*strlen(name)+1))) return
    > NULL;


    You will need to #include <stdlib.h> for the malloc prototype and <string.h>
    for strlen. Please note that the cast is unnecessary, and that sizeof(char)
    is always, always 1 by definition.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jan 15, 2006
    #1
    1. Advertising

  2. Richard Heathfield

    Guest

    Hi, Richard :)

    Sorry for unknown symbols, just posted in the wrong group :)



    Well, the question is why if I call function create_author from within
    find_author , the pointer tmpAut is not updated and has the same
    "garbage value as after tmpAut declaration

    Code:
    author* find_author(LinkedList authorsList,char* name,char* family)
    {
            author *tmpAut;
            tmpAut=create_author(name,family,NULL);
                 ...
    
    Code:
    author* create_author(char *name,char *family, article *art)
    {
            author *tmpAut;
            ALLOCATE(tmpAut,author);
            if (!tmpAut) return NULL;
            if (!(tmpAut->name=(char*)malloc(sizeof(char)*strlen(name)+1)))
    return
    NULL;
            strcpy(tmpAut->name,name);
            if
    (!(tmpAut->family=(char*)malloc(sizeof(char)*strlen(family)+1)))
    return NULL;
            strcpy(tmpAut->family,family);
            tmpAut->articles=LinkedList_create(cpy_same,fre_none);
            if
    (LinkedList_insertAfterCurrent(tmpAut->articles,art)!=LIST_SUCCESS)
    return NULL;
            tmpAut->tGrade=0;
            tmpAut->cGrade=0;
            tmpAut->cWeight=0;
            tmpAut->tWeight=0; 
            return tmpAut; 
    
    
    } 
    
    
    
     
    , Jan 15, 2006
    #2
    1. Advertising

  3. said:

    > Well, the question is why if I call function create_author from within
    > find_author , the pointer tmpAut is not updated and has the same
    > "garbage value as after tmpAut declaration


    It is not obvious from your code why this should be failing. Assuming you
    have the correct headers in place (these are not merely C superstitions -
    they really are needed, especially stdlib.h and string.h in this case), I
    can't see why tmpAut would retain its value after the create_author call.
    It should take either NULL or the result of the malloc in create_author.

    I suggest you remove your "BB-tags" and post a complete, compilable program
    that demonstrates the problem as simply as possible. We might then have a
    fighting chance of helping you to discover what is going wrong.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jan 16, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page