segmentation fault writing to a struct using strncpy

L

lurch132002

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);
}
 
I

Ian Collins

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!
 
P

Peter Nilsson

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);
 
L

lurch132002

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

Ian Collins

(e-mail address removed) 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top