Thread: printing a string to a struct member???

  1. #1
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45

    printing a string to a struct member???

    Well, basically what I need to do is take a command line argument and store its value in a char * located in a struct.

    Code:
    int main(int argc, char *argv[])
    {
      // some other stuff
      struct dir_path *dir = malloc(sizeof(struct dir_path));
    
      if (argc != 2) {
        printf("USAGE: ./backup <dir name>\n");
        exit(EXIT_FAILURE);
      }
      //some stuff
     sprintf(dir->orig, "%s", argv[1]);
    
      //other stuff
    }
    When i compile I get no errors concerning the sprintf line, but when I run the program with a simple string as an argument it gets a Segmentation Fault at the above highlighted sprintf line. The member orig is a char pointer inside the struct. Any help on getting that argument printed into the memory pointed to by dir would be greatly appreciated.

    EDIT: Do I need to allocate memory for the struct members as well as the whole struct?


    Thanks
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    28
    When you store a string's value somewhere else, you either mean copying the whole sequence, or just copying the address of the start of the sequence. The latter seems simplest here.

    In your case, why not just do:
    Code:
    int main(int argc, char *argv[])
    {
      // some other stuff
      struct dir_path *dir = malloc(sizeof(struct dir_path));
    
      if (argc != 2) {
        printf("USAGE: ./backup <dir name>\n");
        exit(EXIT_FAILURE);
      }
      //some stuff
     dir->orig = argv[1];
    
      //other stuff
    }
    Also, you don't need to explicitly allocate memory for members of a struct, it's done automatically. (That's why you need sizeof(struct foo))

  3. #3
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45
    That did seem like the obvious way to do it, but I wanted to have the string copied to the memory I allocated, instead of pointing it to argv[1]. It just seemed like it would have worked better that way in the context of everything else I was doing.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    28
    What memory you allocated? The struct only contains space for a pointer to a string, not the memory for the string itself. (It couldn't contain the memory for the string, not knowing how big it will be)
    Last edited by pdc; 07-28-2005 at 08:46 AM. Reason: formatting typo

  5. #5
    Prying open my third eye.
    Join Date
    Jun 2005
    Posts
    45
    I got everything straight now. I am one of about six interns at this company, and we all sit in cubicles. The whole time I was trying to fix the problem this other intern kept talking, and talking, and talking....I kept trying to say things like "hmm, i need to get this code working" but he wouldnt stop! So when I came back from lunch my original problem was clear as day, I was free()ing some memory in another section so that it returned a pointer to where the free memory was, stupid but it wasn't apparent with the constant blabbering of this intern.

    But I did take what you said into account, and thanks for the responses.
    "So you're one of those condescending UNIX computer users?"

    "Here's a nickel, kid. Get yourself a better computer."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM