C int pointers

This is a discussion on C int pointers within the C Programming forums, part of the General Programming Boards category; I'm learning C and have a question about this code: Code: #include <stdio.h> int main() { char date[] = "20091005"; ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    21

    C int pointers

    I'm learning C and have a question about this code:

    Code:
    #include <stdio.h>
    
    int main() {
    
    char date[]   = "20091005";
    
    char year[5]  = "YYYY";
    char month[3] = "MM";
    char day[3]   = "DD";
    
    char *my_pointer = date;
    
    year[0] = *(my_pointer);
    year[1] = *(my_pointer + 1);
    year[2] = *(my_pointer + 2);
    year[3] = *(my_pointer + 3);
    
    month[0] = *(my_pointer + 4);
    month[1] = *(my_pointer + 5);
    
    day[0] = *(my_pointer + 6);
    day[1] = *(my_pointer + 7);
    
    printf("The Year is: %s and the Month is: %s and the Day is: %s  ", year, month, day);
    
    return 0;
    }
    I changed it to this (changes marked in bold):

    Code:
    #include <stdio.h>
    
    int main() {
    
    int date[]   = 20091005; /* Removed quotes and changed char to int */
    
    int year[5]  = 2009; /* Removed quotes, changed char to int, and changed YYYY to 2009 */
    int month[3] = 10; /* Removed quotes, changed char to int, and changed MM to 10 */
    int day[3]   = 05; /* Removed quotes, changed char to int, and changed DD to 05 */
    
    int *my_pointer = date; /* Changed char to int */
    
    year[0] = *(my_pointer);
    year[1] = *(my_pointer + 1);
    year[2] = *(my_pointer + 2);
    year[3] = *(my_pointer + 3);
    
    month[0] = *(my_pointer + 4);
    month[1] = *(my_pointer + 5);
    
    day[0] = *(my_pointer + 6);
    day[1] = *(my_pointer + 7);
    
    printf("The Year is: %d and the Month is: %d and the Day is: %d  ", year, month, day); /* Changed %s to %d */
    
    return 0;
    }
    But it wouldn't compile due to this error:
    Code:
    practice1.c: In function 'main':
    practice1.c:5: error: invalid initializer
    practice1.c:7: error: invalid initializer
    practice1.c:8: error: invalid initializer
    practice1.c:9: error: invalid initializer
    Why can I not use int instead of char for the pointer?

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    Okay I just realized 20091005 is just a really big number 20,091,005. Even if I change that data type to 'unsigned double long' or something I guess it wouldn't make a difference but I still don't understand why I need to represent these numbers as characters.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    21
    Okay, so this code compiled fine:

    Code:
    #include <stdio.h>
    
    int main() {
    
    int date[]  = {20091005};
    
    int year[5]  = {2009};
    int month[3] = {10};
    int day[3]   = {05};
    
    int *my_pointer = date;
    
    year[0] = *(my_pointer);
    year[1] = *(my_pointer + 1);
    year[2] = *(my_pointer + 2);
    year[3] = *(my_pointer + 3);
    
    month[0] = *(my_pointer + 4);
    month[1] = *(my_pointer + 5);
    
    day[0] = *(my_pointer + 6);
    day[1] = *(my_pointer + 7);
    
    printf("The Year is: %d and the Month is: %d and the Day is: %d \n", year, month, day);
    
    return 0;
    }
    But the results are:

    Code:
    $ a.out                                                         
    The Year is: -809539600 and the Month is: -809539568 and the Day is: -809539580 
    $
    Isn't it:

    date[] = [2][0][0][9][1][0][0][5][\0] (9 array spots)

    year[5] = [2][0][0][9][\0]
    month[3] = [1][0][\0]
    day[3] = [0][5][\0]

    Or am I understanding this incorrectly?

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    No.

    Code:
    date[] = [2][0][0][9][1][0][0][5][\0] (9 array spots)
    is one array spot that takes up 4 bytes.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Since you changed things to int arrays, it's totally broken.
    year[0], year[1] is meaningless the way you meant it. What are you trying to do? Extract the digits?

    *(my_pointer + 1) etc is wrong. You don't have an array of dates to begin with. At least the elements of which are not defined.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by guitarscn View Post
    Okay, so this code compiled fine:
    "Compiled" does not equal "works".

    Or am I understanding this incorrectly?
    Unfortunately I'd have to say you are way way off base here...

    What exactly are you trying to accomplish?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stdio.h?
    By kiros88 in forum C Programming
    Replies: 5
    Last Post: 05-21-2010, 07:09 PM
  2. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

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