Using strcpy() and arrays of structs

This is a discussion on Using strcpy() and arrays of structs within the C Programming forums, part of the General Programming Boards category; Hello everyone, I'm having an issue using strcpy() and having one of the values being an array of structs. Here's ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    7

    Using strcpy() and arrays of structs

    Hello everyone,

    I'm having an issue using strcpy() and having one of the values being an array of structs. Here's the important parts:
    Code:
    #include <string.h>
    .
    .
    .
    struct identifiers {
      char * category;
      char * name;
      char * itype;
      char * scope;
      char * lifetime;
    }symbols[60];
    .
    .
    .
    strcpy((&symbols[i])->name, yytext);
    I have all of my variables declared properly, so there's no need to worry about that. This version compiles fine, but I get a segmentation fault every time I get to this part of the program. Essentially, my question is if I'm using the strcpy() function correctly for my situation. If you need the rest of the code for troubleshooting, let me know. I didn't put all of it in because it's lengthy and messy.

    Thanks ahead of time.
    vital101

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    None of your variables actually point to any allocated memory by default. So unless you're allocating some right before that strcpy call, you aren't pointing anywhere useful. Plus, since you're dealing with pointers, you don't need to add that & in there.


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

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    7
    Thanks, that solved my issue.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,272
    Quzah already explained the problem, but...

    Quote Originally Posted by vital101 View Post
    Code:
    (&symbols[i])->name
    Why are you referencing and then immediately dereferencing? Totally pointless. Just do this:

    Code:
    symbols[i].name

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D arrays, strcpy and strcmp Problems
    By Windu102 in forum C Programming
    Replies: 3
    Last Post: 08-23-2008, 02:00 AM
  2. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  3. strcpy error with arrays
    By trang in forum C Programming
    Replies: 4
    Last Post: 01-10-2004, 10:13 PM
  4. malloc with arrays of strings
    By Lib in forum C Programming
    Replies: 2
    Last Post: 08-03-2003, 11:46 PM
  5. help with strcpy and arrays with header file
    By Agnesa in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2002, 05:06 PM

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