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

passing struct by reference to function

P: n/a
I need to pass a struct by reference to a function:
The struct:
typedef struct tifftags {
uint32 imageWidth, ...;
...
} TIFFTAGS;
typedef TIFFTAGS * TIFFTAGS_PTR;

main() {
.....
TIFFTAGS tags;

/* This function should update tags' members */
readTIFFTags(file, tif, &tags);
...
}

void readTIFFTags(char *file, TIFF *tif, TIFFTAGS_PTR tag) {

/* TIFFGetField should be called with the address of the variable
which should be updated */
if( !TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tags.imageWidth) )
CHECKTIFFTAG_CRITICAL("TIFFTAG_IMAGEWIDTH", file);

if( !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, tags->imageLength) )
CHECKTIFFTAG_CRITICAL("TIFFTAG_IMAGELENGTH", file);

......
}

The first syntax gives compile errors:
tiff/tiff_misc.c: In function `readTIFFTags':
tiff/tiff_misc.c:8: request for member `imageWidth' in something not a
structure or union
While the second syntax (tags->imageLength) gives a seg fault. Also
tried some other syntaxes.

Please let me know the correct syntax. Thanks,
Pushkar Pradhan

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


P: n/a
Pushkar Pradhan wrote:

typedef struct tifftags {
uint32 imageWidth, ...;
...
} TIFFTAGS;
typedef TIFFTAGS * TIFFTAGS_PTR;

void readTIFFTags(char *file, TIFF *tif, TIFFTAGS_PTR tag) {

/* TIFFGetField should be called with the address of the variable
which should be updated */
if( !TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tags.imageWidth) )
CHECKTIFFTAG_CRITICAL("TIFFTAG_IMAGEWIDTH", file);

if( !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, tags->imageLength) )
CHECKTIFFTAG_CRITICAL("TIFFTAG_IMAGELENGTH", file);

.....
}

The first syntax gives compile errors:
tiff/tiff_misc.c: In function `readTIFFTags':
tiff/tiff_misc.c:8: request for member `imageWidth' in something not a
structure or union
While the second syntax (tags->imageLength) gives a seg fault. Also
tried some other syntaxes.


The first problem I see is that the variable "tags" is undeclared in
readTIFFTags. Maybe you meant the second parameter to be "tags" instead of
"tag"? I'll assume that's the case.

&tags.imageWidth won't work, because tag is a pointer, not a struct--and
therefore you can't use the . operator on it.

I'm guessing you're having a problem with tags->imageLength because the
function TIFFGetField expects a pointer--that's what your comment says--and
tags->imageLength is a uint32, which I assume is not a pointer type.

Maybe you want to say

&tags->imageWidth

? "tags->imageWidth" gives you a uint32 object, and then the & operator
gives you its address.

--
Russell Hanneken
rg********@pobox.com
Remove the 'g' from my address to send me mail.
Nov 14 '05 #2

P: n/a

"Pushkar Pradhan" <pu*****@gri.msstate.edu> wrote in message
I need to pass a struct by reference to a function:
In C you pass a pointer to the structure, which you use to update it.
The struct:
typedef struct tifftags {
uint32 imageWidth, ...;
...
} TIFFTAGS;
typedef TIFFTAGS * TIFFTAGS_PTR;
This last typedef is asking for typedef trouble.
main() {
....
TIFFTAGS tags;

/* This function should update tags' members */
readTIFFTags(file, tif, &tags);
..
}

void readTIFFTags(char *file, TIFF *tif, TIFFTAGS_PTR tag) {

/* TIFFGetField should be called with the address of the variable
which should be updated */
if( !TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tags.imageWidth) )
This shoule be tags->imageWidth. You have typedefed your way into a
situation where you no longer understand what is going on.
CHECKTIFFTAG_CRITICAL("TIFFTAG_IMAGEWIDTH", file);

if( !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, tags->imageLength) )
This seems to be OK. Presuming the structure has a member "imageLength" you
should be able to access it. However segfaults can occur for many reasons.
Try
tags->imageLength = 0;
to ensure that this function is working OK, then look at the TIFFGetField
function.
CHECKTIFFTAG_CRITICAL("TIFFTAG_IMAGELENGTH", file);

.....
}

Good luck.
Nov 14 '05 #3

P: n/a

"Pushkar Pradhan" <pu*****@gri.msstate.edu> wrote in message
news:3F**************@gri.msstate.edu...
I need to pass a struct by reference to a function:
The struct:
typedef struct tifftags {
uint32 imageWidth, ...;
...
} TIFFTAGS;
typedef TIFFTAGS * TIFFTAGS_PTR;

main() {
....
TIFFTAGS tags;
This statement defines a pointer veriable of type TIFFTAGS or u can say
struct tifftags.


/* This function should update tags' members */
readTIFFTags(file, tif, &tags);
Here u r passing the address of pointer variable tags (I am right in case u
have written code in C).
..
}

void readTIFFTags(char *file, TIFF *tif, TIFFTAGS_PTR tag) {
This function prototype says that u r receiving third argument as a pointer
of type TIFFTAGS. Which is actually wrong it should be TIFFTAGS *tags.
Because while passing u passed pointer to pointer.

/* TIFFGetField should be called with the address of the variable
which should be updated */
if( !TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &tags.imageWidth) )
I dont about this statement what u r trying to do.
CHECKTIFFTAG_CRITICAL("TIFFTAG_IMAGEWIDTH", file);

if( !TIFFGetField(tif, TIFFTAG_IMAGELENGTH, tags->imageLength) )


This is not at all possible. Because function calling and function
definition does not match.
Here if u want to access imageWidth value then use (*tags)->imageWidth.
If you want its address use &((*tags)->imageWidth).
This is possible only if u take care while receiving your third parameter.
With Regards.
SanBid.
Nov 14 '05 #4

P: n/a
Santoshkumar B wrote:

"Pushkar Pradhan" <pu*****@gri.msstate.edu> wrote in message
news:3F**************@gri.msstate.edu...
I need to pass a struct by reference to a function:
The struct:
typedef struct tifftags {
uint32 imageWidth, ...;
...
} TIFFTAGS;
typedef TIFFTAGS * TIFFTAGS_PTR;

main() {
....
TIFFTAGS tags;


This statement defines a pointer veriable of type TIFFTAGS


No, it doesn't. It defines a struct object, not a pointer.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.