Thread: Question to do with strdup()

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    55

    Wink Question to do with strdup()

    Hey all

    I'm halfway through a really tiny program, just to help me manage a few applicants I get. Wanted to code something in C purely to give me more experience with the language, but anyway.

    Here's the code:

    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>

    #define MAXBUF 1024

    typedef struct appl_def {
        
    int id;
        
    char *nick;
        
    char *email;
        
    int stage;
        
    int task;
        
    char *comments;
        
    struct appl_def *next;
    applicant;

    void getApplicants(void);
    void listApplicants(void);
    void addNode(int idchar *s);

    /*
    ** tptr - Is a pointer to the top node of the linked list.
    ** cptr - Is a pointer to the current node of the linked list.
    */

    applicant *tptr NULL, *cptr NULL;

    int main(void)
    {
        
    getApplicants();

        if (
    tptr != NULL)
            
    listApplicants();
        else
            
    puts("There are currently no applicants.");

        return 
    0;
    }

    void getApplicants(void)
    {
        
    int id;
        
    FILE *fp;
        
    char buf[MAXBUF];

        if ((
    fp fopen("applicants.txt""r")) == NULL)
        {
            
    fprintf(stderr"%s""applicants.txt not found.\n");
            exit(
    EXIT_FAILURE);
        }

        for (
    id 1; ((fgets(bufMAXBUFfp)) != NULL); id++)
            
    addNode(idbuf);

        
    fclose(fp);
    }

    void listApplicants(void)
    {
        for (
    cptr tptrcptr != NULLcptr cptr->next)
            
    printf("%d.) %s"cptr->idcptr->nick);
    }

    /*
    ** nptr - Is a new node for the list.
    */

    void addNode(int idchar *s)
    {
        
    applicant *nptr;
        
    cptr tptr;

        if ((
    nptr = (applicant *) malloc(sizeof(applicant))) != NULL)
        {
            
    nptr->id id;
            
    nptr->nick = (void *)strdup(s);
            
    nptr->next NULL;

            if (
    tptr == NULL)
                
    tptr nptr;
            else
            {
                while (
    cptr->next != NULL)
                    
    cptr cptr->next;
                
    cptr->next nptr;
            }
        }
        else
        {
            
    fprintf(stderr"%s""Not enough memory to add applicant to linked list.\n");
            exit(
    EXIT_FAILURE);
        }

    Now, my question is, I got a compiler warning complaining that 'assignment makes pointer from integer without cast', for this line:

    nptr->nick = strdup(s);

    I fixed it with:

    nptr->nick = (void *)strdup(s);

    However, whilst I've fixed the warning, I don't quite understand why lol. Could someone explain to me why the warning is now gone? I know it's something to do with the pointer that's returned from strdup(), and I think (void *) is casting it as a void pointer. I think that in turn means it can be used with any pointer type, which is why it works, but I'm not certain. Just need someone to say, "Yep, that's it!", or, "Nope, you muppet!".

    Thanks in advance,

    John.

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Because the function prototype of strdup is defined in string.h. You don't include this file so the compiler assumes the strdup function returns a void *.
    Just include string.h and remove the cast...

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Because the function prototype of strdup is defined in string.h.
    If strdup() is supported by the compiler, this is usually true :-)
    *Cela*

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    oh yeah! Thanks mate. Completely forgot about that lol.

    Cheers

    John.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    strdup() malloc's memory on your behalf, and kindly leaves it to you to free() it.... so don't forget to do so
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    Thanks Hammer,

    I shall when I finish it, but thanks for pointing it out as I'd forgotten to define a function to do that for the linked list.

    John.

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Cela
    >>Because the function prototype of strdup is defined in string.h.
    If strdup() is supported by the compiler, this is usually true :-)
    Why must the compiler support this function?
    If it's part of the standard c library (this is usually true because it's an ANSI C function) it should work.

  8. #8
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>If it's part of the standard c library (this is usually true because it's an ANSI C function) it should work.
    This is not true, strdup is a nonstandard function and doesn't have to be supported by compilers. But since it's so easy to write, why not do so? :-)
    Code:
    char *strdup(char *src)
    {
      char *ret = malloc(strlen(src)+1);
    
      if (ret != 0)
      {
        strcpy(ret, src);
      }
    
      return ret;
    }
    *Cela*

  9. #9
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Cela,

    I did a little searching on strdup (I always thought it was part of the ANSI C standard) and you're right.
    It's always nice to learn something from the pro's.

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    Well I'll be... I didn't know that either. Thanks Cela.

    John.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM