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

Weird Pointer In C, Please Help. Code supply...

//Working Example:

#include <stdio.h>
#include <time.h>

int main ()
{
time_t rawtime; /* define rawtime as time_t */

time ( &rawtime );
printf ( "Current date and time are: %s", ctime (&rawtime) ); /*call
ctime use &rawtime*/

return 0;
}

//Not Working Example:

#include <stdio.h>
#include <time.h>

int main ()
{
time_t *rawtime; /* define rawtime as pointer point to time_t */

time ( rawtime );
printf ( "Current date and time are: %s", ctime (rawtime) ); /* call
ctime use rawtime */

return 0;
}

it's very weird in the not working example. they are all pointer.

More example here.
char t_time;
read(fd, &t_time, 25); /* working */

char *t_time;
read(fd, t_time, 25); /* Not working */

Somebody help. i get more confuse on the pointer when i write program
in C. thank you.

Oct 27 '06 #1
12 1905

MQ*****@gmail.com wrote:
//Working Example:

#include <stdio.h>
#include <time.h>

int main ()
{
time_t rawtime; /* define rawtime as time_t */
Out of curiosity, why are you making this comment? This is obvious from
the code itself.
time ( &rawtime );
printf ( "Current date and time are: %s", ctime (&rawtime) ); /*call
ctime use &rawtime*/
Again, obvious comment. A better comment would quickly explain what
ctime does.
return 0;
}

//Not Working Example:

#include <stdio.h>
#include <time.h>

int main ()
{
time_t *rawtime; /* define rawtime as pointer point to time_t */

time ( rawtime );
You are sending a NULL pointer; there is no allocated memory you're
pointing to. See malloc().
printf ( "Current date and time are: %s", ctime (rawtime) ); /* call
ctime use rawtime */

return 0;
}

it's very weird in the not working example. they are all pointer.
I don't see any advantage to using a pointer and allocating over using
a straight variable. I recommend using the first example.
More example here.
char t_time;
read(fd, &t_time, 25); /* working */

char *t_time;
read(fd, t_time, 25); /* Not working */
Again, you're not allocating memory in the second example.
Somebody help. i get more confuse on the pointer when i write program
in C. thank you.
In C, you have to allocate and free memory cells by hand. See malloc()
and free().

Oct 27 '06 #2
In article <11*********************@m73g2000cwd.googlegroups. com>,
Chris Johnson <ef******@gmail.comwrote:
>MQ*****@gmail.com wrote:
>//Not Working Example:
>#include <stdio.h>
#include <time.h>
>int main ()
{
time_t *rawtime; /* define rawtime as pointer point to time_t */
> time ( rawtime );
>You are sending a NULL pointer; there is no allocated memory you're
pointing to. See malloc().
You are correct about there being no allocated memory, but the
pointer being sent in could be anything, because 'auto' variables
are not automatically initialized to anything.
To emphasize to the original poster:

time_t *rawtime; only declares space to hold the pointer itself,
and does not allocate memory to hold anything pointed to.

You could, for example, say,

time_t rawtime;
time_t *rawtimeptr = &rawtime;
time( rawtimeptr );

A pointer can be used to point the beginning of an existing
object, or to point into a distinct part of an existing object
(except a bitfield), or a pointer can be used to hold the address
of memory allocated using malloc() or calloc().

A pointer can also be assigned NULL, which is promised not to point to any
object.

A pointer may also legally be set to point immediately -after- the
end of an object, provided that there is no attempt to access
memory at that location.
--
Prototypes are supertypes of their clones. -- maplesoft
Oct 27 '06 #3

Chris Johnson wrote:
MQ*****@gmail.com wrote:
//Working Example:

#include <stdio.h>
#include <time.h>

int main ()
{
time_t rawtime; /* define rawtime as time_t */

Out of curiosity, why are you making this comment? This is obvious from
the code itself.
time ( &rawtime );
printf ( "Current date and time are: %s", ctime (&rawtime) ); /*call
ctime use &rawtime*/

Again, obvious comment. A better comment would quickly explain what
ctime does.
return 0;
}

//Not Working Example:

#include <stdio.h>
#include <time.h>

int main ()
{
time_t *rawtime; /* define rawtime as pointer point to time_t */

time ( rawtime );

You are sending a NULL pointer; there is no allocated memory you're
pointing to. See malloc().
printf ( "Current date and time are: %s", ctime (rawtime) ); /* call
ctime use rawtime */

return 0;
}

it's very weird in the not working example. they are all pointer.

I don't see any advantage to using a pointer and allocating over using
a straight variable. I recommend using the first example.
More example here.
char t_time;
read(fd, &t_time, 25); /* working */

char *t_time;
read(fd, t_time, 25); /* Not working */

Again, you're not allocating memory in the second example.
Somebody help. i get more confuse on the pointer when i write program
in C. thank you.

In C, you have to allocate and free memory cells by hand. See malloc()
and free().

So, when i declare the pointer, it dosen't really exist until i call
the malloc(). thanks so much Chris. i have lots of C stuff to learn :)

