Some basic questions that need help...

Posted on 2003-11-17
Last Modified: 2010-04-15
I'm quite new to C programming, so I hope u guys'll bare with me in some of these very basic questions:
1) char *ctime(const time_t *timep)
what is the * used for in the above function? Is the char infront the same as the date type character (1 byte char)?

I'm at a lost because I'm confused with my Turbo C compiler's help documentation and couldn't understand and practise on most of the useful functions.

2) How to I use the void gettime(struct time *timep) and void getdate(struct date *datep) functions? What are *timep and *datep?  Pls give an example or 2.

3) I managed to output the time to screen, but it's a string. How do I extract say, only the day from the string? Here's my code:

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

main() {
long ltime;
printf("Current time is:%s",ctime(&ltime));
} /* main */

Thanks for ur time =)
Question by:bobbytcy
  • 3
LVL 45

Expert Comment

ID: 9763320
Hi bobbytcy,

>1) char *ctime(const time_t *timep)
>what is the * used for in the above function?
* here represents pointer ...
>Is the char infront the same as the date type character (1 byte char)?
char is the same as data tyoe char but char * means a pointer to a char which on most systems is 4 bytes ... to get the size on your system, use sizeof ( char * )

>2) How to I use the void gettime(struct time *timep) and void getdate(struct date *datep) functions? What are *timep and
>*datep?  Pls give an example or 2.

struct time * t;
gettime ( &t );

struct date * p;
getdate ( &p );

I figured out their usage from the declaration ... be warned that these are not standard ANSI C functions ... for a list of all time realted ANSI C functions, look here

*timep means that timep is a pointer to data type struct time

>3) I managed to output the time to screen, but it's a string. How do I extract say, only the day from the string? Here's my
there are a number of ways, I am giving you the easiest

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

main() {
long ltime;
struct tm * tp;

tp = localtime (&ltime);
printf("Current day is:%d", tp->tm_mday);
} /* main */

struct tm is defined as

              struct tm
                      int     tm_sec;         /* seconds */
                      int     tm_min;         /* minutes */
                      int     tm_hour;        /* hours */
                      int     tm_mday;        /* day of the month */
                      int     tm_mon;         /* month */
                      int     tm_year;        /* year */
                      int     tm_wday;        /* day of the week */
                      int     tm_yday;        /* day in the year */
                      int     tm_isdst;       /* daylight saving time */

from your post, it seems that you are not yet familiar with pointer and other features of C... I would suggest that you read a book thoroughly ... here is a link to a simple tutorial
LVL 45

Accepted Solution

sunnycoder earned 300 total points
ID: 9763332
CHAPTER 1: What is a pointer?
One of those things beginners in C find difficult is the concept of pointers. The purpose of this tutorial is to provide an introduction to pointers and their use to these beginners.

I have found that often the main reason beginners have a problem with pointers is that they have a weak or minimal feeling for variables, (as they are used in C). Thus we start with a discussion of C variables in general.

A variable in a program is something with a name, the value of which can vary. The way the compiler and linker handles this is that it assigns a specific block of memory within the computer to hold the value of that variable. The size of that block depends on the range over which the variable is allowed to vary. For example, on 32 bit PC's the size of an integer variable is 4 bytes. On older 16 bit PCs integers were 2 bytes.  In C the size of a variable type such as an integer need not be the same on all types of machines.  Further more there is more than one type of integer variable in C.  We have integers, long integers and short integers which you can read up on in any basic text on C.  This document assumes the use of a 32 bit system with 4 byte integers.

If you want to know the size of the various types of integers on your system, running the following code will give you that information.

#include <stdio.h>

int main()
printf("size of a short is %d\n", sizeof(short));
printf("size of a int is %d\n", sizeof(int));
printf("size of a long is %d\n", sizeof(long));

When we declare a variable we inform the compiler of two things, the name of the variable and the type of the variable. For example, we declare a variable of type integer with the name k by writing:

    int k;

On seeing the "int" part of this statement the compiler sets aside 4 bytes of memory (on a PC) to hold the value of the integer. It also sets up a symbol table. In that table it adds the symbol k and the relative address in memory where those 4 bytes were set aside.

Thus, later if we write:

    k = 2;

we expect that, at run time when this statement is executed, the value 2 will be placed in that memory location reserved for the storage of the value of k. In C we refer to a variable such as the integer k as an "object".

In a sense there are two "values" associated with the object k. One is the value of the integer stored there (2 in the above example) and the other the "value" of the memory location, i.e., the address of k. Some texts refer to these two values with the nomenclature rvalue (right value, pronounced "are value") and lvalue (left value, pronounced "el value") respectively.

In some languages, the lvalue is the value permitted on the left side of the assignment operator '=' (i.e. the address where the result of evaluation of the right side ends up). The rvalue is that which is on the right side of the assignment statement, the 2 above. Rvalues cannot be used on the left side of the assignment statement. Thus: 2 = k; is illegal.

Actually, the above definition of "lvalue" is somewhat modified for C. According to K&R II (page 197): [1]

"An object is a named region of storage; an lvalue is an expression referring to an object."
However, at this point, the definition originally cited above is sufficient. As we become more familiar with pointers we will go into more detail on this.

Okay, now consider:

   int j, k;

    k = 2;
    j = 7;    <-- line 1
    k = j;    <-- line 2

