Quote:
> Hello,
[snip].
It would be very helpful if you could post complete programs. It makes
it much easier to talk about. I think you have:
#include <stdio.h>
#include <stdlib.h>
static time_t a_time; /* a_time will have value 0 */
int hello(time_t *pTime)
{
pTime = &a_time; /* See below */
return 0;
Quote:
}
int main()
{
time_t* timest = malloc(sizeof(time_t));
hello(timest);
printf("timest is now pointing to: %ld \n", *timest);
return 0;
Quote:
}
There are a few problems with your code that are *NOT* what you are
asking about:
a) You don't need to cast the result of malloc, and if you don't, the
compiler will warn you if you omit the #include <stdlib.h> (so don't
cast).
b) You are printing time_t with %ld. That won't work on an arbitary ISO
C implimentation. Turn it into a string with ctime
c) You have leaked the memory you allocated with malloc.
The problem is that hello is not altering the variable timest in main.
It is altering a COPY of that variable. In my rewrite of your code, I
used a different formal parameter name to hello, in order to clarify
that point (although in real code I would tend to use the same name).
The problem may be clearer if I rewrite the code again with a typedef:
#include <stdio.h>
#include <stdlib.h>
static time_t a_time; /* a_time will have value 0 */
typdef time_t *pTime_t;
int hello(pTime_t pTime)
{
pTime = &a_time; /* Now you can see that pTime is passed by
value, and you are altering that */
return 0;
Quote:
}
int main()
{
pTime_t timest = malloc(sizeof(time_t));
hello(timest);
printf("timest is now pointing to: %ld \n", *timest);
return 0;
Quote:
}
--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---
--