Oct 27 '06 #4
Chris Johnson <ef******@gmail.comwrote:
time_t *rawtime; /* define rawtime as pointer point to time_t */

time ( rawtime );
You are sending a NULL pointer
Nit: The pointer is uninitialized, which means that it may or may not
be a null pointer and may or may not compare equal to NULL. As
written, the code yields undefined behavior, but

time_t *rawtime = NULL;

time( rawtime );

is perfectly valid, albeit useless.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Oct 27 '06 #5
MQ*****@gmail.com said:

<snip>
So, when i declare the pointer, it dosen't really exist until i call
the malloc().
The pointer exists just fine. It simply doesn't point anywhere! The malloc
function assigns you a block of memory and returns a pointer to that
block's start address (or returns NULL to tell you it can't meet your
request). That's one way to give a pointer a useful value:

p = malloc(n * sizeof *p);

There are, of course, other ways.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 27 '06 #6


MQ*****@gmail.com wrote On 10/27/06 11:19,:
[...]

So, when i declare the pointer, it dosen't really exist until i call
the malloc(). thanks so much Chris. i have lots of C stuff to learn :)
The pointer exists, but it isn't pointing to anything.
Y'know those credit card come-ons that appear in the mail,
the ones with little bits of cardboard or plastic that look
like an actual credit card? Try to buy something with one
of them, and you'll learn the difference between a card with
an actual account number and one with a bogus number -- a
pointer whose value points to something and a pointer whose
value is bogus.

--
Er*********@sun.com

Oct 27 '06 #7

Eric Sosman wrote:
MQ*****@gmail.com wrote On 10/27/06 11:19,:
[...]

So, when i declare the pointer, it dosen't really exist until i call
the malloc(). thanks so much Chris. i have lots of C stuff to learn :)

The pointer exists, but it isn't pointing to anything.
Y'know those credit card come-ons that appear in the mail,
the ones with little bits of cardboard or plastic that look
like an actual credit card? Try to buy something with one
of them, and you'll learn the difference between a card with
an actual account number and one with a bogus number -- a
pointer whose value points to something and a pointer whose
value is bogus.

--
Er*********@sun.com

Here is the first time i use the pointer and it works. in this example
i write the file use fputs library call, when i use" write" system
call, it fails. fputs and write all require " const char *str "
paramater.
/* Working,, why?? */

#include <stdio.h>
#include <time.h>
#include <string.h>

int main (int argc,char *argv[])
{
int i;
FILE *in;
time_t *c_time; /* c_time should point to anywhere.*/

for (i = 1; i < argc; i++){
in = fopen(argv[i],"r+");
time(c_time);
fputs(ctime(c_time), in);
fclose(in);
}
return 0;
}

Oct 27 '06 #8
MQ*****@gmail.com wrote:
//Working Example:

#include <stdio.h>
#include <time.h>

