473,383 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Pointer/typedef struct questions

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
2 3025
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Enquiries, Hopkins Research | last post by:
Hi all I have a conundrum that is puzzling me. I have a large codebase in C that I am converting to C++ as fast as possible (i.e. slowly because I keep learning new idioms and stumbling with...
2
by: Profetas | last post by:
why do I get segmentation Fault here? #include <stdio.h> #include <malloc.h> typedef enum {FALSE=0, TRUE=1} boolean; typedef struct { boolean base; unsigned int fitness; }individual;
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
18
by: steve | last post by:
I'm trying to create a structure of three pointers to doubles. For which I have: typedef struct { double *lst_t, *lst_vc, *lst_ic; } last_values; I then need to allocate space for...
5
by: caleb.vandyke | last post by:
I am working with some code that is doing some pointer to structure casts and I can't figure out how the cast is being done. Here is basically the code. #include <stdio.h> #include <stdlib.h> ...
7
by: Paminu | last post by:
In the following code I am trying to initialize a pointer that is located in a struct. #include <stdlib.h> #include <stdio.h> #define KIDS 4 typedef struct test { void *content;
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
5
by: friend.05 | last post by:
1) #include <stdio.h> #include <stdlib.h> #include "graph.h" #define MAX_VTX 4 struct _graph_vertex {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.