Home > Enterprise >  Output pointer returns (null) instead of char value
Output pointer returns (null) instead of char value

Time:12-20

Program below have to return pointer to an array of names taken from names[] if *pMonthToStudy parameter of Exam function equals birth month of coressponding person (birthDates[] order corresponds to names[] order). In case of June, function should return "John, James, Richard". The output must have exactly the same view as my example.

Do I allocate memory correct? If yes, how to assign those values to returning pointer and output it correctly in main (if my output method is incorrect)?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    int day;
    char month[4];
    int year;
} DATE;

DATE birthDates[] = {{2, "Jun", 2000}, {27, "Jul", 2001}, {12, "Jun", 1999},
                     {15, "Sep", 1998}, {16, "Jun", 2000}};

char names[] = {"John, Mary, James, Elizabeth, Richard"};

char *Exam(char *pNames, DATE *pBirthDates, const char *pMonthToStudy){
    //Check if input ptr are zero or point to empty strings
    if(!pNames || !*pNames || !pBirthDates)
        return 0;
    char *pOutput = (char*) malloc(sizeof(*pNames));
    for (int i = 0; i < 5;   i) {
        if(strcmp(pMonthToStudy, &birthDates->month[i]) == 0){
            strcpy(pOutput, &pNames[i]);
        } else
            return 0;
    }
    return pOutput;
}
int main() {
    char *pResult = Exam(names, birthDates, "Jun");
    printf("%s", pResult);
    return 0;
}

CodePudding user response:

For starters this declaration

char names[] = {"John, Mary, James, Elizabeth, Richard"};

declares a character array with one element that contains the string literal

"John, Mary, James, Elizabeth, Richard"

As a result in this declaration

char *pOutput = (char*) malloc(sizeof(*pNames));

there is allocated only one character.

It seems you mean an array of 5 elements of pointers to string literals

char * names[] = { "John", "Mary", "James", "Elizabeth", "Richard"};

Nevertheless in this declaration with a wrong initialization

char *pOutput = (char*) malloc(sizeof(*pNames));

you need to allocate a character array that can be able to store a corresponding string literal in the array names.

This allocation must be done after the for loop if the target record is found. Otherwise the function will have a memory leak.

The for loop is incorrect

for (int i = 0; i < 5;   i) {
    if(strcmp(pMonthToStudy, &birthDates->month[i]) == 0){
        strcpy(pOutput, &pNames[i]);
    } else
        return 0;
}

It can return 0 in the first iteration of the loop or 0 even when the target record will be found because you do not interrupt the loop in this case. And moreover you are using incorrect expressions in the functions strcmp and strcpy.

Instead write

char *pOutput = NULL;

int i = 0;

while ( i < 5 && strcmp( pMonthToStudy, birthDates[i].month ) != 0 )   i;


if ( i != 5 )
{
    pOutput = malloc( strlen( pNames[i] )   1 );
    if ( pOutput != NULL ) strcpy( pOutput, pNames[i] );
}

return pOutput;

Pay attention to that it is a bad idea to use the magic number 5 within the function. You should pass the number of elements in the referenced arrays through a function parameter.

CodePudding user response:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    int day;
    char month[4];
    int year;
} DATE;

DATE birthDates[] = { {2, "Jun", 2000}, {27, "Jul", 2001}, {12, "Jun", 1999},
                     {15, "Sep", 1998}, {16, "Jun", 2000} };

const char *names[] = { "John", "Mary", "James", "Elizabeth", "Richard" };

char *Exam(const char *pNames, DATE *pBirthDates, const char *pMonthToStudy) {
    if (!pNames || !*pNames || !pBirthDates)
        return 0;
    char *pOutput = NULL;

    int i = 0;

    while (i < 5 && strcmp(pMonthToStudy, birthDates[i].month) != 0)   i;

    if (i != 5)
    {
        pOutput = (char *)(malloc(strlen(&pNames[i])   1));
        memset(pOutput, 0, sizeof pOutput);
        if (pOutput != NULL) strcpy_s(pOutput, sizeof &pNames[i], &pNames[i]);
    }
    return pOutput;
}
int main() {
    char *pResult = Exam(*names, birthDates, "Jun");
    printf("%s", pResult);
    return 0;
}
  • Related