Type definiton with structurs probem and fgets

N

noridotjabi

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
 
S

SuperKoko

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.
 
J

Joe Wright

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

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top