By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,411 Members | 1,045 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,411 IT Pros & Developers. It's quick & easy.

Pointer/typedef struct questions

P: n/a
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

Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Mac
On Sun, 20 Mar 2005 18:37:58 +0100, Immo Birnbaum wrote:
Hi,

I'm trying to solve a programming lab assignment for my college C
programming course,
OK, good.
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));
Better:
ipointer2 = malloc(sizeof (*ipointer2));
*ipointer2=(int)2;
You don't need to cast the constant. You could just write:
*ipointer2 = 2;

Also, while it doesn't matter in little examples, in real code, you
should check the return value of malloc before using it. (malloc returns
NULL on failure.)
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));
myobj1=malloc(sizeof(*myobj1));
((myObject)*myobj1).a=1; You don't have to do this. There is an operator in c which takes on the
left hand side a pointer to a struct, and on the right hand side, takes
the name of an element in that struct. The operator is "->". An example
will make it clearer. Instead of:

((myObject)*myobj1).a=1;

write:

myobj1->a = 1;
((myObject)*myobj1).b=2; 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?

Here are two ways to get the same result:
example 1:
typedef struct {int a,b;} *FIFO;
FIFO f1, f2; /* f1 and f2 are POINTERS */

example 2:
typedef struct {int a,b;} FIFO;
FIFO *f1, *f2; /* f1 and f2 are POINTERS */

While FIFO has a different meaning in example 1 and example 2, f1 and f2
can be used in the same ways in both examples.

Here is an example program which might get the idea across:

#include <stdio.h>
#include <stdlib.h>

typedef struct {int a,b;} *FIFO;

int main(void)
{
FIFO f1, f2;
f1 = malloc(sizeof(*f1));
if (f1 == NULL)
{ /* error handling code here */
return EXIT_FAILURE;
}
f1->a = 1;
f1->b = 2;
printf("f1->a = %d: f1->b = %d\n", f1->a, f1->b);
f2 = f1; /* now f2 and f1 point at the same structure */
f2->a = 3; /* This changes f1->a also */
f2->b = 4; /* This changes f1->b also */
printf("f1->a = %d: f1->b = %d\n", f1->a, f1->b);
free(f1);
/* After this free operation, the memory area pointed to by both f1 and
f2 is no longer valid and must not be accessed. New values could be
assigned to f1 and f2*/
return EXIT_SUCCESS;
}
I'm using gcc 3.3.3 on Debian Linux.
If that matters, then the post is off-topic in this group. But in this
case, that doesn't matter, so you are OK. ;-)

Regards,
Immo Birnbaum


HTH!

--Mac

Nov 14 '05 #2

P: n/a
On Sun, 20 Mar 2005 18:37:58 +0100, Immo Birnbaum
<Im***********@t-online.de> wrote:
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;
Someone has taught you some really bad casting habits. 2 is already
an int. Casting it simply reduces the readability of your program.
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;
myobj1 is a pointer to myObject. Therefore, *myobj1 must have type
myObject. Casting it to the same type is redundant.
((myObject)*myobj1).b=2;
While (*myobj1).b works, the preferred idiom is myobj1->b. It's the
only reason the -> operator exists.
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
No likely. myObject is a pointer. sizeof myObject evaluates to the
size of the pointer, not the size of the struct the pointer points to.
Unless your struct happens to be smaller than the pointer, you did not
request enough space. The fact that your code appeared to work
successfully is one of the more pernicious aspects of undefined
behavior.
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
The first declares FIFO as the type of the anonymous struct. The
second declares FIFO as a pointer to the anonymous struct. By not
providing a tag for the struct, your instructor has forced you to use
pointers and not attempt to define instances of the struct.
allocate the memory and access the struct's fields?


You allocate memory with either
FIFO ptr = malloc(sizeof (*FIFO));
or preferably
FIFO ptr = malloc(sizeof *ptr);

Obviously, the initialization can be moved from the definition to an
assignment statement later in the function.

You access the members of the struct using either
(*ptr).member
or preferably
ptr->member
<<Remove the del for email>>
Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.