Display char elements of a structure in C

Trying to display the char elements of my structure:

#include <stdio.h>

typedef struct
{
    int ID;
    char lastName[10];
    char firstName[10];
    char birthMonth[2];
    char birthDay[2];
    char birthYear[4];
} studentData;

int main()
{
    studentData admin={1111,"Brian","John","7","24","1960"};

    printf("ID: %d\n",admin.ID);
    printf("Last Name: %s\n",admin.lastName);
    printf("First Name: %s\n",admin.firstName);
    printf("Birth Month: %s\n",admin.birthMonth);
    printf("Birth Day: %s\n",admin.birthDay);
    printf("Birth Year: %s\n",admin.birthYear);
}

My results are:

ID: 1111
Last Name: Brian
First Name: John
Birth Month: 7
Birth Day: 241960▒E▒▒P@[▒_▒BP▒
Birth Year: 1960▒E▒▒P@[▒_▒BP▒

Why?

In addition: The Combination of the birthMonth and birthDay should be a total of 4 bytes, I need to create a function to retrieve these bytes as an integer and print the result.Sorry that wasn't clear enough.I think the intent is to get the value locations byte by byte, and changing that (HEX) to Decimal and printing that out How would I go about this?

eg for birthdate 01 and birthmonth 07 the output would be

Address of Day: 0xbfb91f98
Address of Month: 0xbfb91f9a
Address of day and month is at address: bfb91f98
integer value of Day and Month is :925905200

Upvotes: 0

Views: 386

Answers (4)

ad absurdum
ad absurdum

Reputation: 21323

Plenty has been said about the need for adequate space in your character arrays. As for the final part of your question:

In addition: The Combination of the birthMonth and birthDay should be a total of 4 bytes, I need to retrieve these bytes as an integer and print the result. How would I go about this?

I am not exactly sure what you want here. First, birthMonth and birthDay represent a combined total of 6 bytes, including the NUL terminators of the strings. You can convert these strings to numbers using, e.g, the strtol() function, and then you can work with the date information numerically. If you want to form a number from only the month and day numbers after this, that is easy.

The following code converts the strings birthMonth, birthDay, and birthYear to long ints, and then prints the date of birth in month/day/year format. Then the bmonth and bday longs are combined into one long to give single value, birth_digits.

#include <stdio.h>
#include <stdlib.h>  // for strtol()

typedef struct
{
    int ID;
    char lastName[11];
    char firstName[11];
    char birthMonth[3];
    char birthDay[3];
    char birthYear[5];
} studentData;

int main(void)
{
    studentData admin={1111,"Brian","John","7","24","1960"};
    long bmonth, bday, byear;
    long birth_digits;

    bmonth = strtol(admin.birthMonth, NULL, 10);
    bday = strtol(admin.birthDay, NULL, 10);
    byear = strtol(admin.birthYear, NULL, 10);

    printf("ID: %d\n",admin.ID);
    printf("Last Name: %s\n",admin.lastName);
    printf("First Name: %s\n",admin.firstName);
    printf("Birth Month: %s\n",admin.birthMonth);
    printf("Birth Day: %s\n",admin.birthDay);
    printf("Birth Year: %s\n",admin.birthYear);

    printf("DOB: %lu/%lu/%lu\n", bmonth, bday, byear);

    /* Convert bmonth and bday to one number */
    if (bday / 10) {
        birth_digits = bmonth * 100 + bday;
    } else {
        birth_digits = bmonth * 10 + bday;
    }

    printf("DOB Number: %lu\n", birth_digits);

    return 0;
}

The resulting program has this output:

ID: 1111
Last Name: Brian
First Name: John
Birth Month: 7
Birth Day: 24
Birth Year: 1960
DOB: 7/24/1960
DOB Number: 724

Upvotes: 1

RoadRunner
RoadRunner

Reputation: 26315

As others have said, you are not making enough space for the \0 terminator at the end of each string. Add +1 to your arrays, to accommodate space for \0.

Also, since you are using int main(), you need to return 0 at the end, or this will just trigger unnecessary warnings in your code if you use -Wall during compilation.

You need to do something like this:

#include <stdio.h>

#define NAMESTRLEN 10
#define MONTHLEN 2
#define DAYLEN 2
#define YEARLEN 4

typedef struct 
{
    int ID;
    char lastName[NAMESTRLEN+1];
    char firstName[NAMESTRLEN+1];
    char birthMonth[MONTHLEN+1];
    char birthDay[DAYLEN+1];
    char birthYear[YEARLEN+1];
} studentData;

int main()
{
    studentData admin={1111,"Brian","John","7","24","1960"};

    printf("ID: %d\n",admin.ID);
    printf("Last Name: %s\n",admin.lastName);
    printf("First Name: %s\n",admin.firstName);
    printf("Birth Month: %s\n",admin.birthMonth);
    printf("Birth Day: %s\n",admin.birthDay);
    printf("Birth Year: %s\n",admin.birthYear);

    return 0;
}

Output:

ID: 1111
Last Name: Brian
First Name: John
Birth Month: 7
Birth Day: 24
Birth Year: 1960

Upvotes: 1

MayurK
MayurK

Reputation: 1947

You need to put '\0' at the end of char array to make it strings. birthMonth, birthDay and birthYear have no space for it. So add one more space for them.

   char birthMonth[3];
    char birthDay[3];
    char birthYear[5];

Upvotes: 2

shiv
shiv

Reputation: 1952

Your birthday is wrong. "24" is three characters with hidden \0 as string terminator. Try char birthday[3]; and char birthday[5];

Your birthYear is also wrong. You need larger arrays. Even birthMonth will not work for October, November and December. Make that 3 as well.

Upvotes: 2

Related Questions