segmentation fault writing to a struct using strncpy

Discussion in 'C Programming' started by lurch132002@yahoo.com, May 1, 2007.

  1. Guest

    i am trying to create an array of structs to hold some information but
    whenever i get
    to the second element and try to strncpy it i get a segmenation
    fault. ive searched around for
    similar problems but i cant seem to figure out what im doing wrong.
    any help would be appreciated.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    struct filesys {
    char *fname;
    int cylinder;
    int sector;
    int length;
    };

    int main( int argc, char *argv[] ) {

    struct filesys file_array[100];

    int i;
    for( i = 0; i < 100; i++ )
    strncpy(file_array.fname, "hello", 6);

    for(i = 0; i < 100; i++ )
    printf("%s", file_array.fname);
    }
    , May 1, 2007
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > i am trying to create an array of structs to hold some information but
    > whenever i get
    > to the second element and try to strncpy it i get a segmenation
    > fault. ive searched around for
    > similar problems but i cant seem to figure out what im doing wrong.
    > any help would be appreciated.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > struct filesys {
    > char *fname;
    > int cylinder;
    > int sector;
    > int length;
    > };
    >
    > int main( int argc, char *argv[] ) {
    >
    > struct filesys file_array[100];
    >
    > int i;
    > for( i = 0; i < 100; i++ )
    > strncpy(file_array.fname, "hello", 6);
    >

    Try allocating some memory for fname before you attempt to write to it!


    --
    Ian Collins.
    Ian Collins, May 1, 2007
    #2
    1. Advertising

  3. wrote:
    > i am trying to create an array of structs to hold some
    > information but whenever i get to the second element and
    > try to strncpy it i get a segmenation fault. ive searched
    > around for similar problems but i cant seem to figure out
    > what im doing wrong.
    > any help would be appreciated.
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > struct filesys {
    > char *fname;


    This declares a pointer only. It doesn't delcare what the
    pointer points to. Writing through an uninitialised or null
    pointer invokes undefined behaviour.

    Try...

    char fname[6];

    > int cylinder;
    > int sector;
    > int length;
    >
    > };
    >
    > int main( int argc, char *argv[] ) {
    >
    > struct filesys file_array[100];
    >
    > int i;
    > for( i = 0; i < 100; i++ )
    > strncpy(file_array.fname, "hello", 6);


    Why are you using strncpy?

    >
    > for(i = 0; i < 100; i++ )
    > printf("%s", file_array.fname);


    The strncpy function was designed for fixed width fields.
    Those fields need not be null terminated strings. If you
    want to print such a field, then you should specify the
    width so that printf doesn't overrun the field.

    printf("%.6s", file_array.fname);

    --
    Peter
    Peter Nilsson, May 1, 2007
    #3
  4. Guest

    Thanks for the help guys, got it working once i changed fname the
    pointer to fname the char array.
    , May 1, 2007
    #4
  5. Ian Collins Guest

    wrote:

    Please quote enough context for your reply to make sense.

    > Thanks for the help guys, got it working once i changed fname the
    > pointer to fname the char array.
    >

    Make sure the array is large enough.

    --
    Ian Collins.
    Ian Collins, May 1, 2007
    #5
    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.
Similar Threads
  1. anars

    strncpy to a char member of struct

    anars, Apr 21, 2006, in forum: C Programming
    Replies:
    12
    Views:
    675
    Default User
    Apr 25, 2006
  2. SP
    Replies:
    8
    Views:
    741
    Dave Thompson
    Aug 14, 2006
  3. Anna
    Replies:
    3
    Views:
    274
    rahul
    Jun 23, 2008
  4. Anna
    Replies:
    2
    Views:
    318
    Keith Thompson
    Jun 23, 2008
  5. crystal twix

    Segmentation fault in struct

    crystal twix, Nov 10, 2009, in forum: C++
    Replies:
    2
    Views:
    456
    Ian Collins
    Nov 10, 2009
Loading...

Share This Page