I
Immo Birnbaum
Hi,
I'm trying to solve a programming lab assignment for my college C
programming course, but as they taught us two semesters of Java before
teaching us any C, I'm having problems with all the aspects of
pointers. I'd appreciate if anybody could help me with the following
problem:
I tried to learn how to use malloc, free, and the * and & operators.
I started with a few simple lines of code like:
int i;
int *ipointer;
i=1; ipointer=&i; (followed by a printf showing that i and *i have the
same value)
I also wrote something like this:
int *ipointer2;
ipointer2=malloc(sizeof(int));
*ipointer2=(int)2;
free (ipointer2);
Then I got to the point where things are a little more relevant for my
current assignment, as it contains typedef struct's:
typedef struct {int a,b;} myObject;
myObject *myobj1;
myObject myobj2;
myobj1=malloc(sizeof(myObject));
((myObject)*myobj1).a=1;
((myObject)*myobj1).b=2;
free(myobj1);
myobj2.a=3;
myobj2.b=4;
Up to this point, I can create a pointer to my self-defined type and
access its fields, both reading and writing.
But a problem I couldn't solve is that our assignment paper contains a
header file which contains the typdef struct definition which is to be
implemented. Other than in the code above, the header file uses the
following statement: typedef struct {...} *FIFO instead of e.g.
typedef struct {...} FIFO. As soon as I change the typedef struct
statement in the code shown above from
typedef struct {int a,b;} myObject;
to
typedef struct {int a,b;} *myObject;
I still get the correct result for the sizeof function and can perform
malloc/free operations, but I can't access the a and b fields any
more, neither in myobj1, nor in myobj2. What is the difference between
FIFO and *FIFO in the typedef struct statement and how should I
allocate the memory and access the struct's fields?
I'm using gcc 3.3.3 on Debian Linux.
Regards,
Immo Birnbaum
I'm trying to solve a programming lab assignment for my college C
programming course, but as they taught us two semesters of Java before
teaching us any C, I'm having problems with all the aspects of
pointers. I'd appreciate if anybody could help me with the following
problem:
I tried to learn how to use malloc, free, and the * and & operators.
I started with a few simple lines of code like:
int i;
int *ipointer;
i=1; ipointer=&i; (followed by a printf showing that i and *i have the
same value)
I also wrote something like this:
int *ipointer2;
ipointer2=malloc(sizeof(int));
*ipointer2=(int)2;
free (ipointer2);
Then I got to the point where things are a little more relevant for my
current assignment, as it contains typedef struct's:
typedef struct {int a,b;} myObject;
myObject *myobj1;
myObject myobj2;
myobj1=malloc(sizeof(myObject));
((myObject)*myobj1).a=1;
((myObject)*myobj1).b=2;
free(myobj1);
myobj2.a=3;
myobj2.b=4;
Up to this point, I can create a pointer to my self-defined type and
access its fields, both reading and writing.
But a problem I couldn't solve is that our assignment paper contains a
header file which contains the typdef struct definition which is to be
implemented. Other than in the code above, the header file uses the
following statement: typedef struct {...} *FIFO instead of e.g.
typedef struct {...} FIFO. As soon as I change the typedef struct
statement in the code shown above from
typedef struct {int a,b;} myObject;
to
typedef struct {int a,b;} *myObject;
I still get the correct result for the sizeof function and can perform
malloc/free operations, but I can't access the a and b fields any
more, neither in myobj1, nor in myobj2. What is the difference between
FIFO and *FIFO in the typedef struct statement and how should I
allocate the memory and access the struct's fields?
I'm using gcc 3.3.3 on Debian Linux.
Regards,
Immo Birnbaum