473,763 Members | 2,375 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ar ray[i].fname, "hello", 6);

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

Apr 30 '07 #1
4 5769
lu*********@yah oo.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_ar ray[i].fname, "hello", 6);
Try allocating some memory for fname before you attempt to write to it!
--
Ian Collins.
Apr 30 '07 #2
lurch132...@yah oo.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_ar ray[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*********@yah oo.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
2171
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. None of the classes actually allocate memory on the heap. I simplified my code into one piece to show below. I will really appreciate if someone can tell me where the problem is. I have spent quite some time on it and felt a bit lost at this...
3
5210
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 have a std::vector<SPoly> xtop; where SPoly simply is: struct SPoly { int start, end;
2
5547
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 pointed to and data size/len to the file, after the structure. But when I tried to read the file in same sequence, it gives me segmentation fault. When I debugged the program with GDB, it executed well until fclose() and read all the member of...
16
8995
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 who don't speak french arbre means tree.
9
2024
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 debugging it, with no use. I tried to figure out the common reasons (1) Using a memory location which is not allocated. (2) Using memory which is freed. (3) Function use before declaration. I didn't use any other debuggers or tools other than ddd. My...
5
2997
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 understood that a segmentation fault can occur whenever I declare a pointer and I leave it un-initialized. So I thought the problem here is with the (const char *)s in the stuct flightData (please note that I get the same fault declaring as char * the...
27
3364
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! #include <stdlib.h> #include <stdio.h> typedef struct _node_t {
1
965
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 always get segmentation fault, when it comes to run the strncpy command: strncpy (blu,tmp1,len); Can someone help me with this problem? void get_acid(char blu)
6
2057
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 number of "rows" is numfields, also passed to the function. I have double checked the number and it's correct. The segmentation fault is occurring at the strncpy. I have also tried strcpy and the commented out code. for(int x = 0; x < numfields;...
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9998
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8822
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7366
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.