Thread: pointers to strings

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    15

    pointers to strings

    Hi,

    I have a function like this:
    Code:
    void addidfword(char *word, char *dname, IDFWORDS *C) {
       IDFWORDS *new;
       IDFWORDS *ptr;
    
       new=(IDFWORDS *)malloc(sizeof(IDFWORDS));
       new->word=word;
    The char *word passed to the fn is a pointer to malloc spaced for the string.
    Each IDFWORDS struct has a char *word in it.
    It works if instead of:
    new->word=word;
    I do:
    new->word=(char *)malloc(sizeof(char)*strlen(word)+1);
    strcpy(new->word,word);

    But I am wondering why I have to malloc more space for something that is malloced already. Why can't I just set new->word equal to the pointer to the malloced space?

    THanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    But I am wondering why I have to malloc more space for something that is malloced already.
    First off, you're not mallocing anything that exists already. You're mallocing a new instance of IDFWORDS. See:
    Code:
    IDFWORDS *new; /* This is a pointer. It has no allocated instance. */
    IDFWORDS *ptr;
    
    new=(IDFWORDS *)malloc(sizeof(IDFWORDS)); /* Now you are creating an instance of IDFWORDS. */
    All this does is create the IDFWORDS structure instance. The pointer in that structure, contains nothing. The real reason you malloc space for new->word is because it doesn't point to anything.

    Yes, you could just make the pointer there point to the argument passed. However, what happens if something happens to that argument? What if it goes out of scope? What if it is destroyed by another function?

    That's the reason you take care of your own allocation and copying.

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

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    15
    I am not sure I follow...maybe my question wasn't clear or maybe I am just not getting it.

    Before this function is called, i malloc space for a string called word. i then pass the pointer to this malloced space to the function. Shouldn't doing:
    new->word=word
    be okay since word is just a pointer to the malloced space?
    I thought passing a pointer to a function meant you could access it.

    Basically,
    i want new->word to point to a malloced location i created outside the function call.
    i give the function a pointer to that space and want new->word to point to it.


    Thanks again...

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's some compilable code based on yours. I have no idea if it reflects what you're actually doing, but hopefully it will help you understand better.

    The first call to malloc is in main(). This creates the room for the word "123456789".
    The call to malloc() within the addidfword() function is there to create room for the structure of type IDFWORDS. I'm not sure what you're doing with these things, so it's difficult to judge if you're actually going the best way about things.

    Anyway, see how you get on.
    Code:
     #include <stdio.h>
     #include <stdlib.h>
     #include <string.h>
     
     typedef struct
     {
       char  *word;
     } IDFWORDS;
     
     void addidfword(char *word, char *dname, IDFWORDS *C)
     {
       IDFWORDS *new;
     
       IDFWORDS  *ptr;
     
       /* 
        * This was: new=(IDFWORDS *)malloc(sizeof(IDFWORDS)); 
        */ 
     
       new = malloc(sizeof(*new)); 
     
       if (new)
       {
         new->word = word;
       }
     
       /* 
        * Continue to use new
        * ...
        */
       free(new);
     }
     
     int main(void)
     {
       char      *p;
       IDFWORDS  c;
     
       p = malloc(10);
       if (p)
       {
         strcpy(p, "123456789");
         addidfword(p, "dnameblurb", &c);
       }
     
       free(p);
     
       return(0);
     }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Pointers With Strings
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 02-14-2006, 06:28 AM
  2. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. More on pointers and strings
    By mart_man00 in forum C Programming
    Replies: 4
    Last Post: 02-10-2003, 06:10 PM
  5. help with pointers and strings
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2002, 06:54 AM