473,623 Members | 2,433 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing pointer as function parameter

Hello,

I belive the reason of problem is simple, but can't figure out.

This is piece of code:

struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};

extern void proc_v4(char *, ssize_t, struct timeval *);
....

int main(void)
{
static char recvbuf[BUFSIZE];
ssize_t n;
struct timeval tv2;

...

proc_v4(recvbuf , n, &tv2);
}

void proc_v4(char *ptr, ssize_t len, struct timeval *tvsend)
{
struct icmp *icmp;
...
/* here I parse IP packet and address pointer on ICMP packet */
...
tvsend = (struct timeval *) icmp->icmp_data;
}

The problem is after calling proc_v4() the value of 'tv2' doesn't change,
what I can't understand, because I pass pointer, and then re-assign pointer
into another one.

Where is the problem? Thanks a lot.

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Sep 14 '06 #1
6 3019
Roman Mashak wrote:
Hello,

I belive the reason of problem is simple, but can't figure out.

This is piece of code:

struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};

extern void proc_v4(char *, ssize_t, struct timeval *);
...

int main(void)
{
static char recvbuf[BUFSIZE];
ssize_t n;
struct timeval tv2;

...

proc_v4(recvbuf , n, &tv2);
}

void proc_v4(char *ptr, ssize_t len, struct timeval *tvsend)
{
struct icmp *icmp;
...
/* here I parse IP packet and address pointer on ICMP packet */
...
tvsend = (struct timeval *) icmp->icmp_data;
What you probably want to do is assign values to the elements of
tvsend. Even if you had done everything else right (and you
haven't), icmp->icmp_data might cease to exist when proc_v4 ends,
depending on where it came from, so you don't want to keep a
pointer to it unless you know that it will live on.
}

The problem is after calling proc_v4() the value of 'tv2' doesn't change,
what I can't understand, because I pass pointer, and then re-assign pointer
into another one.

Where is the problem? Thanks a lot.
You aren't doing what you think you are doing. What you think
you are doing is something like:

struct timeval *tv2;
proc_v4(..., &tv2);

This is very different from what you are really doing.

--
Thomas M. Sommers -- tm*@nj.net -- AB2SB

Sep 14 '06 #2
Roman Mashak wrote:
void proc_v4(char *ptr, ssize_t len, struct timeval *tvsend)
{
struct icmp *icmp;
...
/* here I parse IP packet and address pointer on ICMP packet */
...
tvsend = (struct timeval *) icmp->icmp_data;
This line assigns to the tvsend pointer. the tvsend pointer
is local to this function.

You want to assign to what it points at rather, as the caller pointed
it at a struct timeval it can access:

*tvsend = *(struct timeval *) icmp->icmp_data;

Or
struct timeval *tmp

tmp = (struct timeval *) icmp->icmp_data;
tvsend->tv_sec = tmp->tv_sec;
tvsend->tv_usec = tmp->tv_usec;
}
Another problem is icmp->icmp_data; might indeed not values
a struct timeval that your platform can understand..
(alignment problems, padding and endianess might skew things)

Sep 14 '06 #3
Roman Mashak wrote:

void proc_v4(char *ptr, ssize_t len, struct timeval *tvsend)
{
struct icmp *icmp;
...
/* here I parse IP packet and address pointer on ICMP packet */
...
tvsend = (struct timeval *) icmp->icmp_data;
}

The problem is after calling proc_v4() the value of 'tv2' doesn't
change, what I can't understand, because I pass pointer, and then
re-assign pointer into another one.
That's covered in the FAQs:

<http://c-faq.com/ptrs/passptrinit.htm l>


Brian
Sep 14 '06 #4
Hello, Default!
You wrote on 14 Sep 2006 17:59:36 GMT:

??>void proc_v4(char *ptr, ssize_t len, struct timeval *tvsend)
??>{
??> struct icmp *icmp;
??> ...
??> /* here I parse IP packet and address pointer on ICMP packet */
??> ...
??> tvsend = (struct timeval *) icmp->icmp_data;
??>}
??>>
??>The problem is after calling proc_v4() the value of 'tv2' doesn't
??>change, what I can't understand, because I pass pointer, and then
??>re-assign pointer into another one.

DUThat's covered in the FAQs:

DU<http://c-faq.com/ptrs/passptrinit.htm l>
Hm, I'm confused. FAQ reecommends to pass the function address of pointer.
Is it considered to be a "right standard confirming way" ?
On the other hand: passing just pointer and assigning the value to what the
pointer points at (as was proposed by Nils O. Selasdal) - works well.

Which way is correct?

Thank you.

With best regards, Roman Mashak. E-mail: mr*@tusur.ru
Sep 15 '06 #5
"Roman Mashak" <mr*@tusur.ruwr ites:
Hello, Default!
You wrote on 14 Sep 2006 17:59:36 GMT:

??>void proc_v4(char *ptr, ssize_t len, struct timeval *tvsend)
??>{
??> struct icmp *icmp;
??> ...
??> /* here I parse IP packet and address pointer on ICMP packet */
??> ...
??> tvsend = (struct timeval *) icmp->icmp_data;
??>}
??>>
??>The problem is after calling proc_v4() the value of 'tv2' doesn't
??>change, what I can't understand, because I pass pointer, and then
??>re-assign pointer into another one.

DUThat's covered in the FAQs:

DU<http://c-faq.com/ptrs/passptrinit.htm l>
Hm, I'm confused. FAQ reecommends to pass the function address of pointer.
Is it considered to be a "right standard confirming way" ?
On the other hand: passing just pointer and assigning the value to what the
pointer points at (as was proposed by Nils O. Selasdal) - works well.

Which way is correct?
It depends on what you want to do.

In general, if you have a FOO, and you want a function to modify its
value for you, you need to pass the function a FOO* (i.e., you pass it
a pointer to a FOO so it can modify your FOO).

If the FOO itself happens to be a pointer to something (i.e., you want
the function to modify a pointer object for you), then passing a FOO*
means passing a pointer-to-pointer-to-something.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 15 '06 #6
Roman Mashak wrote:
Hello, Default!
You wrote on 14 Sep 2006 17:59:36 GMT:
DU<http://c-faq.com/ptrs/passptrinit.htm l>
Hm, I'm confused. FAQ reecommends to pass the function address of
pointer. Is it considered to be a "right standard confirming way" ?
On the other hand: passing just pointer and assigning the value to
what the pointer points at (as was proposed by Nils O. Selasdal) -
works well.

Which way is correct?
Ok, I think I confused your post with one in a different thread.


Brian
Sep 15 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
10114
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
8
4107
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
4
3812
by: sam1967 | last post by:
How do I get a function to return a GMP integer type mpz_t when i try it i get an error message. i am trying mpz_t hooch (int x) { mpz_t y; ........
6
8220
by: bob_jenkins | last post by:
{ const void *p; (void)memset((void *)p, ' ', (size_t)10); } Should this call to memset() be legal? Memset is of type void *memset(void *, unsigned char, size_t) Also, (void *) is the generic pointer type. My real question is, is (void *) such a generic pointer type that it
11
4453
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to accomplish. // - - - - - - - - begin code - - - - - - - typedef int sm_t; typedef int bg_t; sm_t sm; bg_t bg;
3
1990
by: Miguel | last post by:
Hi, I'm new to .NET and am using VB .NET as I am from a VB background. I am having difficulty understanding the way .NET handles, passes and uses objects. I have had a look at the Micrsoft Data Access Application blocks for .NET and am struggling to understand the following: 1. When creating a function that returns a datareader, there is a datareader instantiated inside the function. This datareader is then returned to the
11
8114
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
7
3296
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
5
1470
by: CapCity | last post by:
I'm sure I'm missing something simple - I do not code in C regularly, and the breaks are long enough for me to forget. The situation I have is I need to create an array but I do not know the dimension until runtime. I pass the pointer to a function which then determines the size and then creates and populates it. I can walk the array OK in the function, but the app crashes when I try to do so in the calling routine. Here's a small...
0
8227
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8326
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7150
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6106
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4164
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2602
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
1778
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1473
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.