int main ()
{
time_t rawtime; /* define rawtime as time_t */
Useless comment.
time ( &rawtime );
printf ( "Current date and time are: %s", ctime (&rawtime) ); /*call
ctime use &rawtime*/
Another useless comment. Comments should say something not
obvious from the code. /Why/ something is being done is
one possibility.

(fx:snip)
int main ()
{
time_t *rawtime; /* define rawtime as pointer point to time_t */

time ( rawtime );
`rawtime` is a pointer, but it has no defined value. So using it is
a mistake. The value of an uninitialised variable is poison, unless
that variable is static (in which case it's a suitable zero).

--
Chris "back home once again ...." Dollin
"- born in the lab under strict supervision -", - Magenta, /Genetesis/

Oct 27 '06 #9
MQ*****@gmail.com wrote:
>
Eric Sosman wrote:
>MQ*****@gmail.com wrote On 10/27/06 11:19,:
[...]

So, when i declare the pointer, it dosen't really exist until i call
the malloc(). thanks so much Chris. i have lots of C stuff to learn :)

The pointer exists, but it isn't pointing to anything.
Y'know those credit card come-ons that appear in the mail,
the ones with little bits of cardboard or plastic that look
like an actual credit card? Try to buy something with one
of them, and you'll learn the difference between a card with
an actual account number and one with a bogus number -- a
pointer whose value points to something and a pointer whose
value is bogus.

Here is the first time i use the pointer and it works.
You were unlucky.
in this example
i write the file use fputs library call, when i use" write" system
call, it fails. fputs and write all require " const char *str "
paramater.
(moved section of code)
time_t *c_time; /* c_time should point to anywhere.*/

for (i = 1; i < argc; i++){
in = fopen(argv[i],"r+");
time(c_time);
fputs(ctime(c_time), in);
fclose(in);
}
When you do `time(c_time)`, you're implementation is free to
ignore the fact that the value of `c_time` is poison. It
treats it as a legal pointer value. Unluckily, it seems that
the value looks like a legal pointer to somewhere, so `time`
trashes that location. It could be somewhere important: part
of the malloc heap, the return address location for `main`,
the FILE* structure of `stdout` - anything.

You have now broken your program. It may look like it's
working, but that's just bad luck.

As to why it fails if you use `fwrite`, that's just /good/
luck. Probably you trashed a more sensitive location.

--
Chris "back home once again ...." Dollin
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/

Oct 27 '06 #10


MQ*****@gmail.com wrote On 10/27/06 11:54,:
Eric Sosman wrote:
>>MQ*****@gmail.com wrote On 10/27/06 11:19,:
>>>[...]

So, when i declare the pointer, it dosen't really exist until i call
the malloc(). thanks so much Chris. i have lots of C stuff to learn :)

The pointer exists, but it isn't pointing to anything.
Y'know those credit card come-ons that appear in the mail,
the ones with little bits of cardboard or plastic that look
like an actual credit card? Try to buy something with one
of them, and you'll learn the difference between a card with
an actual account number and one with a bogus number -- a
pointer whose value points to something and a pointer whose
value is bogus.

--
Er*********@sun.com

Here is the first time i use the pointer and it works. in this example
i write the file use fputs library call, when i use" write" system
call, it fails. fputs and write all require " const char *str "
paramater.
/* Working,, why?? */

#include <stdio.h>
#include <time.h>
#include <string.h>

int main (int argc,char *argv[])
{
int i;
FILE *in;
time_t *c_time; /* c_time should point to anywhere.*/

for (i = 1; i < argc; i++){
in = fopen(argv[i],"r+");
time(c_time);
fputs(ctime(c_time), in);
fclose(in);
}
return 0;
}
One of the defects of my credit card analogy is that the
card-like thing they mail you is carefully printed with an
account number that cannot be valid, and will definitely be
detected as invalid if you try to use the fake card.

The pointer variable c_time, though, contains "random
garbage," just like any other `auto' variable that has not
been initialized. By sheer luck (decide for yourself whether
it's good luck or bad), that garbage might just happen to
point at a chunk of accessible memory. The time() function
might succeed in writing to that memory, and ctime() might
then succeed in reading from it, and the program might appear
to work. Make a tiny change that "shouldn't" make a difference,
and you get slightly different random garbage, and the program
suddenly and mysteriously starts misbehaving.

Of course, you can't count on c_time pointing anywhere
in particular, and there's no telling what other variable
might occupy the memory it happens to point at. Sometimes
the garbage pointer will be altogether invalid and the code
will (probably) crash detectably; other times you'll just
scribble over something else and you may or may not suffer
from having done so.

It's as if those fake credit cards were imprinted with
completely random digits: Most such cards would probably have
invalid account numbers and get rejected if you tried to use
them, but by pure chance a few of the account numbers might
actually be valid. You might succeed in making purchases with
"random garbage" account numbers, and maybe you'd even get
away with it. But be careful! It might happen that the
account number you stumble on belongs to Bennie the Bullet.
Everybody loves Bennie -- at least, nobody he's been mad at
seems to come around any more ...

--
Er*********@sun.com

Oct 27 '06 #11
Christopher Benson-Manica <at***@ukato.freeshell.orgwrites:
Chris Johnson <ef******@gmail.comwrote:
time_t *rawtime; /* define rawtime as pointer point to time_t */

time ( rawtime );
>You are sending a NULL pointer

Nit: The pointer is uninitialized, which means that it may or may not
be a null pointer and may or may not compare equal to NULL. As
written, the code yields undefined behavior, but

time_t *rawtime = NULL;

time( rawtime );

is perfectly valid, albeit useless.
Right, and that's only because the time() function specifically does
not attempt to write the result through its pointer argument if the
argument is a null pointer. If it weren't for that special-case rule,
then the behavior of the above code would be equally undefined.

If the time() function were being defined today, there would be no
reason for it to use a pointer at all. It almost certainly would take
no arguments and return a time_t result:

time_t t = time(); /* NOT VALID C */

Its odd definition, returning a result *and* optionally storing the
same result via a pointer object, is for historical reasons (in
ancient versions of C, a long int was implemented as an array of two
16-bit ints, and you couldn't treat it as a value).

If you're trying to learn about pointers, using the time() function
isn't a bad way to do it. If you're just trying to get the current
time, there's no point in using the argument; just call time(NULL) and
use the result.

--
Keith Thompson (The_Other_Keith) 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.
Oct 27 '06 #12
Chris Dollin <ch**********@hp.comwrites:
MQ*****@gmail.com wrote:
>//Working Example:

#include <stdio.h>
#include <time.h>

int main ()
{
time_t rawtime; /* define rawtime as time_t */

Useless comment.
[...]

Not at all.

In production code, yes, a comment like that would be useless, because
you expect both the author and any readers to know exactly what
"time_t rawtime;" means.

But look at it in context. The OP posted two sample programs. In one,
he had:

time_t rawtime; /* define rawtime as time_t */

In the other, he had:

time_t *rawtime; /* define rawtime as pointer point to time_t */

The comments emphasize the difference between the two alternative
declarations, which is an important point. (Incidentally, I would
have used different names.)

Furthermore, though most of us know perfectly well what the
declarations mean, the comments make it clear *to us* that the OP
knows what they mean.

A comment like this:

x = 42; /* assign 42 to x */

is useful if (and only if) the point is to explain assignment
statements.

--
Keith Thompson (The_Other_Keith) 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.
Oct 27 '06 #13

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

Similar topics

9
by: Chris Jankowski | last post by:
Hi all, I am still new to using Python, so bare with me. I am trying to call a DLL from my program to use some of it's functions. The only parameter is supposed to be a pointer to an image. I...
2
by: Todd A. Anderson | last post by:
Hi...any help would be appreciated. Kind of a weird scenario but I have foo's that point to bars and whenever anybody tries to use a foo as a pointer I want it typed as a baz * and likewise if...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
26
by: Bill Reid | last post by:
Bear with me, as I am not a "professional" programmer, but I was working on part of program that reads parts of four text files into a buffer which I re-allocate the size as I read each file. I...
1
by: Frederick Dean | last post by:
Hi, guys! Please look at the very simple code below: //---------------code begin------------------------ void f() { cout << "f function " << endl; } void (*pf)() = &f;
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
18
by: atv | last post by:
at least to me it is. I can't figure out for the life what it is i'm doing wrong here. i have a function called assign_coordinate. usually, i pass a malloced pointer to it, then individual...
14
by: WStoreyII | last post by:
the following code is supposed to read a whole line upto a new line char from a file. however it does not work. it is producing weird results. please help. I had error checking in there for...
14
by: leptone | last post by:
Dear all, I am programming a PLC with an 80188 processor, hence I am using an old version of Borland C++ (3.10). While doing the job, I've encountered strange behaviours and I've isolated the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.