Pointer Question

This is a discussion on Pointer Question within the C Programming forums, part of the General Programming Boards category; Why does the following work: <code> #include <stdio.h> #include <time.h> int main() { time_t now; time(&now); printf("It's now %s\n",ctime(&now)); return ...

  1. #1
    Unregistered
    Guest

    Pointer Question

    Why does the following work:
    <code>
    #include <stdio.h>
    #include <time.h>

    int main()
    {
    time_t now;
    time(&now);
    printf("It's now %s\n",ctime(&now));
    return 0;
    }
    </code>

    And not the following:

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

    int main()
    {
    time_t *now;
    time(now);
    printf("It's now %s\n",ctime(now));
    return 0;
    }
    </code>

    I assume that it is because pointers have to be pointed to a memory location when initialized but, I am not sure of the syntax. I tried:
    time_t *now = new time_t;
    as suggested in the tutorials but, that just gave me errors.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >time_t *now;
    >time(now);
    This won't work because now is a pointer that is uninitialised, and therefore doesn't point to valid memory.

    To allocate memory, use malloc().

    This:
    >time_t *now = new time_t;
    is C++ syntax.

    Code:
    #include <stdio.h> 
    #include <time.h> 
    #include <stdlib.h>
    
    int main(void) 
    { 
    	time_t *now; 
    	if ((now = malloc(sizeof(time_t))) == NULL)
    	{
    		printf ("malloc failure\n");
    		return EXIT_FAILURE;
    	}
    	
    	time(now); 
    	printf("It's now %s\n",ctime(now)); 
    	free(now);
    	return EXIT_SUCCESS; 
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Sayeh
    Guest
    And, to add to Hammer's response, more importantly:

    "&now" and "*now" are NOT the same thing.

    ---

    "&now" means "Address of variable named 'now'.".

    "*now" means "Point indirectly through contents of 'now'.".

    ---

    See how the two are different? Let's look at some RAM:


    Code:
    Address      Data
    ----------------------------------------------
    00000000  01 A7 5E 3F D2 B2 CC 0D
    00000008  00 A7 55 61 32 21 1A 7F
    00000010  20 20 20 16 A4 EE C6 2B
    00000018  00 00 00 08 8C 9D 23 00
    00000020  00 01 A3 22 49 00 00 00
    ...
    00000048  E7 0D 07 13 65 AA C0 C0
    00000050  55 61 32 21 00 A7 55 61
    ...
    Let's say the compiler creates 'now' at location 0x00000018. If we say the following:

    "x = &now;" 'x' is going to equal 0x00000018. if we say
    "x = *now;" 'x' is going to equal 0x00A75561.

    Because the contents of now (at location 18hex) is address 08hex. Well, if we look at location 08hex (0x00000008), what do we find? We see 0x00A75561.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 09:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 10:59 AM
  4. Pointer question
    By rakan in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2006, 01:23 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21