473,836 Members | 1,596 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1934

MQ*****@gmail.c om 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************ *********@m73g2 000cwd.googlegr oups.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.c om 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)gma il.com | don't, I need to know. Flames welcome.
Oct 27 '06 #5
MQ*****@gmail.c om 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.c om 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.c om 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_t ime), in);
fclose(in);
}
return 0;
}

Oct 27 '06 #8
MQ*****@gmail.c om 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.c om wrote:
>
Eric Sosman wrote:
>MQ*****@gmail.c om 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_t ime), 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

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

Similar topics

9
2807
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 have tried several variations on passing this parameter to the DLL and I am not having much luck. This is going to call other functions later for OCR, but I want to get a simple one working first. Here is my code: (Very basic)
2
1549
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 they do foo-> then also typed as a baz *. So I overload some operators as shown below. In Visual Studio 6, I tried simply to printf a foo and instead of the getting the value of the baz I got the value of bar *. Moreover, I tried to pass that foo...
4
3638
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 useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
26
3073
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 read some of the items from the bottom up of the buffer, and some from the top down, moving the bottom items back to the new re-allocated bottom on every file read. Then when I've read all four files, I sort the top and bottom items separately...
1
1497
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
3663
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 string to a integer number. bool Convert(const string& str, int* pData); bool Convert(const string& str, int& data);
18
2000
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 pointers are malloced within that function. No problem here. Now i have a situation in this software, where i need to assign new coordinates to all nodes in the list.
14
2729
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 mallocs and ect, but i removed them to help me debug. thanks. #include <stdio.h> #include <stdlib.h> #include <string.h> void freadl ( FILE *stream, char **string ) {
14
1836
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 problem that seems to be related to a malloc function. I wrote a little piece of code to reproduce the situation: #include <stdlib.h> #include <stdio.h>
0
9826
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
9674
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10860
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10560
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...
0
10261
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9389
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...
0
5659
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4466
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
2
4026
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.