473,408 Members | 2,450 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

segmentation fault writing to a struct using strncpy

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
4 5747
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
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
Thanks for the help guys, got it working once i changed fname the
pointer to fname the char array.

May 1 '07 #4
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joel | last post by:
I have this bug that quite puzzled me. Basically I am having a segmentation fault on deleting an object, which belongs to a class which is the result of multiple inheritance from two other classes....
3
by: mblome | last post by:
Hi everybody! I came across a very strange problem with stl vectors during developement of a mesh creation program. As the program is quite large I can only post small parts of it. Basically I...
2
by: Atulvid | last post by:
Hi, I created a test file for my application by writing structures to a binary file. To make sure that I will get correct data for the pointers inside the structure, i wrote actual data they...
16
by: laberth | last post by:
I've got a segmentation fault on a calloc and I don'tunderstand why? Here is what I use : typedef struct noeud { int val; struct noeud *fgauche; struct noeud *fdroit; } *arbre; //for those...
9
by: Narendran Kumaraguru Nathan | last post by:
Hi all, I am fairly experianced in C. I am writing a program in which I'm getting a segmentation fault. The problem is that it is getting the segmentation fault when executing calloc. I tried...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
1
by: Gaurinn | last post by:
Hi all, I´m stuck here. I´m trying to get a single word from an array. After the text "Sometext10" comes a word of 6 letters, which I want to get. I don´t know what is wrong with this code. I...
6
drumgirl67
by: drumgirl67 | last post by:
I am getting a segmentation fault in a function in a C++ program. "fields" is a two dimensional array that was passed to the function. Each "row" in fields is a 32 character array, and the total...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.