Pointer to long int 
Author Message
 Pointer to long int

Hello,

In my main(), I have the line:

time_t * timest       = (time_t *) malloc(sizeof(time_t));

, where time_t is a long int.

I then call a functioni with the following prototype:

int hello(time_t *);

I call it using:
hello(timest);

In the function, I simply make timest point to something:

timest = &(a_time);

, where a_time is of type time_t.

However, when I print the value of *timest, I get a 6-digit
number that is totally different to a_time, which is a 9-digit
number eg. a_time = 123456789, *timest = 745293.

I print the value using:

printf("timest is now pointing to: %ld \n", *timest);

Any ideas, please? :)

Daniel.
--



Sun, 28 Oct 2001 03:00:00 GMT  
 Pointer to long int

Quote:

>Hello,

>In my main(), I have the line:

>time_t * timest       = (time_t *) malloc(sizeof(time_t));

>, where time_t is a long int.

>I then call a functioni with the following prototype:

>int hello(time_t *);

>I call it using:
>hello(timest);

>In the function, I simply make timest point to something:

>timest = &(a_time);

[...]

timest now points to the _address_ of a_time. I think this is what you want:

int hello(time_t *timest)
{
  time_t a_time = 123456789;
  *timest = a_time;
  return 1;

Quote:
}

Hth,

Thurman Gillespy
Seattle, Washington
--



Mon, 29 Oct 2001 03:00:00 GMT  
 Pointer to long int

Quote:

> printf("timest is now pointing to: %ld \n", *timest);

When printing out values of type time_t or such, I usually cast them
to some other type so that I know the format specifier matches the
type of the value.

printf("timest is now pointing to: %ld \n", (long) *timest);

But I don't think this was your problem, since you wrote time_t was
already long int.

Quote:
> Any ideas, please? :)

I couldn't quite follow your description of the problem; please show
the entire program which doesn't work.
--



Mon, 29 Oct 2001 03:00:00 GMT  
 Pointer to long int
Thanks Thurman, and everyone who replied, my c knowledge was getting a bit rusty.
The problem is solved now, Thurman got it in one.

Dan.


: >Hello,
: >
: >In my main(), I have the line:
: >
: >time_t * timest       = (time_t *) malloc(sizeof(time_t));
: >
: >
: >, where time_t is a long int.
: >
: >I then call a functioni with the following prototype:
: >
: >int hello(time_t *);
: >
: >I call it using:
: >hello(timest);
: >
: >In the function, I simply make timest point to something:
: >
: >timest = &(a_time);
: [...]

: timest now points to the _address_ of a_time. I think this is what you want:

: int hello(time_t *timest)
: {
:   time_t a_time = 123456789;
:   *timest = a_time;
:   return 1;
: }

: Hth,

: Thurman Gillespy
: Seattle, Washington
: --

--

________________________________________________________________

Daniel Ng

University of Melbourne, Australia

BE(Computer Engineering) Honours,
Final Year BSc(Computer Science)
----------------------------------------------------------------
--



Tue, 30 Oct 2001 03:00:00 GMT  
 Pointer to long int

Quote:

> Hello,

> In my main(), I have the line:

> time_t * timest       = (time_t *) malloc(sizeof(time_t));

> , where time_t is a long int.

> I then call a functioni with the following prototype:

> int hello(time_t *);

> I call it using:
> hello(timest);

> In the function, I simply make timest point to something:

> timest = &(a_time);

> , where a_time is of type time_t.

> However, when I print the value of *timest, I get a 6-digit
> number that is totally different to a_time, which is a 9-digit
> number eg. a_time = 123456789, *timest = 745293.

> I print the value using:

> printf("timest is now pointing to: %ld \n", *timest);

> Any ideas, please? :)

> Daniel.
> --


I beleive the problem is the variable a_time is out of scope once the
function
returns. Having a variable which is local to a function be returned has
unpredictable and possibly undesireable effects. What you want is

*timest = a_time;

This will be a valid memory address in the scope of the main function.

Norman Tackett.
--



Sat, 03 Nov 2001 03:00:00 GMT  
 Pointer to long int

Quote:

>In my main(), I have the line:

>time_t * timest       = (time_t *) malloc(sizeof(time_t));

>, where time_t is a long int.

>I then call a functioni with the following prototype:

>int hello(time_t *);

>I call it using:
>hello(timest);

>In the function, I simply make timest point to something:

>timest = &(a_time);

>, where a_time is of type time_t.

Note that 'hello' is passed a _copy_ of 'timest'; any changes made to
the copy will not be reflected in the original.

Perhaps you mean

  *timest = a_time;

so that the time_t value contained in 'a_time' is assigned to the
object pointed at by 'timest'.

Also note that unless 'a_time' is an external ("global") or static
object, it will cease to exist upon return from 'hello'. Any attempt
to access a pointer to it outside 'hello' would result in undefined
behaviour: nothing, strange results, crashes, or worse.

Quote:
>, where a_time is of type time_t.

>However, when I print the value of *timest, I get a 6-digit
>number that is totally different to a_time, which is a 9-digit
>number eg. a_time = 123456789, *timest = 745293.

-- Mat.

--



Fri, 09 Nov 2001 03:00:00 GMT  
 Pointer to long int

NG) wrote in comp.lang.c.moderated:

Quote:
> Hello,

> In my main(), I have the line:

> time_t * timest       = (time_t *) malloc(sizeof(time_t));

> , where time_t is a long int.

> I then call a functioni with the following prototype:

> int hello(time_t *);

> I call it using:
> hello(timest);

> In the function, I simply make timest point to something:

> timest = &(a_time);

> , where a_time is of type time_t.

> However, when I print the value of *timest, I get a 6-digit
> number that is totally different to a_time, which is a 9-digit
> number eg. a_time = 123456789, *timest = 745293.

> I print the value using:

> printf("timest is now pointing to: %ld \n", *timest);

> Any ideas, please? :)

> Daniel.

<Jack>

A time_t might be a long int on your implementation, that is not
guaranteed by the C or POSIX standards.

What you didn't mention was whether the printf() call is in the
function hello(), or in the calling function after hello() returned.

Remember hello() gets a copy of the pointer timest.  If it changes its
copy to point to something, that does not change what the original
timest back in the calling function points to.

</Jack>
--
Do not email me with questions about programming.
Post them to the appropriate newsgroup.
Followups to my posts are welcome.

--



Fri, 09 Nov 2001 03:00:00 GMT  
 Pointer to long int


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.---
--



Fri, 09 Nov 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. char size (was long long long long long int)

2. typecasting int to long long int

3. Long int as big as pointer?

4. long long int

5. long long int into octets (and back again)

6. long long int

7. unsigned long long int

8. long long int

9. Assigning and printing long long int's

10. long int versus long

11. long long int

12. Help! INT longer than LONG

 

 
Powered by phpBB® Forum Software