473,466 Members | 1,443 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Type definiton with structurs probem and fgets

I have a program (its gigantic so I'm not posting the source) that I'm
writing with two probelms. First of all can someone give me an example
of how fgets is suposed to be used because I've read its man pages but
I still get a segmentation fault everytime I try to use it. Second
here is the problem with structures.

#include <stdio.h>

typedef struct MY_STRUCT{
int one;
int two;
}MSTCT;

void pins(MSTCT *st, int num) {
*st.one = num;
return; /* this is just a habit of mine */
}

int main() {
MSTCT *mystructure;

pins(mystructure.one, 1);
printf("In my strucure is %d\n", *mystruct.one);
return 0;
}

When I try to compile this I get something along the lines of trying to
acces a member outside of a structure or union. So I tried:

#include <stdio.h>

struct MY_STRUCT{
int one;
int two;
};

void pins(struct MY_STRUCT *st, int num) {
*st.one = num;
return; /* this is just a habit of mine */
}

int main() {
struct M_STRUCT *mystructure;

pins(mystructure.one, 1);
printf("In my strucure is %d\n", *mystructure.one);
return 0;
}

Unfortunately this did not work either. I am now curious as to what I
am doing wrong. Thanks in advanced.
Nori

Jun 3 '06 #1
2 1795

no*********@gmail.com wrote:
I have a program (its gigantic so I'm not posting the source) that I'm
writing with two probelms. First of all can someone give me an example
of how fgets is suposed to be used because I've read its man pages but
I still get a segmentation fault everytime I try to use it. Second
here is the problem with structures.

#include <stdio.h>

typedef struct MY_STRUCT{
int one;
int two;
}MSTCT;

void pins(MSTCT *st, int num) {
*st.one = num;
return; /* this is just a habit of mine */
}

int main() {
MSTCT *mystructure;

pins(mystructure.one, 1);
printf("In my strucure is %d\n", *mystruct.one);
return 0;
}

Several problems:
1) Your compiler should complain that mystructure.one is invalid,
because the left side of the '.' operator expects a structure (and not
a pointer).
2) mystructure->one is an int... But the pins function expects a MSTCT*
3) You never allocate memory and use a dangling pointer.
4) *mystruct.one is interpreted as *(mystruct.one) while you want
(*mystruct).one

Try something like that:

#include <stdio.h>

typedef struct MY_STRUCT{
int one;
int two;
}MSTCT;

void pins(MSTCT *st, int num) {
*st.one = num;
return;
}

int main() {
MSTCT mystructure;

pins(&mystructure, 1);
printf("In my strucure is %d\n", mystruct->one);
return 0;
}

I suggest you learn the basics of pointers in a good book.

Jun 3 '06 #2
no*********@gmail.com wrote:
I have a program (its gigantic so I'm not posting the source) that I'm
writing with two probelms. First of all can someone give me an example
of how fgets is suposed to be used because I've read its man pages but
I still get a segmentation fault everytime I try to use it. Second
here is the problem with structures.

#include <stdio.h>

typedef struct MY_STRUCT{
int one;
int two;
}MSTCT;

void pins(MSTCT *st, int num) {
*st.one = num;
return; /* this is just a habit of mine */
}

int main() {
MSTCT *mystructure;

pins(mystructure.one, 1);
printf("In my strucure is %d\n", *mystruct.one);
return 0;
}

When I try to compile this I get something along the lines of trying to
acces a member outside of a structure or union. So I tried:

#include <stdio.h>

struct MY_STRUCT{
int one;
int two;
};

void pins(struct MY_STRUCT *st, int num) {
*st.one = num;
return; /* this is just a habit of mine */
}

int main() {
struct M_STRUCT *mystructure;

pins(mystructure.one, 1);
printf("In my strucure is %d\n", *mystructure.one);
return 0;
}

Unfortunately this did not work either. I am now curious as to what I
am doing wrong. Thanks in advanced.
Nori

Where is the instance of the structure? You have struct types and
pointers of the type but no structure. Try this (un tested)..

typedef struct MY_STRUCT{
int one;
int two;
}MSTCT;

void pins(MSTCT *st, int num) {
st->one = num;
return; /* this is just a habit of mine */
}

int main() {
MSTCT mystruct; /* a struct, not a pointer */

pins(&mystruct, 1);
printf("In my strucure is %d\n", mystruct.one);
return 0;
}

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 4 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tanel | last post by:
Hello, I need to read a result of the first script (that takes some time to run) from the second script so that the first script doesn't block the second. Here is a short example. do_smth_else()...
3
by: Zunbeltz Izaola | last post by:
I've a clas with following methods: class difracsocket: def __init__(self,sock=None): self.Handle = 1 def cliente(self,orden,respuesta):
4
by: oliver.lin | last post by:
In my simple test code, I tried to define my constructor outside of the class declaration headr file. The header file: file_handler.h ============================================================...
35
by: David Mathog | last post by:
Every so often one of my fgets() based programs encounters an input file containing embedded nulls. fgets is happy to read these but the embedded nulls subsequently cause problems elsewhere in...
11
by: santosh | last post by:
Hi, A book that I'm currently using notes that the fgets() function does not return until Return is pressed or an EOF or other error is encountered. It then at most (in the absence of...
7
by: Michael | last post by:
Hi, I could understand the difference between class and object. However, I could find out a good definiton of type. how to understand the relaitonship between type, class, and object? Thanks! ...
3
by: hta1984 | last post by:
In my homework i need to use enum type input,and these inputs will contain a port number and a description. Here is the input: HTTP : 80 “Hypertext Transfer Protocol” SSH : 22 “Secure Shell” ...
70
by: garyusenet | last post by:
I'm using an example piece of code: - namespace Wintellect.Interop.Sound{ using System; using System.Runtime.InteropServices; using System.ComponentModel; sealed class Sound{ public static...
1
by: eros | last post by:
page1.php (with header function) <?php header("Content-type: text/vnd.wap.wml"); print "<?xml version=\"1.0\" encoding=\"Shift-JIS\"?>"; $test = 123; ?> <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
1
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...
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
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,...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.