By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 911 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

segmentation fault writing to a struct using strncpy

P: n/a
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[i].fname, "hello", 6);

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

Apr 30 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
lu*********@yahoo.com 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[i].fname, "hello", 6);
Try allocating some memory for fname before you attempt to write to it!
--
Ian Collins.
Apr 30 '07 #2

P: n/a
lurch132...@yahoo.com 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[i].fname, "hello", 6);
Why are you using strncpy?
>
for(i = 0; i < 100; i++ )
printf("%s", file_array[i].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[i].fname);

--
Peter

May 1 '07 #3

P: n/a
Thanks for the help guys, got it working once i changed fname the
pointer to fname the char array.

May 1 '07 #4

P: n/a
lu*********@yahoo.com 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.
May 1 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.