473,320 Members | 1,846 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,320 software developers and data experts.

passing struct by reference to function

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
4 13721
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

"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

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

Similar topics

4
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back...
5
by: {AGUT2} {H}-IWIK | last post by:
Hi, I'm trying to pass my vector to a function external to my main(), but my compiler is complaining. FYI, the struct for the facetInfo works perfectly, it's just the vector passing. A quick...
15
by: Dave | last post by:
I'm currently working on a small project (admitedly for my CS class) that compares the time difference between passing by value and passing by reference. I'm passing an array of 50000 int's. ...
5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
5
by: uny ternally | last post by:
I was experimenting in Visual C++ and ran into the following problem. I have the struct listed below. I also have a function that passes a variable of the struct type by reference and set the...
9
by: Just Me | last post by:
PARAFORMAT2 is a structure that SendMessage will return stuff in. Is the "ref" correct or since only a pointer is being passed should it be by value? Suppose I was passing data rather then...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
11
by: abhiM | last post by:
I have a struct that has an array in it. I need to assign space to the array in a function and pass the corresponding struct by reference to another function so that it can store values into the...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.