In the above, the compiler interprets the j in line 1 as the address of the variable j (its lvalue) and creates code to copy the value 7 to that address. In line 2, however, the j is interpreted as its rvalue (since it is on the right hand side of the assignment operator '='). That is, here the j refers to the value stored at the memory location set aside for j, in this case 7. So, the 7 is copied to the address designated by the lvalue of k.

In all of these examples, we are using 4 byte integers so all copying of rvalues from one storage location to the other is done by copying 4 bytes. Had we been using two byte integers, we would be copying 2 bytes.

Now, let's say that we have a reason for wanting a variable designed to hold an lvalue (an address). The size required to hold such a value depends on the system. On older desk top computers with 64K of memory total, the address of any point in memory can be contained in 2 bytes. Computers with more memory would require more bytes to hold an address.  The actual size required is not too important so long as we have a way of informing the compiler that what we want to store is an address.

Such a variable is called a pointer variable (for reasons which hopefully will become clearer a little later). In C when we define a pointer variable we do so by preceding its name with an asterisk. In C we also give our pointer a type which, in this case, refers to the type of data stored at the address we will be storing in our pointer. For example, consider the variable declaration:

   int *ptr;

ptr is the name of our variable (just as k was the name of our integer variable). The '*' informs the compiler that we want a pointer variable, i.e. to set aside however many bytes is required to store an address in memory. The int says that we intend to use our pointer variable to store the address of an integer. Such a pointer is said to "point to" an integer. However, note that when we wrote int k; we did not give k a value. If this definition is made outside of any function ANSI compliant compilers will initialize it to zero. Similarly, ptr has no value, that is we haven't stored an address in it in the above declaration. In this case, again if the declaration is outside of any function, it is initialized to a value guaranteed in such a way that it is guaranteed to not point to any C object or function. A pointer initialized in this manner is called a "null" pointer.

The actual bit pattern used for a null pointer may or may not evaluate to zero since it depends on the specific system on which the code is developed. To make the source code compatible between various compilers on various systems, a macro is used to represent a null pointer. That macro goes under the name NULL. Thus, setting the value of a pointer using the NULL macro, as with an assignment statement such as ptr = NULL, guarantees that the pointer has become a null pointer. Similarly, just as one can test for an integer value of zero, as in if(k == 0), we can test for a null pointer using if (ptr == NULL).

But, back to using our new variable ptr. Suppose now that we want to store in ptr the address of our integer variable k. To do this we use the unary & operator and write:

    ptr = &k;

What the & operator does is retrieve the lvalue (address) of k, even though k is on the right hand side of the assignment operator '=', and copies that to the contents of our pointer ptr. Now, ptr is said to "point to" k. Bear with us now, there is only one more operator we need to discuss.

The "dereferencing operator" is the asterisk and it is used as follows:

    *ptr = 7;

will copy 7 to the address pointed to by ptr. Thus if ptr "points to" (contains the address of) k, the above statement will set the value of k to 7. That is, when we use the '*' this way we are referring to the value of that which ptr is pointing to, not the value of the pointer itself.

Similarly, we could write:


to print to the screen the integer value stored at the address pointed to by ptr;.

One way to see how all this stuff fits together would be to run the following program and then review the code and the output carefully.

------------ Program 1.1 ---------------------------------

/* Program 1.1 from PTRTUT10.TXT   6/10/97 */

#include <stdio.h>

int j, k;
int *ptr;

int main(void)
    j = 1;
    k = 2;
    ptr = &k;
    printf("j has the value %d and is stored at %p\n", j, (void *)&j);
    printf("k has the value %d and is stored at %p\n", k, (void *)&k);
    printf("ptr has the value %p and is stored at %p\n", ptr, (void *)&ptr);
    printf("The value of the integer pointed to by ptr is %d\n", *ptr);

    return 0;

take time to read this tutorial ... looks good

this post was from the same tutorial

Assisted Solution

gourav_jain earned 20 total points
ID: 9763417
1. what is the * used for in the above function ?
Ans: * is used to tell the compiler that i am going to store an address. Here in the code the char * indicates that you are going to return an address of the char generally this is used when the return type is a array of character.

2) How to I use the void gettime(struct time *timep) and void getdate(struct date *datep) functions? What are *timep and *datep?  Pls give an example or 2.
Ans: timep and datep are nothing but just the pointer variable of type struct time and struct date. when you will execute these functions the values of time and date will be assigned to these variables.

you can use it like this

struct time *timep;

then you can get the value of hour, min, sec thru this temep variable.

3.3) I managed to output the time to screen, but it's a string. How do I extract say, only the day from the string? Here's my code:
Ans: you can split the string using the sprintf function into your desired values.

int main()
        int year,day;
        char mon[10], wday[10],stime[10];
        long ltime;
        sscanf(ctime(&ltime),"%s %s %d %s %d",&wday,&mon,&day,&stime,&year);
        printf("Current time is: %d %s",day,mon);

Gourav Jain

Author Comment

ID: 9770391
sunnycoder: Thanks 4 the comprehensive answer!!! you've more than answered my questions.

gourav_jain: Thanks for ur help too

bobbytcy =)
LVL 45

Expert Comment

ID: 9770404
glad to be of help :o)

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c++ VC2015.  Run-Time Check Failure #2 - Stack around the variable 'house' was corrupted. 2 216
C hashtable library 3 90
List out all word 7 292
Line meaning 9 86
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode ( They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question