473,587 Members | 2,580 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(fi le, tif, &tags);
...
}

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

/* TIFFGetField should be called with the address of the variable
which should be updated */
if( !TIFFGetField(t if, TIFFTAG_IMAGEWI DTH, &tags.imageWidt h) )
CHECKTIFFTAG_CR ITICAL("TIFFTAG _IMAGEWIDTH", file);

if( !TIFFGetField(t if, TIFFTAG_IMAGELE NGTH, tags->imageLength) )
CHECKTIFFTAG_CR ITICAL("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 13741
Pushkar Pradhan wrote:

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

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

/* TIFFGetField should be called with the address of the variable
which should be updated */
if( !TIFFGetField(t if, TIFFTAG_IMAGEWI DTH, &tags.imageWidt h) )
CHECKTIFFTAG_CR ITICAL("TIFFTAG _IMAGEWIDTH", file);

if( !TIFFGetField(t if, TIFFTAG_IMAGELE NGTH, tags->imageLength) )
CHECKTIFFTAG_CR ITICAL("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.imageWidt h 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********@pobo x.com
Remove the 'g' from my address to send me mail.
Nov 14 '05 #2

"Pushkar Pradhan" <pu*****@gri.ms state.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(fi le, tif, &tags);
..
}

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

/* TIFFGetField should be called with the address of the variable
which should be updated */
if( !TIFFGetField(t if, TIFFTAG_IMAGEWI DTH, &tags.imageWidt h) )
This shoule be tags->imageWidth. You have typedefed your way into a
situation where you no longer understand what is going on.
CHECKTIFFTAG_CR ITICAL("TIFFTAG _IMAGEWIDTH", file);

if( !TIFFGetField(t if, TIFFTAG_IMAGELE NGTH, tags->imageLength) )
This seems to be OK. Presuming the structure has a member "imageLengt h" 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_CR ITICAL("TIFFTAG _IMAGELENGTH", file);

.....
}

Good luck.
Nov 14 '05 #3

"Pushkar Pradhan" <pu*****@gri.ms state.edu> wrote in message
news:3F******** ******@gri.msst ate.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(fi le, 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(ch ar *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(t if, TIFFTAG_IMAGEWI DTH, &tags.imageWidt h) )
I dont about this statement what u r trying to do.
CHECKTIFFTAG_CR ITICAL("TIFFTAG _IMAGEWIDTH", file);

if( !TIFFGetField(t if, TIFFTAG_IMAGELE NGTH, 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.ms state.edu> wrote in message
news:3F******** ******@gri.msst ate.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.pow ernet.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
3585
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 in my variables ?? note1: I'm not the one who wrote the web service.. so I can't modify
5
3174
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 overview... I have four sets of co-ordinates in each 'facetInfo', and a vector of 'facetInfo's. I want to strip those 'facetInfo' objects that have...
15
4661
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. However, since in C++ an array is passed by reference by default I need to embed the array into a struct in order to pass it by value. The problem is...
5
34353
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 the actual function call. I will post the code below of the structure, the prototype and and function call and if someone can explain this I would...
5
2451
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 values. When I access the struct in the global space after calling the function the integer value is correcty, however the LPTSTR value is empty. ...
9
2292
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 receiving it, would that change the answer to the above?
12
5377
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
11
3362
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 array. When I try it with the following code i get these errors 1. Use of possibly unassigned field 'micData' 2. The out parameter 'rem' must be...
8
3492
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
7843
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7967
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6621
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3840
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.